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

This commit is contained in:
Павел Ладягин 2024-03-28 18:07:22 +04:00
parent a12a340c89
commit 7d44b74eba
5 changed files with 449 additions and 65 deletions

View File

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

View File

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

View File

@ -0,0 +1,77 @@
namespace ProjectAirplaneWithRadar.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 (name == null || _storages.ContainsKey(name))
return;
switch (collectionType)
{
case CollectionType.None:
return;
case CollectionType.Massive:
_storages[name] = new MassiveGenericObjects<T>();
return;
case CollectionType.List:
_storages[name] = new ListGenericObjects<T>();
return;
}
}
/// <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 (name == null || !_storages.ContainsKey(name))
return null;
return _storages[name];
}
}
}
}

View File

@ -29,26 +29,35 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
buttonRefresh = new Button(); panelCompanyTools = new Panel();
buttonGoToCheck = new Button();
buttonRemoveAirplane = new Button();
maskedTextBoxPosition = new MaskedTextBox();
buttonAddAirplaneWithRadar = new Button();
buttonAddAirplane = new Button(); buttonAddAirplane = new Button();
buttonAddAirplaneWithRadar = new Button();
maskedTextBoxPosition = new MaskedTextBox();
buttonRefresh = new Button();
buttonRemoveAirplane = new Button();
buttonGoToCheck = 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(); comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox(); pictureBox = new PictureBox();
groupBoxTools.SuspendLayout(); groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// groupBoxTools // groupBoxTools
// //
groupBoxTools.Controls.Add(buttonRefresh); groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonGoToCheck); groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(buttonRemoveAirplane); groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddAirplaneWithRadar);
groupBoxTools.Controls.Add(buttonAddAirplane);
groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(883, 0); groupBoxTools.Location = new Point(883, 0);
@ -58,69 +67,176 @@
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты"; groupBoxTools.Text = "Инструменты";
// //
// buttonRefresh // panelCompanyTools
// //
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; panelCompanyTools.Controls.Add(buttonAddAirplane);
buttonRefresh.Location = new Point(109, 513); panelCompanyTools.Controls.Add(buttonAddAirplaneWithRadar);
buttonRefresh.Name = "buttonRefresh"; panelCompanyTools.Controls.Add(maskedTextBoxPosition);
buttonRefresh.Size = new Size(91, 56); panelCompanyTools.Controls.Add(buttonRefresh);
buttonRefresh.TabIndex = 6; panelCompanyTools.Controls.Add(buttonRemoveAirplane);
buttonRefresh.Text = "Обновить"; panelCompanyTools.Controls.Add(buttonGoToCheck);
buttonRefresh.UseVisualStyleBackColor = true; panelCompanyTools.Dock = DockStyle.Bottom;
buttonRefresh.Click += ButtonRefresh_Click; panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 417);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(200, 187);
panelCompanyTools.TabIndex = 8;
// //
// buttonGoToCheck // buttonAddAirplane
// //
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonAddAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 513); buttonAddAirplane.Location = new Point(0, 3);
buttonGoToCheck.Name = "buttonGoToCheck"; buttonAddAirplane.Name = "buttonAddAirplane";
buttonGoToCheck.Size = new Size(91, 56); buttonAddAirplane.Size = new Size(97, 56);
buttonGoToCheck.TabIndex = 5; buttonAddAirplane.TabIndex = 1;
buttonGoToCheck.Text = "Передать на тесты"; buttonAddAirplane.Text = "Добавить самолет";
buttonGoToCheck.UseVisualStyleBackColor = true; buttonAddAirplane.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click; buttonAddAirplane.Click += ButtonAddAirplane_Click;
// //
// buttonRemoveAirplane // buttonAddAirplaneWithRadar
// //
buttonRemoveAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonAddAirplaneWithRadar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveAirplane.Location = new Point(109, 204); buttonAddAirplaneWithRadar.Location = new Point(103, 3);
buttonRemoveAirplane.Name = "buttonRemoveAirplane"; buttonAddAirplaneWithRadar.Name = "buttonAddAirplaneWithRadar";
buttonRemoveAirplane.Size = new Size(91, 43); buttonAddAirplaneWithRadar.Size = new Size(97, 56);
buttonRemoveAirplane.TabIndex = 4; buttonAddAirplaneWithRadar.TabIndex = 2;
buttonRemoveAirplane.Text = "Удалить самолет"; buttonAddAirplaneWithRadar.Text = "Добавить самолет с радаром";
buttonRemoveAirplane.UseVisualStyleBackColor = true; buttonAddAirplaneWithRadar.UseVisualStyleBackColor = true;
buttonRemoveAirplane.Click += ButtonRemoveAirplane_Click; buttonAddAirplaneWithRadar.Click += ButtonAddAirplaneWithRadar_Click;
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //
maskedTextBoxPosition.Location = new Point(6, 215); maskedTextBoxPosition.Location = new Point(0, 81);
maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(78, 23); maskedTextBoxPosition.Size = new Size(78, 23);
maskedTextBoxPosition.TabIndex = 3; maskedTextBoxPosition.TabIndex = 3;
maskedTextBoxPosition.ValidatingType = typeof(int); maskedTextBoxPosition.ValidatingType = typeof(int);
// //
// buttonAddAirplaneWithRadar // buttonRefresh
// //
buttonAddAirplaneWithRadar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddAirplaneWithRadar.Location = new Point(109, 98); buttonRefresh.Location = new Point(103, 123);
buttonAddAirplaneWithRadar.Name = "buttonAddAirplaneWithRadar"; buttonRefresh.Name = "buttonRefresh";
buttonAddAirplaneWithRadar.Size = new Size(91, 56); buttonRefresh.Size = new Size(97, 56);
buttonAddAirplaneWithRadar.TabIndex = 2; buttonRefresh.TabIndex = 6;
buttonAddAirplaneWithRadar.Text = "Добавить самолет с радаром"; buttonRefresh.Text = "Обновить";
buttonAddAirplaneWithRadar.UseVisualStyleBackColor = true; buttonRefresh.UseVisualStyleBackColor = true;
buttonAddAirplaneWithRadar.Click += ButtonAddAirplaneWithRadar_Click; buttonRefresh.Click += ButtonRefresh_Click;
// //
// buttonAddAirplane // buttonRemoveAirplane
// //
buttonAddAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonRemoveAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddAirplane.Location = new Point(6, 98); buttonRemoveAirplane.Location = new Point(103, 70);
buttonAddAirplane.Name = "buttonAddAirplane"; buttonRemoveAirplane.Name = "buttonRemoveAirplane";
buttonAddAirplane.Size = new Size(91, 56); buttonRemoveAirplane.Size = new Size(97, 43);
buttonAddAirplane.TabIndex = 1; buttonRemoveAirplane.TabIndex = 4;
buttonAddAirplane.Text = "Добавить самолет"; buttonRemoveAirplane.Text = "Удалить самолет";
buttonAddAirplane.UseVisualStyleBackColor = true; buttonRemoveAirplane.UseVisualStyleBackColor = true;
buttonAddAirplane.Click += ButtonAddAirplane_Click; buttonRemoveAirplane.Click += ButtonRemoveAirplane_Click;
//
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(0, 123);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(97, 56);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 305);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(194, 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(200, 251);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 221);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(194, 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, 121);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(194, 94);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 82);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(194, 23);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(106, 57);
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(27, 57);
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, 28);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(194, 23);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(38, 10);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(125, 15);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
// //
// comboBoxSelectorCompany // comboBoxSelectorCompany
// //
@ -128,7 +244,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(6, 22); comboBoxSelectorCompany.Location = new Point(6, 276);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(194, 23); comboBoxSelectorCompany.Size = new Size(194, 23);
comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.TabIndex = 0;
@ -153,7 +269,10 @@
Name = "FormAirplaneCollection"; Name = "FormAirplaneCollection";
Text = "Коллекция самолетов"; Text = "Коллекция самолетов";
groupBoxTools.ResumeLayout(false); groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout(); panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -169,5 +288,15 @@
private Button buttonRefresh; private Button buttonRefresh;
private Button buttonGoToCheck; private Button buttonGoToCheck;
private Button buttonRemoveAirplane; private Button buttonRemoveAirplane;
private Panel panelStorage;
private TextBox textBoxCollectionName;
private Label labelCollectionName;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private Panel panelCompanyTools;
} }
} }

