From 7397095a66fc0e0aa7fe9c30e399c83e28cddf34 Mon Sep 17 00:00:00 2001 From: strwbrry1 Date: Wed, 3 Apr 2024 12:02:51 +0400 Subject: [PATCH 1/4] Lab 4 done --- .../ArrayGenericObjects.cs | 8 +- .../CollectionType.cs | 17 ++ .../ListGenericObjects.cs | 66 ++++++ .../StorageCollection.cs | 62 ++++++ .../Catamaran/FormBoatColletion.Designer.cs | 193 +++++++++++++++--- Catamaran/Catamaran/FormBoatColletion.cs | 96 ++++++++- 6 files changed, 396 insertions(+), 46 deletions(-) create mode 100644 Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs create mode 100644 Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs index 8131fd0..305a8fb 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs @@ -25,7 +25,7 @@ namespace Catamaran.CollectionGenericObjects } else { - _collection = new T[value]; + _collection = new T?[value]; } } } @@ -38,7 +38,11 @@ namespace Catamaran.CollectionGenericObjects public T? Get(int position) { - return _collection[position]; + if (position >= 0 && position < Count) + { + return _collection[position]; + } + return null; } public int Insert(T obj) diff --git a/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs b/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..3136a70 --- /dev/null +++ b/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.CollectionGenericObjects +{ + public enum CollectionType + { + None = 0, + + Array = 1, + + List = 2 + } +} diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..8a537b6 --- /dev/null +++ b/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.CollectionGenericObjects +{ + public class ListGenericObjects : ICollectionGenericObjects + where T : class + { + + private readonly List _collection; + + private int _maxCount; + + public int Count => _collection.Count; + + public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + + public ListGenericObjects() + { + _collection = new(); + } + + public T? Get(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + if (Count + 1 > _maxCount) + { + return -1; + } + _collection.Add(obj); + return 1; + } + + public int Insert(T obj, int position) + { + if (position < 0 || position > Count || Count + 1 > _maxCount) + { + return -1; + } + _collection.Insert(position, obj); + return 1; + } + + public T? Remove(int position) + { + if (position < 0 || position > Count) + { + return null; + } + T? obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + } + } +} diff --git a/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs b/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..3108130 --- /dev/null +++ b/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,62 @@ +using Catamaran.Drawings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.CollectionGenericObjects +{ + public class StorageCollection + where T : class + { + readonly Dictionary> _storages; + + public List Keys => _storages.Keys.ToList(); + + public StorageCollection() + { + _storages = new Dictionary>(); + } + + public void AddCollection(string name, CollectionType collectionType) + { + if (!string.IsNullOrEmpty(name) && !_storages.ContainsKey(name)) + { + switch (collectionType) + { + case CollectionType.None: + return; + case CollectionType.Array: + _storages[name] = new ArrayGenericObjects(); + return; + case CollectionType.List: + _storages[name] = new ListGenericObjects(); + return; + } + + } + } + + public void DelCollection(string name) + { + if (string.IsNullOrEmpty(name) || !_storages.ContainsKey(name)) + { + return; + } + _storages.Remove(name); + } + + public ICollectionGenericObjects? this[string name] + { + get + { + if (!_storages.ContainsKey(name) || string.IsNullOrEmpty(name)) + { + return null; + } + return _storages[name]; + } + } + } +} diff --git a/Catamaran/Catamaran/FormBoatColletion.Designer.cs b/Catamaran/Catamaran/FormBoatColletion.Designer.cs index a1e3696..aa2c822 100644 --- a/Catamaran/Catamaran/FormBoatColletion.Designer.cs +++ b/Catamaran/Catamaran/FormBoatColletion.Designer.cs @@ -29,27 +29,36 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + buttonCompanyCreate = new Button(); + panelCollection = new Panel(); + buttonCollectionDelete = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonArray = new RadioButton(); + textBoxCollectionName = new TextBox(); + labelCollectionName = new Label(); UpdateButton = new Button(); GoToTestButton = new Button(); DeleteButton = new Button(); maskedTextBox = new MaskedTextBox(); AddCatamaranButton = new Button(); AddBoatButton = new Button(); - InstrumentBox = new ComboBox(); + comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + panelCompanyTools = new Panel(); groupBox1.SuspendLayout(); + panelCollection.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + panelCompanyTools.SuspendLayout(); SuspendLayout(); // // groupBox1 // - groupBox1.Controls.Add(UpdateButton); - groupBox1.Controls.Add(GoToTestButton); - groupBox1.Controls.Add(DeleteButton); - groupBox1.Controls.Add(maskedTextBox); - groupBox1.Controls.Add(AddCatamaranButton); - groupBox1.Controls.Add(AddBoatButton); - groupBox1.Controls.Add(InstrumentBox); + groupBox1.Controls.Add(panelCompanyTools); + groupBox1.Controls.Add(buttonCompanyCreate); + groupBox1.Controls.Add(panelCollection); + groupBox1.Controls.Add(comboBoxSelectorCompany); groupBox1.Dock = DockStyle.Right; groupBox1.Location = new Point(809, 0); groupBox1.Name = "groupBox1"; @@ -58,11 +67,101 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // buttonCompanyCreate + // + buttonCompanyCreate.Location = new Point(6, 366); + buttonCompanyCreate.Name = "buttonCompanyCreate"; + buttonCompanyCreate.Size = new Size(225, 29); + buttonCompanyCreate.TabIndex = 8; + buttonCompanyCreate.Text = "Создать компанию"; + buttonCompanyCreate.UseVisualStyleBackColor = true; + buttonCompanyCreate.Click += buttonCompanyCreate_Click; + // + // panelCollection + // + panelCollection.Controls.Add(buttonCollectionDelete); + panelCollection.Controls.Add(listBoxCollection); + panelCollection.Controls.Add(buttonCollectionAdd); + panelCollection.Controls.Add(radioButtonList); + panelCollection.Controls.Add(radioButtonArray); + panelCollection.Controls.Add(textBoxCollectionName); + panelCollection.Controls.Add(labelCollectionName); + panelCollection.Location = new Point(6, 26); + panelCollection.Name = "panelCollection"; + panelCollection.Size = new Size(225, 298); + panelCollection.TabIndex = 7; + // + // buttonCollectionDelete + // + buttonCollectionDelete.Location = new Point(3, 265); + buttonCollectionDelete.Name = "buttonCollectionDelete"; + buttonCollectionDelete.Size = new Size(219, 29); + buttonCollectionDelete.TabIndex = 6; + buttonCollectionDelete.Text = "Удалить коллекцию"; + buttonCollectionDelete.UseVisualStyleBackColor = true; + buttonCollectionDelete.Click += buttonCollectionDelete_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.Location = new Point(3, 155); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(216, 104); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 114); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(219, 29); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(91, 76); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(80, 24); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonArray + // + radioButtonArray.AutoSize = true; + radioButtonArray.Location = new Point(3, 76); + radioButtonArray.Name = "radioButtonArray"; + radioButtonArray.Size = new Size(82, 24); + radioButtonArray.TabIndex = 2; + radioButtonArray.TabStop = true; + radioButtonArray.Text = "Массив"; + radioButtonArray.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 37); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(219, 27); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(36, 5); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(158, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // UpdateButton // - UpdateButton.Location = new Point(6, 565); + UpdateButton.Location = new Point(3, 202); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(225, 38); + UpdateButton.Size = new Size(222, 38); UpdateButton.TabIndex = 6; UpdateButton.Text = "Обновить"; UpdateButton.UseVisualStyleBackColor = true; @@ -70,9 +169,9 @@ // // GoToTestButton // - GoToTestButton.Location = new Point(12, 355); + GoToTestButton.Location = new Point(3, 158); GoToTestButton.Name = "GoToTestButton"; - GoToTestButton.Size = new Size(225, 38); + GoToTestButton.Size = new Size(222, 38); GoToTestButton.TabIndex = 5; GoToTestButton.Text = "Передать на тесты"; GoToTestButton.UseVisualStyleBackColor = true; @@ -80,9 +179,9 @@ // // DeleteButton // - DeleteButton.Location = new Point(6, 238); + DeleteButton.Location = new Point(3, 113); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(225, 38); + DeleteButton.Size = new Size(222, 38); DeleteButton.TabIndex = 4; DeleteButton.Text = "Удалить лодку"; DeleteButton.UseVisualStyleBackColor = true; @@ -90,18 +189,18 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(6, 205); + maskedTextBox.Location = new Point(3, 80); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(225, 27); + maskedTextBox.Size = new Size(222, 27); maskedTextBox.TabIndex = 3; maskedTextBox.ValidatingType = typeof(int); // // AddCatamaranButton // - AddCatamaranButton.Location = new Point(6, 104); + AddCatamaranButton.Location = new Point(3, 41); AddCatamaranButton.Name = "AddCatamaranButton"; - AddCatamaranButton.Size = new Size(225, 38); + AddCatamaranButton.Size = new Size(222, 33); AddCatamaranButton.TabIndex = 2; AddCatamaranButton.Text = "Добавить катамаран"; AddCatamaranButton.UseVisualStyleBackColor = true; @@ -109,26 +208,26 @@ // // AddBoatButton // - AddBoatButton.Location = new Point(6, 60); + AddBoatButton.Location = new Point(3, 3); AddBoatButton.Name = "AddBoatButton"; - AddBoatButton.Size = new Size(225, 38); + AddBoatButton.Size = new Size(222, 32); AddBoatButton.TabIndex = 1; AddBoatButton.Text = "Добавить лодку"; AddBoatButton.UseVisualStyleBackColor = true; AddBoatButton.Click += AddBoatButton_Click; // - // InstrumentBox + // comboBoxSelectorCompany // - InstrumentBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - InstrumentBox.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" }); - InstrumentBox.DropDownStyle = ComboBoxStyle.DropDownList; - InstrumentBox.FormattingEnabled = true; - InstrumentBox.Items.AddRange(new object[] { "Хранилище" }); - InstrumentBox.Location = new Point(6, 26); - InstrumentBox.Name = "InstrumentBox"; - InstrumentBox.Size = new Size(225, 28); - InstrumentBox.TabIndex = 0; - InstrumentBox.SelectedIndexChanged += InstrumentBox_SelectedIndexChanged; + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" }); + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(6, 330); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(225, 28); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += comboBoxSelectorCompany_SelectedIndexChanged; // // pictureBox // @@ -139,6 +238,21 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(AddCatamaranButton); + panelCompanyTools.Controls.Add(AddBoatButton); + panelCompanyTools.Controls.Add(UpdateButton); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(GoToTestButton); + panelCompanyTools.Controls.Add(DeleteButton); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 398); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(231, 241); + panelCompanyTools.TabIndex = 9; + // // FormBoatColletion // AutoScaleDimensions = new SizeF(8F, 20F); @@ -149,15 +263,18 @@ Name = "FormBoatColletion"; Text = "Коллекция лодок"; groupBox1.ResumeLayout(false); - groupBox1.PerformLayout(); + panelCollection.ResumeLayout(false); + panelCollection.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } #endregion private GroupBox groupBox1; - private ComboBox InstrumentBox; + private ComboBox comboBoxSelectorCompany; private Button AddBoatButton; private Button AddCatamaranButton; private PictureBox pictureBox; @@ -165,5 +282,15 @@ private Button DeleteButton; private Button GoToTestButton; private Button UpdateButton; + private Panel panelCollection; + private RadioButton radioButtonList; + private RadioButton radioButtonArray; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private Button buttonCompanyCreate; + private Button buttonCollectionDelete; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/Catamaran/Catamaran/FormBoatColletion.cs b/Catamaran/Catamaran/FormBoatColletion.cs index ff418d8..2693c35 100644 --- a/Catamaran/Catamaran/FormBoatColletion.cs +++ b/Catamaran/Catamaran/FormBoatColletion.cs @@ -17,21 +17,17 @@ namespace Catamaran private AbstractCompany? _company = null; - + private readonly StorageCollection _storageCollection; public FormBoatColletion() { InitializeComponent(); + _storageCollection = new(); } - private void InstrumentBox_SelectedIndexChanged(object sender, EventArgs e) + private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (InstrumentBox.Text) - { - case "Хранилище": - _company = new Harbor(pictureBox.Width, pictureBox.Height, new ArrayGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } private void CreateObject(string type) @@ -133,10 +129,10 @@ namespace Catamaran DrawingBoat? boat = null; int counter = 100; - while (boat == null) - { + while (boat == null) + { boat = _company.GetRandomObject(); - + counter--; if (counter <= 0) break; } @@ -152,5 +148,83 @@ namespace Catamaran }; form.ShowDialog(); } + + private void RefreshListBoxItems() + { + listBoxCollection.Items.Clear(); + for (int i = 0; i < _storageCollection.Keys?.Count; ++i) + { + string? colName = _storageCollection.Keys?[i]; + if (!string.IsNullOrEmpty(colName) ) + { + listBoxCollection.Items.Add(colName); + } + } + } + + private void buttonCollectionAdd_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonArray.Checked && !radioButtonList.Checked)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + CollectionType collectionType = CollectionType.None; + if (radioButtonArray.Checked) + { + collectionType = CollectionType.Array; + } + else if (radioButtonList.Checked) + { + collectionType = CollectionType.List; + } + + _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + RefreshListBoxItems(); + } + + private void buttonCollectionDelete_Click(object sender, EventArgs e) + { + if (listBoxCollection.SelectedItem == null || listBoxCollection.SelectedIndex < 0) + { + MessageBox.Show("Не выбрана коллекция"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RefreshListBoxItems(); + } + + private void buttonCompanyCreate_Click(object sender, EventArgs e) + { + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + + ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; + if (collection == null) + { + MessageBox.Show("Коллекция не инициализирована"); + return; + } + + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new Harbor(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RefreshListBoxItems(); + } + + } } -- 2.25.1 From fdf88350d6ab6469f9e7c4656dcc81261633074a Mon Sep 17 00:00:00 2001 From: strwbrry1 Date: Wed, 3 Apr 2024 12:45:00 +0400 Subject: [PATCH 2/4] Revert "Lab 4 done" This reverts commit 7397095a66fc0e0aa7fe9c30e399c83e28cddf34. --- .../ArrayGenericObjects.cs | 8 +- .../CollectionType.cs | 17 -- .../ListGenericObjects.cs | 66 ------ .../StorageCollection.cs | 62 ------ .../Catamaran/FormBoatColletion.Designer.cs | 193 +++--------------- Catamaran/Catamaran/FormBoatColletion.cs | 96 +-------- 6 files changed, 46 insertions(+), 396 deletions(-) delete mode 100644 Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs delete mode 100644 Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs delete mode 100644 Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs index 305a8fb..8131fd0 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs @@ -25,7 +25,7 @@ namespace Catamaran.CollectionGenericObjects } else { - _collection = new T?[value]; + _collection = new T[value]; } } } @@ -38,11 +38,7 @@ namespace Catamaran.CollectionGenericObjects public T? Get(int position) { - if (position >= 0 && position < Count) - { - return _collection[position]; - } - return null; + return _collection[position]; } public int Insert(T obj) diff --git a/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs b/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs deleted file mode 100644 index 3136a70..0000000 --- a/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Catamaran.CollectionGenericObjects -{ - public enum CollectionType - { - None = 0, - - Array = 1, - - List = 2 - } -} diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs deleted file mode 100644 index 8a537b6..0000000 --- a/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Catamaran.CollectionGenericObjects -{ - public class ListGenericObjects : ICollectionGenericObjects - where T : class - { - - private readonly List _collection; - - private int _maxCount; - - public int Count => _collection.Count; - - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } - - public ListGenericObjects() - { - _collection = new(); - } - - public T? Get(int position) - { - if (position < 0 || position >= Count) - { - return null; - } - return _collection[position]; - } - - public int Insert(T obj) - { - if (Count + 1 > _maxCount) - { - return -1; - } - _collection.Add(obj); - return 1; - } - - public int Insert(T obj, int position) - { - if (position < 0 || position > Count || Count + 1 > _maxCount) - { - return -1; - } - _collection.Insert(position, obj); - return 1; - } - - public T? Remove(int position) - { - if (position < 0 || position > Count) - { - return null; - } - T? obj = _collection[position]; - _collection.RemoveAt(position); - return obj; - } - } -} diff --git a/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs b/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs deleted file mode 100644 index 3108130..0000000 --- a/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs +++ /dev/null @@ -1,62 +0,0 @@ -using Catamaran.Drawings; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Catamaran.CollectionGenericObjects -{ - public class StorageCollection - where T : class - { - readonly Dictionary> _storages; - - public List Keys => _storages.Keys.ToList(); - - public StorageCollection() - { - _storages = new Dictionary>(); - } - - public void AddCollection(string name, CollectionType collectionType) - { - if (!string.IsNullOrEmpty(name) && !_storages.ContainsKey(name)) - { - switch (collectionType) - { - case CollectionType.None: - return; - case CollectionType.Array: - _storages[name] = new ArrayGenericObjects(); - return; - case CollectionType.List: - _storages[name] = new ListGenericObjects(); - return; - } - - } - } - - public void DelCollection(string name) - { - if (string.IsNullOrEmpty(name) || !_storages.ContainsKey(name)) - { - return; - } - _storages.Remove(name); - } - - public ICollectionGenericObjects? this[string name] - { - get - { - if (!_storages.ContainsKey(name) || string.IsNullOrEmpty(name)) - { - return null; - } - return _storages[name]; - } - } - } -} diff --git a/Catamaran/Catamaran/FormBoatColletion.Designer.cs b/Catamaran/Catamaran/FormBoatColletion.Designer.cs index aa2c822..a1e3696 100644 --- a/Catamaran/Catamaran/FormBoatColletion.Designer.cs +++ b/Catamaran/Catamaran/FormBoatColletion.Designer.cs @@ -29,36 +29,27 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); - buttonCompanyCreate = new Button(); - panelCollection = new Panel(); - buttonCollectionDelete = new Button(); - listBoxCollection = new ListBox(); - buttonCollectionAdd = new Button(); - radioButtonList = new RadioButton(); - radioButtonArray = new RadioButton(); - textBoxCollectionName = new TextBox(); - labelCollectionName = new Label(); UpdateButton = new Button(); GoToTestButton = new Button(); DeleteButton = new Button(); maskedTextBox = new MaskedTextBox(); AddCatamaranButton = new Button(); AddBoatButton = new Button(); - comboBoxSelectorCompany = new ComboBox(); + InstrumentBox = new ComboBox(); pictureBox = new PictureBox(); - panelCompanyTools = new Panel(); groupBox1.SuspendLayout(); - panelCollection.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); - panelCompanyTools.SuspendLayout(); SuspendLayout(); // // groupBox1 // - groupBox1.Controls.Add(panelCompanyTools); - groupBox1.Controls.Add(buttonCompanyCreate); - groupBox1.Controls.Add(panelCollection); - groupBox1.Controls.Add(comboBoxSelectorCompany); + groupBox1.Controls.Add(UpdateButton); + groupBox1.Controls.Add(GoToTestButton); + groupBox1.Controls.Add(DeleteButton); + groupBox1.Controls.Add(maskedTextBox); + groupBox1.Controls.Add(AddCatamaranButton); + groupBox1.Controls.Add(AddBoatButton); + groupBox1.Controls.Add(InstrumentBox); groupBox1.Dock = DockStyle.Right; groupBox1.Location = new Point(809, 0); groupBox1.Name = "groupBox1"; @@ -67,101 +58,11 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // - // buttonCompanyCreate - // - buttonCompanyCreate.Location = new Point(6, 366); - buttonCompanyCreate.Name = "buttonCompanyCreate"; - buttonCompanyCreate.Size = new Size(225, 29); - buttonCompanyCreate.TabIndex = 8; - buttonCompanyCreate.Text = "Создать компанию"; - buttonCompanyCreate.UseVisualStyleBackColor = true; - buttonCompanyCreate.Click += buttonCompanyCreate_Click; - // - // panelCollection - // - panelCollection.Controls.Add(buttonCollectionDelete); - panelCollection.Controls.Add(listBoxCollection); - panelCollection.Controls.Add(buttonCollectionAdd); - panelCollection.Controls.Add(radioButtonList); - panelCollection.Controls.Add(radioButtonArray); - panelCollection.Controls.Add(textBoxCollectionName); - panelCollection.Controls.Add(labelCollectionName); - panelCollection.Location = new Point(6, 26); - panelCollection.Name = "panelCollection"; - panelCollection.Size = new Size(225, 298); - panelCollection.TabIndex = 7; - // - // buttonCollectionDelete - // - buttonCollectionDelete.Location = new Point(3, 265); - buttonCollectionDelete.Name = "buttonCollectionDelete"; - buttonCollectionDelete.Size = new Size(219, 29); - buttonCollectionDelete.TabIndex = 6; - buttonCollectionDelete.Text = "Удалить коллекцию"; - buttonCollectionDelete.UseVisualStyleBackColor = true; - buttonCollectionDelete.Click += buttonCollectionDelete_Click; - // - // listBoxCollection - // - listBoxCollection.FormattingEnabled = true; - listBoxCollection.Location = new Point(3, 155); - listBoxCollection.Name = "listBoxCollection"; - listBoxCollection.Size = new Size(216, 104); - listBoxCollection.TabIndex = 5; - // - // buttonCollectionAdd - // - buttonCollectionAdd.Location = new Point(3, 114); - buttonCollectionAdd.Name = "buttonCollectionAdd"; - buttonCollectionAdd.Size = new Size(219, 29); - buttonCollectionAdd.TabIndex = 4; - buttonCollectionAdd.Text = "Добавить коллекцию"; - buttonCollectionAdd.UseVisualStyleBackColor = true; - buttonCollectionAdd.Click += buttonCollectionAdd_Click; - // - // radioButtonList - // - radioButtonList.AutoSize = true; - radioButtonList.Location = new Point(91, 76); - radioButtonList.Name = "radioButtonList"; - radioButtonList.Size = new Size(80, 24); - radioButtonList.TabIndex = 3; - radioButtonList.TabStop = true; - radioButtonList.Text = "Список"; - radioButtonList.UseVisualStyleBackColor = true; - // - // radioButtonArray - // - radioButtonArray.AutoSize = true; - radioButtonArray.Location = new Point(3, 76); - radioButtonArray.Name = "radioButtonArray"; - radioButtonArray.Size = new Size(82, 24); - radioButtonArray.TabIndex = 2; - radioButtonArray.TabStop = true; - radioButtonArray.Text = "Массив"; - radioButtonArray.UseVisualStyleBackColor = true; - // - // textBoxCollectionName - // - textBoxCollectionName.Location = new Point(3, 37); - textBoxCollectionName.Name = "textBoxCollectionName"; - textBoxCollectionName.Size = new Size(219, 27); - textBoxCollectionName.TabIndex = 1; - // - // labelCollectionName - // - labelCollectionName.AutoSize = true; - labelCollectionName.Location = new Point(36, 5); - labelCollectionName.Name = "labelCollectionName"; - labelCollectionName.Size = new Size(158, 20); - labelCollectionName.TabIndex = 0; - labelCollectionName.Text = "Название коллекции:"; - // // UpdateButton // - UpdateButton.Location = new Point(3, 202); + UpdateButton.Location = new Point(6, 565); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(222, 38); + UpdateButton.Size = new Size(225, 38); UpdateButton.TabIndex = 6; UpdateButton.Text = "Обновить"; UpdateButton.UseVisualStyleBackColor = true; @@ -169,9 +70,9 @@ // // GoToTestButton // - GoToTestButton.Location = new Point(3, 158); + GoToTestButton.Location = new Point(12, 355); GoToTestButton.Name = "GoToTestButton"; - GoToTestButton.Size = new Size(222, 38); + GoToTestButton.Size = new Size(225, 38); GoToTestButton.TabIndex = 5; GoToTestButton.Text = "Передать на тесты"; GoToTestButton.UseVisualStyleBackColor = true; @@ -179,9 +80,9 @@ // // DeleteButton // - DeleteButton.Location = new Point(3, 113); + DeleteButton.Location = new Point(6, 238); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(222, 38); + DeleteButton.Size = new Size(225, 38); DeleteButton.TabIndex = 4; DeleteButton.Text = "Удалить лодку"; DeleteButton.UseVisualStyleBackColor = true; @@ -189,18 +90,18 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(3, 80); + maskedTextBox.Location = new Point(6, 205); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(222, 27); + maskedTextBox.Size = new Size(225, 27); maskedTextBox.TabIndex = 3; maskedTextBox.ValidatingType = typeof(int); // // AddCatamaranButton // - AddCatamaranButton.Location = new Point(3, 41); + AddCatamaranButton.Location = new Point(6, 104); AddCatamaranButton.Name = "AddCatamaranButton"; - AddCatamaranButton.Size = new Size(222, 33); + AddCatamaranButton.Size = new Size(225, 38); AddCatamaranButton.TabIndex = 2; AddCatamaranButton.Text = "Добавить катамаран"; AddCatamaranButton.UseVisualStyleBackColor = true; @@ -208,26 +109,26 @@ // // AddBoatButton // - AddBoatButton.Location = new Point(3, 3); + AddBoatButton.Location = new Point(6, 60); AddBoatButton.Name = "AddBoatButton"; - AddBoatButton.Size = new Size(222, 32); + AddBoatButton.Size = new Size(225, 38); AddBoatButton.TabIndex = 1; AddBoatButton.Text = "Добавить лодку"; AddBoatButton.UseVisualStyleBackColor = true; AddBoatButton.Click += AddBoatButton_Click; // - // comboBoxSelectorCompany + // InstrumentBox // - comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - comboBoxSelectorCompany.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" }); - comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxSelectorCompany.FormattingEnabled = true; - comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 330); - comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; - comboBoxSelectorCompany.Size = new Size(225, 28); - comboBoxSelectorCompany.TabIndex = 0; - comboBoxSelectorCompany.SelectedIndexChanged += comboBoxSelectorCompany_SelectedIndexChanged; + InstrumentBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + InstrumentBox.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" }); + InstrumentBox.DropDownStyle = ComboBoxStyle.DropDownList; + InstrumentBox.FormattingEnabled = true; + InstrumentBox.Items.AddRange(new object[] { "Хранилище" }); + InstrumentBox.Location = new Point(6, 26); + InstrumentBox.Name = "InstrumentBox"; + InstrumentBox.Size = new Size(225, 28); + InstrumentBox.TabIndex = 0; + InstrumentBox.SelectedIndexChanged += InstrumentBox_SelectedIndexChanged; // // pictureBox // @@ -238,21 +139,6 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // - // panelCompanyTools - // - panelCompanyTools.Controls.Add(AddCatamaranButton); - panelCompanyTools.Controls.Add(AddBoatButton); - panelCompanyTools.Controls.Add(UpdateButton); - panelCompanyTools.Controls.Add(maskedTextBox); - panelCompanyTools.Controls.Add(GoToTestButton); - panelCompanyTools.Controls.Add(DeleteButton); - panelCompanyTools.Dock = DockStyle.Bottom; - panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 398); - panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(231, 241); - panelCompanyTools.TabIndex = 9; - // // FormBoatColletion // AutoScaleDimensions = new SizeF(8F, 20F); @@ -263,18 +149,15 @@ Name = "FormBoatColletion"; Text = "Коллекция лодок"; groupBox1.ResumeLayout(false); - panelCollection.ResumeLayout(false); - panelCollection.PerformLayout(); + groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); - panelCompanyTools.ResumeLayout(false); - panelCompanyTools.PerformLayout(); ResumeLayout(false); } #endregion private GroupBox groupBox1; - private ComboBox comboBoxSelectorCompany; + private ComboBox InstrumentBox; private Button AddBoatButton; private Button AddCatamaranButton; private PictureBox pictureBox; @@ -282,15 +165,5 @@ private Button DeleteButton; private Button GoToTestButton; private Button UpdateButton; - private Panel panelCollection; - private RadioButton radioButtonList; - private RadioButton radioButtonArray; - private TextBox textBoxCollectionName; - private Label labelCollectionName; - private Button buttonCompanyCreate; - private Button buttonCollectionDelete; - private ListBox listBoxCollection; - private Button buttonCollectionAdd; - private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/Catamaran/Catamaran/FormBoatColletion.cs b/Catamaran/Catamaran/FormBoatColletion.cs index 2693c35..ff418d8 100644 --- a/Catamaran/Catamaran/FormBoatColletion.cs +++ b/Catamaran/Catamaran/FormBoatColletion.cs @@ -17,17 +17,21 @@ namespace Catamaran private AbstractCompany? _company = null; - private readonly StorageCollection _storageCollection; + public FormBoatColletion() { InitializeComponent(); - _storageCollection = new(); } - private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + private void InstrumentBox_SelectedIndexChanged(object sender, EventArgs e) { - panelCompanyTools.Enabled = false; + switch (InstrumentBox.Text) + { + case "Хранилище": + _company = new Harbor(pictureBox.Width, pictureBox.Height, new ArrayGenericObjects()); + break; + } } private void CreateObject(string type) @@ -129,10 +133,10 @@ namespace Catamaran DrawingBoat? boat = null; int counter = 100; - while (boat == null) - { + while (boat == null) + { boat = _company.GetRandomObject(); - + counter--; if (counter <= 0) break; } @@ -148,83 +152,5 @@ namespace Catamaran }; form.ShowDialog(); } - - private void RefreshListBoxItems() - { - listBoxCollection.Items.Clear(); - for (int i = 0; i < _storageCollection.Keys?.Count; ++i) - { - string? colName = _storageCollection.Keys?[i]; - if (!string.IsNullOrEmpty(colName) ) - { - listBoxCollection.Items.Add(colName); - } - } - } - - private void buttonCollectionAdd_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonArray.Checked && !radioButtonList.Checked)) - { - MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - CollectionType collectionType = CollectionType.None; - if (radioButtonArray.Checked) - { - collectionType = CollectionType.Array; - } - else if (radioButtonList.Checked) - { - collectionType = CollectionType.List; - } - - _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); - RefreshListBoxItems(); - } - - private void buttonCollectionDelete_Click(object sender, EventArgs e) - { - if (listBoxCollection.SelectedItem == null || listBoxCollection.SelectedIndex < 0) - { - MessageBox.Show("Не выбрана коллекция"); - return; - } - if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - { - return; - } - _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); - RefreshListBoxItems(); - } - - private void buttonCompanyCreate_Click(object sender, EventArgs e) - { - if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) - { - MessageBox.Show("Коллекция не выбрана"); - return; - } - - ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; - if (collection == null) - { - MessageBox.Show("Коллекция не инициализирована"); - return; - } - - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new Harbor(pictureBox.Width, pictureBox.Height, collection); - break; - } - - panelCompanyTools.Enabled = true; - RefreshListBoxItems(); - } - - } } -- 2.25.1 From f3dc0a4866dbe314a88eb68461215e2d28866e78 Mon Sep 17 00:00:00 2001 From: strwbrry1 Date: Wed, 3 Apr 2024 15:26:14 +0400 Subject: [PATCH 3/4] Reapply "Lab 4 done" This reverts commit fdf88350d6ab6469f9e7c4656dcc81261633074a. --- .../ArrayGenericObjects.cs | 8 +- .../CollectionType.cs | 17 ++ .../ListGenericObjects.cs | 66 ++++++ .../StorageCollection.cs | 62 ++++++ .../Catamaran/FormBoatColletion.Designer.cs | 193 +++++++++++++++--- Catamaran/Catamaran/FormBoatColletion.cs | 96 ++++++++- 6 files changed, 396 insertions(+), 46 deletions(-) create mode 100644 Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs create mode 100644 Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs index 8131fd0..305a8fb 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs @@ -25,7 +25,7 @@ namespace Catamaran.CollectionGenericObjects } else { - _collection = new T[value]; + _collection = new T?[value]; } } } @@ -38,7 +38,11 @@ namespace Catamaran.CollectionGenericObjects public T? Get(int position) { - return _collection[position]; + if (position >= 0 && position < Count) + { + return _collection[position]; + } + return null; } public int Insert(T obj) diff --git a/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs b/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..3136a70 --- /dev/null +++ b/Catamaran/Catamaran/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.CollectionGenericObjects +{ + public enum CollectionType + { + None = 0, + + Array = 1, + + List = 2 + } +} diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..8a537b6 --- /dev/null +++ b/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.CollectionGenericObjects +{ + public class ListGenericObjects : ICollectionGenericObjects + where T : class + { + + private readonly List _collection; + + private int _maxCount; + + public int Count => _collection.Count; + + public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + + public ListGenericObjects() + { + _collection = new(); + } + + public T? Get(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + if (Count + 1 > _maxCount) + { + return -1; + } + _collection.Add(obj); + return 1; + } + + public int Insert(T obj, int position) + { + if (position < 0 || position > Count || Count + 1 > _maxCount) + { + return -1; + } + _collection.Insert(position, obj); + return 1; + } + + public T? Remove(int position) + { + if (position < 0 || position > Count) + { + return null; + } + T? obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + } + } +} diff --git a/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs b/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..3108130 --- /dev/null +++ b/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,62 @@ +using Catamaran.Drawings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.CollectionGenericObjects +{ + public class StorageCollection + where T : class + { + readonly Dictionary> _storages; + + public List Keys => _storages.Keys.ToList(); + + public StorageCollection() + { + _storages = new Dictionary>(); + } + + public void AddCollection(string name, CollectionType collectionType) + { + if (!string.IsNullOrEmpty(name) && !_storages.ContainsKey(name)) + { + switch (collectionType) + { + case CollectionType.None: + return; + case CollectionType.Array: + _storages[name] = new ArrayGenericObjects(); + return; + case CollectionType.List: + _storages[name] = new ListGenericObjects(); + return; + } + + } + } + + public void DelCollection(string name) + { + if (string.IsNullOrEmpty(name) || !_storages.ContainsKey(name)) + { + return; + } + _storages.Remove(name); + } + + public ICollectionGenericObjects? this[string name] + { + get + { + if (!_storages.ContainsKey(name) || string.IsNullOrEmpty(name)) + { + return null; + } + return _storages[name]; + } + } + } +} diff --git a/Catamaran/Catamaran/FormBoatColletion.Designer.cs b/Catamaran/Catamaran/FormBoatColletion.Designer.cs index a1e3696..aa2c822 100644 --- a/Catamaran/Catamaran/FormBoatColletion.Designer.cs +++ b/Catamaran/Catamaran/FormBoatColletion.Designer.cs @@ -29,27 +29,36 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + buttonCompanyCreate = new Button(); + panelCollection = new Panel(); + buttonCollectionDelete = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonArray = new RadioButton(); + textBoxCollectionName = new TextBox(); + labelCollectionName = new Label(); UpdateButton = new Button(); GoToTestButton = new Button(); DeleteButton = new Button(); maskedTextBox = new MaskedTextBox(); AddCatamaranButton = new Button(); AddBoatButton = new Button(); - InstrumentBox = new ComboBox(); + comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + panelCompanyTools = new Panel(); groupBox1.SuspendLayout(); + panelCollection.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + panelCompanyTools.SuspendLayout(); SuspendLayout(); // // groupBox1 // - groupBox1.Controls.Add(UpdateButton); - groupBox1.Controls.Add(GoToTestButton); - groupBox1.Controls.Add(DeleteButton); - groupBox1.Controls.Add(maskedTextBox); - groupBox1.Controls.Add(AddCatamaranButton); - groupBox1.Controls.Add(AddBoatButton); - groupBox1.Controls.Add(InstrumentBox); + groupBox1.Controls.Add(panelCompanyTools); + groupBox1.Controls.Add(buttonCompanyCreate); + groupBox1.Controls.Add(panelCollection); + groupBox1.Controls.Add(comboBoxSelectorCompany); groupBox1.Dock = DockStyle.Right; groupBox1.Location = new Point(809, 0); groupBox1.Name = "groupBox1"; @@ -58,11 +67,101 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // buttonCompanyCreate + // + buttonCompanyCreate.Location = new Point(6, 366); + buttonCompanyCreate.Name = "buttonCompanyCreate"; + buttonCompanyCreate.Size = new Size(225, 29); + buttonCompanyCreate.TabIndex = 8; + buttonCompanyCreate.Text = "Создать компанию"; + buttonCompanyCreate.UseVisualStyleBackColor = true; + buttonCompanyCreate.Click += buttonCompanyCreate_Click; + // + // panelCollection + // + panelCollection.Controls.Add(buttonCollectionDelete); + panelCollection.Controls.Add(listBoxCollection); + panelCollection.Controls.Add(buttonCollectionAdd); + panelCollection.Controls.Add(radioButtonList); + panelCollection.Controls.Add(radioButtonArray); + panelCollection.Controls.Add(textBoxCollectionName); + panelCollection.Controls.Add(labelCollectionName); + panelCollection.Location = new Point(6, 26); + panelCollection.Name = "panelCollection"; + panelCollection.Size = new Size(225, 298); + panelCollection.TabIndex = 7; + // + // buttonCollectionDelete + // + buttonCollectionDelete.Location = new Point(3, 265); + buttonCollectionDelete.Name = "buttonCollectionDelete"; + buttonCollectionDelete.Size = new Size(219, 29); + buttonCollectionDelete.TabIndex = 6; + buttonCollectionDelete.Text = "Удалить коллекцию"; + buttonCollectionDelete.UseVisualStyleBackColor = true; + buttonCollectionDelete.Click += buttonCollectionDelete_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.Location = new Point(3, 155); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(216, 104); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 114); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(219, 29); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(91, 76); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(80, 24); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonArray + // + radioButtonArray.AutoSize = true; + radioButtonArray.Location = new Point(3, 76); + radioButtonArray.Name = "radioButtonArray"; + radioButtonArray.Size = new Size(82, 24); + radioButtonArray.TabIndex = 2; + radioButtonArray.TabStop = true; + radioButtonArray.Text = "Массив"; + radioButtonArray.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 37); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(219, 27); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(36, 5); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(158, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // UpdateButton // - UpdateButton.Location = new Point(6, 565); + UpdateButton.Location = new Point(3, 202); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(225, 38); + UpdateButton.Size = new Size(222, 38); UpdateButton.TabIndex = 6; UpdateButton.Text = "Обновить"; UpdateButton.UseVisualStyleBackColor = true; @@ -70,9 +169,9 @@ // // GoToTestButton // - GoToTestButton.Location = new Point(12, 355); + GoToTestButton.Location = new Point(3, 158); GoToTestButton.Name = "GoToTestButton"; - GoToTestButton.Size = new Size(225, 38); + GoToTestButton.Size = new Size(222, 38); GoToTestButton.TabIndex = 5; GoToTestButton.Text = "Передать на тесты"; GoToTestButton.UseVisualStyleBackColor = true; @@ -80,9 +179,9 @@ // // DeleteButton // - DeleteButton.Location = new Point(6, 238); + DeleteButton.Location = new Point(3, 113); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(225, 38); + DeleteButton.Size = new Size(222, 38); DeleteButton.TabIndex = 4; DeleteButton.Text = "Удалить лодку"; DeleteButton.UseVisualStyleBackColor = true; @@ -90,18 +189,18 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(6, 205); + maskedTextBox.Location = new Point(3, 80); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(225, 27); + maskedTextBox.Size = new Size(222, 27); maskedTextBox.TabIndex = 3; maskedTextBox.ValidatingType = typeof(int); // // AddCatamaranButton // - AddCatamaranButton.Location = new Point(6, 104); + AddCatamaranButton.Location = new Point(3, 41); AddCatamaranButton.Name = "AddCatamaranButton"; - AddCatamaranButton.Size = new Size(225, 38); + AddCatamaranButton.Size = new Size(222, 33); AddCatamaranButton.TabIndex = 2; AddCatamaranButton.Text = "Добавить катамаран"; AddCatamaranButton.UseVisualStyleBackColor = true; @@ -109,26 +208,26 @@ // // AddBoatButton // - AddBoatButton.Location = new Point(6, 60); + AddBoatButton.Location = new Point(3, 3); AddBoatButton.Name = "AddBoatButton"; - AddBoatButton.Size = new Size(225, 38); + AddBoatButton.Size = new Size(222, 32); AddBoatButton.TabIndex = 1; AddBoatButton.Text = "Добавить лодку"; AddBoatButton.UseVisualStyleBackColor = true; AddBoatButton.Click += AddBoatButton_Click; // - // InstrumentBox + // comboBoxSelectorCompany // - InstrumentBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - InstrumentBox.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" }); - InstrumentBox.DropDownStyle = ComboBoxStyle.DropDownList; - InstrumentBox.FormattingEnabled = true; - InstrumentBox.Items.AddRange(new object[] { "Хранилище" }); - InstrumentBox.Location = new Point(6, 26); - InstrumentBox.Name = "InstrumentBox"; - InstrumentBox.Size = new Size(225, 28); - InstrumentBox.TabIndex = 0; - InstrumentBox.SelectedIndexChanged += InstrumentBox_SelectedIndexChanged; + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" }); + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(6, 330); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(225, 28); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += comboBoxSelectorCompany_SelectedIndexChanged; // // pictureBox // @@ -139,6 +238,21 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(AddCatamaranButton); + panelCompanyTools.Controls.Add(AddBoatButton); + panelCompanyTools.Controls.Add(UpdateButton); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(GoToTestButton); + panelCompanyTools.Controls.Add(DeleteButton); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 398); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(231, 241); + panelCompanyTools.TabIndex = 9; + // // FormBoatColletion // AutoScaleDimensions = new SizeF(8F, 20F); @@ -149,15 +263,18 @@ Name = "FormBoatColletion"; Text = "Коллекция лодок"; groupBox1.ResumeLayout(false); - groupBox1.PerformLayout(); + panelCollection.ResumeLayout(false); + panelCollection.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } #endregion private GroupBox groupBox1; - private ComboBox InstrumentBox; + private ComboBox comboBoxSelectorCompany; private Button AddBoatButton; private Button AddCatamaranButton; private PictureBox pictureBox; @@ -165,5 +282,15 @@ private Button DeleteButton; private Button GoToTestButton; private Button UpdateButton; + private Panel panelCollection; + private RadioButton radioButtonList; + private RadioButton radioButtonArray; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private Button buttonCompanyCreate; + private Button buttonCollectionDelete; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/Catamaran/Catamaran/FormBoatColletion.cs b/Catamaran/Catamaran/FormBoatColletion.cs index ff418d8..2693c35 100644 --- a/Catamaran/Catamaran/FormBoatColletion.cs +++ b/Catamaran/Catamaran/FormBoatColletion.cs @@ -17,21 +17,17 @@ namespace Catamaran private AbstractCompany? _company = null; - + private readonly StorageCollection _storageCollection; public FormBoatColletion() { InitializeComponent(); + _storageCollection = new(); } - private void InstrumentBox_SelectedIndexChanged(object sender, EventArgs e) + private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (InstrumentBox.Text) - { - case "Хранилище": - _company = new Harbor(pictureBox.Width, pictureBox.Height, new ArrayGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } private void CreateObject(string type) @@ -133,10 +129,10 @@ namespace Catamaran DrawingBoat? boat = null; int counter = 100; - while (boat == null) - { + while (boat == null) + { boat = _company.GetRandomObject(); - + counter--; if (counter <= 0) break; } @@ -152,5 +148,83 @@ namespace Catamaran }; form.ShowDialog(); } + + private void RefreshListBoxItems() + { + listBoxCollection.Items.Clear(); + for (int i = 0; i < _storageCollection.Keys?.Count; ++i) + { + string? colName = _storageCollection.Keys?[i]; + if (!string.IsNullOrEmpty(colName) ) + { + listBoxCollection.Items.Add(colName); + } + } + } + + private void buttonCollectionAdd_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonArray.Checked && !radioButtonList.Checked)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + CollectionType collectionType = CollectionType.None; + if (radioButtonArray.Checked) + { + collectionType = CollectionType.Array; + } + else if (radioButtonList.Checked) + { + collectionType = CollectionType.List; + } + + _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + RefreshListBoxItems(); + } + + private void buttonCollectionDelete_Click(object sender, EventArgs e) + { + if (listBoxCollection.SelectedItem == null || listBoxCollection.SelectedIndex < 0) + { + MessageBox.Show("Не выбрана коллекция"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RefreshListBoxItems(); + } + + private void buttonCompanyCreate_Click(object sender, EventArgs e) + { + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + + ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; + if (collection == null) + { + MessageBox.Show("Коллекция не инициализирована"); + return; + } + + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new Harbor(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RefreshListBoxItems(); + } + + } } -- 2.25.1 From cbf8e6fb24a12a1ed8b35db6f414241b7e47df88 Mon Sep 17 00:00:00 2001 From: strwbrry1 Date: Wed, 3 Apr 2024 15:49:23 +0400 Subject: [PATCH 4/4] cosmetic changes --- .../Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs | 1 - Catamaran/Catamaran/FormBoatColletion.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs index 305a8fb..71fc9b8 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs @@ -87,7 +87,6 @@ namespace Catamaran.CollectionGenericObjects { if (position > Count || position < 0) { - return null; } T? obj = _collection[position]; diff --git a/Catamaran/Catamaran/FormBoatColletion.cs b/Catamaran/Catamaran/FormBoatColletion.cs index 2693c35..0a87f4d 100644 --- a/Catamaran/Catamaran/FormBoatColletion.cs +++ b/Catamaran/Catamaran/FormBoatColletion.cs @@ -155,7 +155,7 @@ namespace Catamaran for (int i = 0; i < _storageCollection.Keys?.Count; ++i) { string? colName = _storageCollection.Keys?[i]; - if (!string.IsNullOrEmpty(colName) ) + if (!string.IsNullOrEmpty(colName)) { listBoxCollection.Items.Add(colName); } -- 2.25.1