Lab 4 done

This commit is contained in:
ZakenChannel 2024-04-06 19:38:29 +04:00
parent 472e299e9f
commit 2821afd065
5 changed files with 427 additions and 35 deletions

View File

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

View File

@ -0,0 +1,70 @@
namespace ProjectAirFighter.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];
}
else
{
return null;
}
}
public int Insert(T obj)
{
if (Count == _maxCount) { return -1; }
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
{
if (position < 0 || position >= Count || Count == _maxCount)
{
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,74 @@
namespace ProjectAirFighter.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.Add(name, new MassiveGenericObjects<T> { });
return;
case CollectionType.List:
_storages.Add(name, new ListGenericObjects<T> { });
return;
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
if (name == null || !_storages.ContainsKey(name)) { return; }
_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();
panelCompanyTools = new Panel();
buttonAddFighter = 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();
buttonRefresh = new Button(); buttonRefresh = new Button();
buttonGoToCheck = new Button(); buttonGoToCheck = new Button();
buttonRemoveFighter = new Button(); buttonRemoveFighter = new Button();
maskedTextBox = new MaskedTextBox(); maskedTextBox = new MaskedTextBox();
buttonAddAirFighter = new Button(); buttonAddAirFighter = new Button();
buttonAddFighter = new Button();
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(buttonCreateCompany);
groupBoxTools.Controls.Add(buttonGoToCheck); groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonRemoveFighter); groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddAirFighter);
groupBoxTools.Controls.Add(buttonAddFighter);
groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(974, 0); groupBoxTools.Location = new Point(974, 0);
@ -58,20 +67,139 @@
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты"; groupBoxTools.Text = "Инструменты";
// //
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddFighter);
panelCompanyTools.Controls.Add(buttonAddAirFighter);
panelCompanyTools.Controls.Add(maskedTextBox);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonRemoveFighter);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 333);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(230, 282);
panelCompanyTools.TabIndex = 11;
//
// buttonAddFighter
//
buttonAddFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddFighter.Location = new Point(2, 11);
buttonAddFighter.Name = "buttonAddFighter";
buttonAddFighter.Size = new Size(224, 42);
buttonAddFighter.TabIndex = 1;
buttonAddFighter.Text = "Добавление истребителя";
buttonAddFighter.UseVisualStyleBackColor = true;
buttonAddFighter.Click += ButtonAddFighter_Click;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(8, 307);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(218, 23);
buttonCreateCompany.TabIndex = 9;
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(230, 253);
panelStorage.TabIndex = 8;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(5, 219);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(218, 23);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(5, 119);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(218, 94);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 83);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(224, 23);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(135, 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(32, 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(224, 23);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(55, 11);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(125, 15);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// buttonRefresh // buttonRefresh
// //
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(6, 564); buttonRefresh.Location = new Point(2, 232);
buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(224, 42); buttonRefresh.Size = new Size(224, 42);
buttonRefresh.TabIndex = 6; buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить"; buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
// //
// buttonGoToCheck // buttonGoToCheck
// //
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 396); buttonGoToCheck.Location = new Point(2, 184);
buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(224, 42); buttonGoToCheck.Size = new Size(224, 42);
buttonGoToCheck.TabIndex = 5; buttonGoToCheck.TabIndex = 5;
@ -82,7 +210,7 @@
// buttonRemoveFighter // buttonRemoveFighter
// //
buttonRemoveFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonRemoveFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveFighter.Location = new Point(6, 263); buttonRemoveFighter.Location = new Point(2, 136);
buttonRemoveFighter.Name = "buttonRemoveFighter"; buttonRemoveFighter.Name = "buttonRemoveFighter";
buttonRemoveFighter.Size = new Size(224, 42); buttonRemoveFighter.Size = new Size(224, 42);
buttonRemoveFighter.TabIndex = 4; buttonRemoveFighter.TabIndex = 4;
@ -92,7 +220,7 @@
// //
// maskedTextBox // maskedTextBox
// //
maskedTextBox.Location = new Point(6, 234); maskedTextBox.Location = new Point(2, 107);
maskedTextBox.Mask = "00"; maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(224, 23); maskedTextBox.Size = new Size(224, 23);
@ -102,7 +230,7 @@
// buttonAddAirFighter // buttonAddAirFighter
// //
buttonAddAirFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonAddAirFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddAirFighter.Location = new Point(6, 133); buttonAddAirFighter.Location = new Point(2, 59);
buttonAddAirFighter.Name = "buttonAddAirFighter"; buttonAddAirFighter.Name = "buttonAddAirFighter";
buttonAddAirFighter.Size = new Size(224, 42); buttonAddAirFighter.Size = new Size(224, 42);
buttonAddAirFighter.TabIndex = 2; buttonAddAirFighter.TabIndex = 2;
@ -110,24 +238,13 @@
buttonAddAirFighter.UseVisualStyleBackColor = true; buttonAddAirFighter.UseVisualStyleBackColor = true;
buttonAddAirFighter.Click += ButtonAddAirFighter_Click; buttonAddAirFighter.Click += ButtonAddAirFighter_Click;
// //
// buttonAddFighter
//
buttonAddFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddFighter.Location = new Point(6, 85);
buttonAddFighter.Name = "buttonAddFighter";
buttonAddFighter.Size = new Size(224, 42);
buttonAddFighter.TabIndex = 1;
buttonAddFighter.Text = "Добавление истребителя";
buttonAddFighter.UseVisualStyleBackColor = true;
buttonAddFighter.Click += ButtonAddFighter_Click;
//
// comboBoxSelectorCompany // comboBoxSelectorCompany
// //
comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
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, 278);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(224, 23); comboBoxSelectorCompany.Size = new Size(224, 23);
comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.TabIndex = 0;
@ -142,17 +259,20 @@
pictureBox.TabIndex = 1; pictureBox.TabIndex = 1;
pictureBox.TabStop = false; pictureBox.TabStop = false;
// //
// FormFighterCollection // FormWarPlaneCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1210, 618); ClientSize = new Size(1210, 618);
Controls.Add(pictureBox); Controls.Add(pictureBox);
Controls.Add(groupBoxTools); Controls.Add(groupBoxTools);
Name = "FormFighterCollection"; Name = "FormWarPlaneCollection";
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);
} }
@ -161,12 +281,22 @@
private GroupBox groupBoxTools; private GroupBox groupBoxTools;
private ComboBox comboBoxSelectorCompany; private ComboBox comboBoxSelectorCompany;
private Button buttonAddAirFighter; private PictureBox pictureBox;
private Panel panelStorage;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Label labelCollectionName;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
private Button buttonAddFighter; private Button buttonAddFighter;
private Button buttonRefresh; private Button buttonRefresh;
private Button buttonGoToCheck; private Button buttonGoToCheck;
private Button buttonRemoveFighter; private Button buttonRemoveFighter;
private MaskedTextBox maskedTextBox; private MaskedTextBox maskedTextBox;
private PictureBox pictureBox; private Button buttonAddAirFighter;
} }
} }