View File

@ -8,6 +8,11 @@ namespace ProjectAirplaneWithRadar
/// </summary> /// </summary>
public partial class FormAirplaneCollection : Form public partial class FormAirplaneCollection : Form
{ {
/// <summary>
/// Хранилише коллекций
/// </summary>
private readonly StorageCollection<DrawningAirplane> _storageCollection;
/// <summary> /// <summary>
/// Компания /// Компания
/// </summary> /// </summary>
@ -19,6 +24,7 @@ namespace ProjectAirplaneWithRadar
public FormAirplaneCollection() public FormAirplaneCollection()
{ {
InitializeComponent(); InitializeComponent();
_storageCollection = new();
} }
/// <summary> /// <summary>
@ -28,12 +34,7 @@ namespace ProjectAirplaneWithRadar
/// <param name="e"></param> /// <param name="e"></param>
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{ {
switch (comboBoxSelectorCompany.Text) panelCompanyTools.Enabled = false;
{
case "Хранилище":
_company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningAirplane>());
break;
}
} }
/// <summary> /// <summary>
@ -185,5 +186,94 @@ namespace ProjectAirplaneWithRadar
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
} }
/// <summary>
/// Добавление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
RefreshListBoxItems();
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCollectionDel_Click(object sender, EventArgs e)
{
if(listBoxCollection.SelectedItems == null || listBoxCollection.SelectedIndex < 0)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RefreshListBoxItems();
}
/// <summary>
/// Обновление списка в listBoxCollection
/// </summary>
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);
}
}
/// <summary>
/// Создание компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawningAirplane>? 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;
RefreshListBoxItems();
}
} }
} }