Лабораторная работа №4

This commit is contained in:
Никита Шипилов 2024-04-15 00:56:51 +04:00
parent de3f45d42f
commit 1d9936c880
5 changed files with 353 additions and 25 deletions

View File

@ -0,0 +1,17 @@
namespace ProjectSeaplane.CollectionGenericObjects;
public enum CollectionType
{
/// <summary>
/// Неопределено
/// </summary>
None = 0,
/// <summary>
/// Массив
/// </summary>
Massive = 1,
/// <summary>
/// Список
/// </summary>
List = 2
}

View File

@ -0,0 +1,45 @@
namespace ProjectSeaplane.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 >= Count || position < 0) return null;
return _collection[position];
}
public int Insert(T obj)
{
if (Count == _maxCount) return -1;
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
{
if (Count == _maxCount) return -1;
if (position >= Count || position < 0) return -1;
_collection.Insert(position, obj);
return position;
}
public T Remove(int position)
{
if (position >= Count || position < 0) return null;
T obj = _collection[position];
_collection.RemoveAt(position);
return obj;
}
}

View File

@ -0,0 +1,63 @@
namespace ProjectSeaplane.CollectionGenericObjects;
public class StorageCollection<T>
where T : class
{
/// <summary>
/// Словарь (хранилище) с коллекциями
/// </summary>
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
/// <summary>
/// Возвращение списка названий коллекций
/// </summary>
public List<string> Keys => _storages.Keys.ToList();
/// <summary>
/// Конструктор
/// </summary>
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
}
/// <summary>
/// Добавление коллекции в хранилище
/// </summary>
/// <param name="name">Название коллекции</param>
/// <param name="collectionType">тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType)
{
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
// TODO Прописать логику для добавления
if (_storages.ContainsKey(name)) return;
if (collectionType == CollectionType.None) return;
else if (collectionType == CollectionType.Massive)
_storages[name] = new MassiveGenericObjects<T>();
else if (collectionType == CollectionType.List)
_storages[name] = new ListGenericObjects<T>();
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
// TODO Прописать логику для удаления коллекции
if (_storages.ContainsKey(name))
_storages.Remove(name);
}
/// <summary>
/// Доступ к коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
/// <returns></returns>
public ICollectionGenericObjects<T>? this[string name]
{
get
{
// TODO Продумать логику получения объекта
if (_storages.ContainsKey(name))
return _storages[name];
return null;
}
}
}

View File