View File

@ -8,6 +8,11 @@ namespace ProjectAirFighter;
/// </summary> /// </summary>
public partial class FormWarPlaneCollection : Form public partial class FormWarPlaneCollection : Form
{ {
/// <summary>
/// Хранилише коллекций
/// </summary>
private readonly StorageCollection<DrawningWarPlane> _storageCollection;
/// <summary> /// <summary>
/// Компания /// Компания
/// </summary> /// </summary>
@ -19,6 +24,7 @@ public partial class FormWarPlaneCollection : Form
public FormWarPlaneCollection() public FormWarPlaneCollection()
{ {
InitializeComponent(); InitializeComponent();
_storageCollection = new();
} }
/// <summary> /// <summary>
@ -28,12 +34,7 @@ public partial class FormWarPlaneCollection : Form
/// <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 Hangar(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningWarPlane>());
break;
}
} }
/// <summary> /// <summary>
@ -174,4 +175,99 @@ public partial class FormWarPlaneCollection : Form
}; };
form.ShowDialog(); form.ShowDialog();
} }
/// <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);
RerfreshListBoxItems();
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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.Yes)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RerfreshListBoxItems();
}
/// <summary>
/// Обновление списка в listBoxCollection
/// </summary>
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);
}
}
}
/// <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<DrawningWarPlane>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new Hangar(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
} }