4 лабораторная работа

This commit is contained in:
Arslan 2024-04-01 02:04:09 +04:00
parent 465d3d20ca
commit 8aa82bb587
5 changed files with 441 additions and 66 deletions

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectFighterJet.CollectionGenericObjects;
/// <summary>
/// Тип коллекции
/// </summary>
public enum CollectionType
{
/// <summary>
/// Неопределено
/// </summary>
None = 0,
/// <summary>
/// Массив
/// </summary>
Massive = 1,
/// <summary>
/// Список
/// </summary>
List = 2
}

View File

@ -0,0 +1,71 @@
using ProjectFighterJet.CollectionGenericObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectFighterJet.CollectionGenericObjects;
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
{
/// <summary>
/// Список объектов, которые храним
/// </summary>
private readonly List<T?> _collection;
/// <summary>
/// Максимально допустимое число объектов в списке
/// </summary>
private int _maxCount;
public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
/// <summary>
/// Конструктор
/// </summary>
public ListGenericObjects()
{
_collection = new();
}
public T? Get(int position)
{
if (position >= 0 && position < Count)
{
return _collection[position];
}
return null;
}
public int Insert(T obj)
{
if (Count <= _maxCount)
{
_collection.Add(obj);
return Count;
}
return -1;
}
public int Insert(T obj, int position)
{
if (Count < _maxCount && position >= 0 && position < _maxCount)
{
_collection.Insert(position, obj);
return position;
}
return -1;
}
public T Remove(int position)
{
T temp = _collection[position];
if (position >= 0 && position < _maxCount)
{
_collection.RemoveAt(position);
return temp;
}
return null;
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectFighterJet.CollectionGenericObjects;
/// <summary>
/// Класс-хранилище коллекций
/// </summary>
/// <typeparam name="T"></typeparam>
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)
{
if (!(collectionType == CollectionType.None) && !_storages.ContainsKey(name))
{
if (collectionType == CollectionType.List)
{
_storages.Add(name, new ListGenericObjects<T>());
}
else if (collectionType == CollectionType.Massive)
{
_storages.Add(name, new MassiveGenericObjects<T>());
}
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
if (_storages.ContainsKey(name)) { _storages.Remove(name); }
}
/// <summary>
/// Доступ к коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
/// <returns></returns>
public ICollectionGenericObjects<T>? this[string name]
{
get
{
if (_storages.ContainsKey(name))
{
return _storages[name];
}
return null;
}
}
}

View File

@ -29,26 +29,35 @@
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
buttonGoToCheck = new Button();
buttonRefresh = new Button();
buttonRemoveJet = new Button();
maskedTextBoxPosition = new MaskedTextBox();
buttonAddFighterJet = new Button();
panelCompanyTools = new Panel();
buttonAddJet = new Button();
buttonAddFighterJet = new Button();
maskedTextBoxPosition = new MaskedTextBox();
buttonGoToCheck = new Button();
buttonRemoveJet = new Button();
buttonRefresh = new Button();
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();
comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox();
groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonRemoveJet);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddFighterJet);
groupBoxTools.Controls.Add(buttonAddJet);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(911, 0);
@ -58,69 +67,176 @@
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты ";
//
// buttonGoToCheck
// panelCompanyTools
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 433);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(196, 48);
buttonGoToCheck.TabIndex = 6;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click;
panelCompanyTools.Controls.Add(buttonAddJet);
panelCompanyTools.Controls.Add(buttonAddFighterJet);
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonRemoveJet);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(9, 378);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(199, 287);
panelCompanyTools.TabIndex = 8;
//
// buttonRefresh
// buttonAddJet
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(6, 589);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(196, 48);
buttonRefresh.TabIndex = 5;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += buttonRefresh_Click;
buttonAddJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddJet.Location = new Point(3, 3);
buttonAddJet.Name = "buttonAddJet";
buttonAddJet.Size = new Size(190, 48);
buttonAddJet.TabIndex = 1;
buttonAddJet.Text = "Добавление самолёта";
buttonAddJet.UseVisualStyleBackColor = true;
buttonAddJet.Click += buttonAddJet_Click;
//
// buttonRemoveJet
// buttonAddFighterJet
//
buttonRemoveJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveJet.Location = new Point(6, 304);
buttonRemoveJet.Name = "buttonRemoveJet";
buttonRemoveJet.Size = new Size(196, 48);
buttonRemoveJet.TabIndex = 4;
buttonRemoveJet.Text = "Удалить самолёт";
buttonRemoveJet.UseVisualStyleBackColor = true;
buttonRemoveJet.Click += buttonRemoveJet_Click;
buttonAddFighterJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddFighterJet.Location = new Point(3, 57);
buttonAddFighterJet.Name = "buttonAddFighterJet";
buttonAddFighterJet.Size = new Size(190, 48);
buttonAddFighterJet.TabIndex = 2;
buttonAddFighterJet.Text = "Добавление истрибителя";
buttonAddFighterJet.UseVisualStyleBackColor = true;
buttonAddFighterJet.Click += buttonAddFighterJet_Click;
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(6, 271);
maskedTextBoxPosition.Location = new Point(3, 111);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(202, 27);
maskedTextBoxPosition.TabIndex = 3;
maskedTextBoxPosition.ValidatingType = typeof(int);
//
// buttonAddFighterJet
// buttonGoToCheck
//
buttonAddFighterJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddFighterJet.Location = new Point(6, 179);
buttonAddFighterJet.Name = "buttonAddFighterJet";
buttonAddFighterJet.Size = new Size(196, 48);
buttonAddFighterJet.TabIndex = 2;
buttonAddFighterJet.Text = "Добавление истрибителя";
buttonAddFighterJet.UseVisualStyleBackColor = true;
buttonAddFighterJet.Click += buttonAddFighterJet_Click;
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(3, 182);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(190, 33);
buttonGoToCheck.TabIndex = 6;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click;
//
// buttonAddJet
// buttonRemoveJet
//
buttonAddJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddJet.Location = new Point(6, 125);
buttonAddJet.Name = "buttonAddJet";
buttonAddJet.Size = new Size(196, 48);
buttonAddJet.TabIndex = 1;
buttonAddJet.Text = "Добавление самолёта";
buttonAddJet.UseVisualStyleBackColor = true;
buttonAddJet.Click += buttonAddJet_Click;
buttonRemoveJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveJet.Location = new Point(3, 144);
buttonRemoveJet.Name = "buttonRemoveJet";
buttonRemoveJet.Size = new Size(190, 32);
buttonRemoveJet.TabIndex = 4;
buttonRemoveJet.Text = "Удалить самолёт";
buttonRemoveJet.UseVisualStyleBackColor = true;
buttonRemoveJet.Click += buttonRemoveJet_Click;
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(3, 221);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(190, 33);
buttonRefresh.TabIndex = 5;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += buttonRefresh_Click;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 343);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(196, 29);
buttonCreateCompany.TabIndex = 7;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
buttonCreateCompany.Click += buttonCreateCompany_Click;
//
// panelStorage
//
panelStorage.BackColor = SystemColors.Control;
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, 23);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(202, 280);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 248);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(196, 29);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += buttonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 20;
listBoxCollection.Location = new Point(3, 138);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(196, 104);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 103);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(196, 29);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += buttonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(113, 73);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(80, 24);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(6, 73);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(82, 24);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив\r\n";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 35);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(196, 27);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(22, 12);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(155, 20);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции";
//
// comboBoxSelectorCompany
//
@ -129,7 +245,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(6, 26);
comboBoxSelectorCompany.Location = new Point(6, 309);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(196, 28);
comboBoxSelectorCompany.TabIndex = 0;
@ -154,7 +270,10 @@
Name = "FormJetCollection";
Text = "Коллекция самолётов";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
ResumeLayout(false);
}
@ -170,5 +289,15 @@
private Button buttonRefresh;
private Button buttonRemoveJet;
private Button buttonGoToCheck;
private Panel panelStorage;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Label labelCollectionName;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
}
}

View File

@ -14,6 +14,11 @@ namespace ProjectFighterJet;
public partial class FormJetCollection : Form
{
/// <summary>
/// Хранилище коллекций
/// </summary>
private readonly StorageCollection<DrawningJet> _storageCollection;
/// <summary>
/// Компания
/// </summary>
@ -24,6 +29,7 @@ public partial class FormJetCollection : Form
public FormJetCollection()
{
InitializeComponent();
_storageCollection = new();
}
/// <summary>
/// Выбор компании
@ -33,13 +39,7 @@ public partial class FormJetCollection : Form
private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new FighterJetSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningJet>());
break;
}
panelCompanyTools.Enabled = false;
}
/// <summary>
/// Создание объекта класса-перемещения
@ -176,4 +176,79 @@ public partial class FormJetCollection : 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)
{
// нужно убедиться, что есть выбранная коллекция
// спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись
// удалить и обновить ListBox
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<DrawningJet>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new FighterJetSharingService(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
}