Reapply "Lab 4 done"

This reverts commit fdf88350d6.
This commit is contained in:
strwbrry1 2024-04-03 15:26:14 +04:00
parent fdf88350d6
commit f3dc0a4866
6 changed files with 396 additions and 46 deletions

View File

@ -25,7 +25,7 @@ namespace Catamaran.CollectionGenericObjects
}
else
{
_collection = new T[value];
_collection = new T?[value];
}
}
}
@ -37,9 +37,13 @@ namespace Catamaran.CollectionGenericObjects
}
public T? Get(int position)
{
if (position >= 0 && position < Count)
{
return _collection[position];
}
return null;
}
public int Insert(T obj)
{

View File

@ -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
}
}

View File

@ -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<T> : ICollectionGenericObjects<T>
where T : class
{
private readonly List<T?> _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;
}
}
}

View File

@ -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<T>
where T : class
{
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
public List<string> Keys => _storages.Keys.ToList();
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
}
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<T>();
return;
case CollectionType.List:
_storages[name] = new ListGenericObjects<T>();
return;
}
}
}
public void DelCollection(string name)
{
if (string.IsNullOrEmpty(name) || !_storages.ContainsKey(name))
{
return;
}
_storages.Remove(name);
}
public ICollectionGenericObjects<T>? this[string name]
{
get
{
if (!_storages.ContainsKey(name) || string.IsNullOrEmpty(name))
{
return null;
}
return _storages[name];
}
}
}
}

View File

@ -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;
}
}

View File

@ -17,21 +17,17 @@ namespace Catamaran
private AbstractCompany? _company = null;
private readonly StorageCollection<DrawingBoat> _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<DrawingBoat>());
break;
}
panelCompanyTools.Enabled = false;
}
private void CreateObject(string type)
@ -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<DrawingBoat>? 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();
}
}
}