@ -29,6 +29,15 @@
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
buttonCreateCompany = new Button();
panelStorage = new Panel();
buttonCollectionDel = new Button();
listBoxCollection = new ListBox();
buttonCollectionAdd = new Button();
radioButtonList = new RadioButton();
radioButtonMassive = new RadioButton();
textBoxCollectionName = new TextBox();
labelCollectionName = new Label();
buttonRefresh = new Button();
buttonGoToCheck = new Button();
buttonDelPlane = new Button();
@ -37,30 +46,122 @@
buttonAddPlane = new Button();
comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox();
panelCompanyTools = new Panel();
groupBoxTools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
panelCompanyTools.SuspendLayout();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonDelPlane);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddSeaplane);
groupBoxTools.Controls.Add(buttonAddPlane);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(655, 0);
groupBoxTools.Location = new Point(764, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(200, 565);
groupBoxTools.Size = new Size(200, 627);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 300);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(182, 23);
buttonCreateCompany.TabIndex = 7;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
buttonCreateCompany.Click += buttonCreateCompany_Click;
//
// panelStorage
//
panelStorage.Controls.Add(buttonCollectionDel);
panelStorage.Controls.Add(listBoxCollection);
panelStorage.Controls.Add(buttonCollectionAdd);
panelStorage.Controls.Add(radioButtonList);
panelStorage.Controls.Add(radioButtonMassive);
panelStorage.Controls.Add(textBoxCollectionName);
panelStorage.Controls.Add(labelCollectionName);
panelStorage.Dock = DockStyle.Top;
panelStorage.Location = new Point(3, 19);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(194, 246);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 212);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(188, 23);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += buttonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(3, 112);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(188, 94);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 83);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(188, 23);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += buttonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(90, 58);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(66, 19);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(17, 58);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(67, 19);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 29);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(188, 23);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(38, 11);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(125, 15);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// buttonRefresh
//
buttonRefresh.Location = new Point(6, 469);
buttonRefresh.Location = new Point(3, 216);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(182, 41);
buttonRefresh.TabIndex = 6;
@ -70,7 +171,7 @@
//
// buttonGoToCheck
//
buttonGoToCheck.Location = new Point(6, 322);
buttonGoToCheck.Location = new Point(3, 173);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(182, 41);
buttonGoToCheck.TabIndex = 5;
@ -80,7 +181,7 @@
//
// buttonDelPlane
//
buttonDelPlane.Location = new Point(6, 235);
buttonDelPlane.Location = new Point(3, 126);
buttonDelPlane.Name = "buttonDelPlane";
buttonDelPlane.Size = new Size(182, 41);
buttonDelPlane.TabIndex = 4;
@ -90,7 +191,7 @@
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(6, 206);
maskedTextBoxPosition.Location = new Point(3, 97);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(182, 23);
@ -99,7 +200,7 @@
//
// buttonAddSeaplane
//
buttonAddSeaplane.Location = new Point(6, 123);
buttonAddSeaplane.Location = new Point(3, 50);
buttonAddSeaplane.Name = "buttonAddSeaplane";
buttonAddSeaplane.Size = new Size(182, 41);
buttonAddSeaplane.TabIndex = 2;
@ -109,7 +210,7 @@
//
// buttonAddPlane
//
buttonAddPlane.Location = new Point(6, 76);
buttonAddPlane.Location = new Point(3, 3);
buttonAddPlane.Name = "buttonAddPlane";
buttonAddPlane.Size = new Size(182, 41);
buttonAddPlane.TabIndex = 1;
@ -123,7 +224,7 @@
comboBoxSelectorCompany.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" });
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(6, 22);
comboBoxSelectorCompany.Location = new Point(6, 271);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(182, 23);
comboBoxSelectorCompany.TabIndex = 0;
@ -134,22 +235,39 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(655, 565);
pictureBox.Size = new Size(764, 627);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddPlane);
panelCompanyTools.Controls.Add(buttonAddSeaplane);
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonDelPlane);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(6, 346);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(188, 269);
panelCompanyTools.TabIndex = 8;
//
// FormSeaplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(855, 565);
ClientSize = new Size(964, 627);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormSeaplaneCollection";
Text = "FormSeaplaneCollection";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
ResumeLayout(false);
}
@ -164,5 +282,15 @@
private MaskedTextBox maskedTextBoxPosition;
private Button buttonAddSeaplane;
private PictureBox pictureBox;
private Panel panelStorage;
private Label labelCollectionName;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
}
}

View File

@ -6,21 +6,19 @@ namespace ProjectSeaplane;
public partial class FormSeaplaneCollection : Form
{
private readonly StorageCollection<DrawingPlane> _storageCollection;
private AbstractCompany? _company = null;
public FormSeaplaneCollection()
{
InitializeComponent();
_storageCollection = new();
}
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawingPlane>());
break;
}
panelCompanyTools.Enabled = false;
}
private void ButtonAddPlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingPlane));
@ -137,4 +135,81 @@ public partial class FormSeaplaneCollection : Form
pictureBox.Image = _company.Show();
}
private void buttonCollectionAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CollectionType collectionType = CollectionType.None;
if (radioButtonMassive.Checked)
{
collectionType = CollectionType.Massive;
}
else if (radioButtonList.Checked)
{
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
RerfreshListBoxItems();
}
private void buttonCollectionDel_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RerfreshListBoxItems();
}
private void RerfreshListBoxItems()
{
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 buttonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawingPlane>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
}