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

This commit is contained in:
Vladislave 2024-04-12 12:58:06 +03:00
parent 5f2615ad3a
commit 96e1afff66
5 changed files with 438 additions and 60 deletions

View File

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

View File

@ -0,0 +1,79 @@
using ProjectAirBomber.CollectionGenericObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirBomber.CollectionGenericObject;
public class ListGenericObjects<T> : ICollectionGenericObject<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)
{
// TODO проверка позиции
if (position >= Count || position < 0) return null;
return _collection[position];
}
public int Insert(T obj)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO вставка в конец набора
if (Count == _maxCount) return -1;
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO проверка позиции
// TODO вставка по позиции
if (Count == _maxCount) return -1;
if (position >= Count || position < 0) return -1;
_collection.Insert(position, obj);
return position;
}
public T Remove(int position)
{
// TODO проверка позиции
// TODO удаление объекта из списка
if (position >= Count || position < 0) return null;
T obj = _collection[position];
_collection.RemoveAt(position);
return obj;
}
bool? ICollectionGenericObject<T>.Insert(T obj, int position)
{
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirBomber.CollectionGenericObject;
/// <summary>
/// Класс-хранилище коллекций
/// </summary>
/// <typeparam name="T"></typeparam>
public class StorageCollection<T> where T : class
{
/// <summary>
/// Словарь (хранилище) с коллекциями
/// </summary>
readonly Dictionary<string, ICollectionGenericObject<T>> _storages;
/// <summary>
/// Возвращение списка названий коллекций
/// </summary>
public List<string> Keys => _storages.Keys.ToList();
/// <summary>
/// Конструктор
/// </summary>
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObject<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 MassiveGenericObject<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 ICollectionGenericObject<T>? this[string name]
{
get
{
// TODO Продумать логику получения объекта
if (_storages.ContainsKey(name))
return _storages[name];
return null;
}
}
}

View File

@ -29,50 +29,103 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
panelCompanyTools = new Panel();
maskedTextBox = new MaskedTextBox();
buttonAddPlane = new Button();
buttonRefresh = new Button(); buttonRefresh = new Button();
buttonAddAirBomber = new Button();
buttonGoToCheck = new Button(); buttonGoToCheck = new Button();
buttonDelPlane = new Button(); buttonDelPlane = new Button();
maskedTextBox = new MaskedTextBox();
buttonAddAirBomber = new Button();
buttonAddPlane = new Button();
comboBoxSelectionCompany = new ComboBox(); comboBoxSelectionCompany = new ComboBox();
buttonCreateCompany = new Button();
panelStorage = new Panel();
buttonCollectionDel = new Button();
listBoxCollection = new ListBox();
buttonCollectionAdd = new Button();
radioButtonList = new RadioButton();
radioButtonMassive = new RadioButton();
labelCollectionName = new Label();
textBoxCollectionName = new TextBox();
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(buttonDelPlane);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddAirBomber);
groupBoxTools.Controls.Add(buttonAddPlane);
groupBoxTools.Controls.Add(comboBoxSelectionCompany); groupBoxTools.Controls.Add(comboBoxSelectionCompany);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(1035, 0); groupBoxTools.Location = new Point(1152, 0);
groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(250, 667); groupBoxTools.Size = new Size(250, 713);
groupBoxTools.TabIndex = 0; groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты"; groupBoxTools.Text = "Инструменты";
// //
// panelCompanyTools
//
panelCompanyTools.Controls.Add(maskedTextBox);
panelCompanyTools.Controls.Add(buttonAddPlane);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonAddAirBomber);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonDelPlane);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 437);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(244, 273);
panelCompanyTools.TabIndex = 9;
//
// maskedTextBox
//
maskedTextBox.Location = new Point(9, 56);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(229, 27);
maskedTextBox.TabIndex = 4;
maskedTextBox.ValidatingType = typeof(int);
//
// buttonAddPlane
//
buttonAddPlane.Location = new Point(6, 3);
buttonAddPlane.Name = "buttonAddPlane";
buttonAddPlane.Size = new Size(235, 47);
buttonAddPlane.TabIndex = 1;
buttonAddPlane.Text = "Добавление самолетов";
buttonAddPlane.UseVisualStyleBackColor = true;
buttonAddPlane.Click += ButtonAddPlane_Click;
//
// buttonRefresh // buttonRefresh
// //
buttonRefresh.Location = new Point(6, 291); buttonRefresh.Location = new Point(6, 169);
buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(232, 32); buttonRefresh.Size = new Size(229, 32);
buttonRefresh.TabIndex = 7; buttonRefresh.TabIndex = 7;
buttonRefresh.Text = "Обновить"; buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click; buttonRefresh.Click += ButtonRefresh_Click;
// //
// buttonAddAirBomber
//
buttonAddAirBomber.Location = new Point(3, 207);
buttonAddAirBomber.Name = "buttonAddAirBomber";
buttonAddAirBomber.Size = new Size(235, 59);
buttonAddAirBomber.TabIndex = 2;
buttonAddAirBomber.Text = "Добавление бомбардировщика";
buttonAddAirBomber.UseVisualStyleBackColor = true;
buttonAddAirBomber.Click += ButtonAddAirBomber_Click;
//
// buttonGoToCheck // buttonGoToCheck
// //
buttonGoToCheck.Location = new Point(6, 253); buttonGoToCheck.Location = new Point(9, 131);
buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(232, 32); buttonGoToCheck.Size = new Size(229, 32);
buttonGoToCheck.TabIndex = 6; buttonGoToCheck.TabIndex = 6;
buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true; buttonGoToCheck.UseVisualStyleBackColor = true;
@ -80,7 +133,7 @@
// //
// buttonDelPlane // buttonDelPlane
// //
buttonDelPlane.Location = new Point(9, 211); buttonDelPlane.Location = new Point(6, 89);
buttonDelPlane.Name = "buttonDelPlane"; buttonDelPlane.Name = "buttonDelPlane";
buttonDelPlane.Size = new Size(232, 36); buttonDelPlane.Size = new Size(232, 36);
buttonDelPlane.TabIndex = 5; buttonDelPlane.TabIndex = 5;
@ -88,54 +141,115 @@
buttonDelPlane.UseVisualStyleBackColor = true; buttonDelPlane.UseVisualStyleBackColor = true;
buttonDelPlane.Click += ButtonDelPlane_Click; buttonDelPlane.Click += ButtonDelPlane_Click;
// //
// maskedTextBox
//
maskedTextBox.Location = new Point(9, 178);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(229, 27);
maskedTextBox.TabIndex = 4;
maskedTextBox.ValidatingType = typeof(int);
//
// buttonAddAirBomber
//
buttonAddAirBomber.Location = new Point(9, 113);
buttonAddAirBomber.Name = "buttonAddAirBomber";
buttonAddAirBomber.Size = new Size(232, 59);
buttonAddAirBomber.TabIndex = 2;
buttonAddAirBomber.Text = "Добавление бомбардировщика";
buttonAddAirBomber.UseVisualStyleBackColor = true;
buttonAddAirBomber.Click += ButtonAddAirBomber_Click;
//
// buttonAddPlane
//
buttonAddPlane.Location = new Point(9, 60);
buttonAddPlane.Name = "buttonAddPlane";
buttonAddPlane.Size = new Size(232, 47);
buttonAddPlane.TabIndex = 1;
buttonAddPlane.Text = "Добавление самолетов";
buttonAddPlane.UseVisualStyleBackColor = true;
buttonAddPlane.Click += ButtonAddPlane_Click;
//
// comboBoxSelectionCompany // comboBoxSelectionCompany
// //
comboBoxSelectionCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; comboBoxSelectionCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxSelectionCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectionCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectionCompany.FormattingEnabled = true; comboBoxSelectionCompany.FormattingEnabled = true;
comboBoxSelectionCompany.Items.AddRange(new object[] { "Ангар" }); comboBoxSelectionCompany.Items.AddRange(new object[] { "Ангар" });
comboBoxSelectionCompany.Location = new Point(6, 26); comboBoxSelectionCompany.Location = new Point(12, 403);
comboBoxSelectionCompany.Name = "comboBoxSelectionCompany"; comboBoxSelectionCompany.Name = "comboBoxSelectionCompany";
comboBoxSelectionCompany.Size = new Size(232, 28); comboBoxSelectionCompany.Size = new Size(232, 28);
comboBoxSelectionCompany.TabIndex = 0; comboBoxSelectionCompany.TabIndex = 0;
comboBoxSelectionCompany.SelectedIndexChanged += ComboBoxSelectionCompany_SelectedIndexChanged; //
comboBoxSelectionCompany.Click += ComboBoxSelectionCompany_SelectedIndexChanged; // buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 358);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(232, 29);
buttonCreateCompany.TabIndex = 6;
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(labelCollectionName);
panelStorage.Controls.Add(textBoxCollectionName);
panelStorage.Dock = DockStyle.Top;
panelStorage.Location = new Point(3, 23);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(244, 293);
panelStorage.TabIndex = 8;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(6, 262);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(226, 29);
buttonCollectionDel.TabIndex = 5;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 20;
listBoxCollection.Location = new Point(9, 132);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(229, 124);
listBoxCollection.TabIndex = 4;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(9, 97);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(226, 29);
buttonCollectionAdd.TabIndex = 3;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(155, 67);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(80, 24);
radioButtonList.TabIndex = 2;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(9, 67);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(82, 24);
radioButtonMassive.TabIndex = 1;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(41, 11);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(155, 20);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции";
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(9, 34);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(226, 27);
textBoxCollectionName.TabIndex = 0;
// //
// pictureBox // pictureBox
// //
pictureBox.Dock = DockStyle.Fill; pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0); pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox"; pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(1035, 667); pictureBox.Size = new Size(1152, 713);
pictureBox.TabIndex = 3; pictureBox.TabIndex = 3;
pictureBox.TabStop = false; pictureBox.TabStop = false;
// //
@ -143,13 +257,16 @@
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1285, 667); ClientSize = new Size(1402, 713);
Controls.Add(pictureBox); Controls.Add(pictureBox);
Controls.Add(groupBoxTools); Controls.Add(groupBoxTools);
Name = "FormPlaneCollection"; Name = "FormPlaneCollection";
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);
} }
@ -165,5 +282,15 @@
private MaskedTextBox maskedTextBox; private MaskedTextBox maskedTextBox;
private Button buttonRefresh; private Button buttonRefresh;
private Button buttonGoToCheck; private Button buttonGoToCheck;
private Panel panelStorage;
private Label labelCollectionName;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
private Panel panelCompanyTools;
} }
} }

View File

@ -16,6 +16,8 @@ namespace ProjectAirBomber
{ {
public partial class FormPlaneCollection : Form public partial class FormPlaneCollection : Form
{ {
private readonly StorageCollection<DrawningPlane> _storageCollection;
/// <summary> /// <summary>
/// Стратегия перемещения /// Стратегия перемещения
/// </summary> /// </summary>
@ -24,18 +26,13 @@ namespace ProjectAirBomber
public FormPlaneCollection() public FormPlaneCollection()
{ {
InitializeComponent(); InitializeComponent();
_storageCollection = new();
} }
private void ComboBoxSelectionCompany_SelectedIndexChanged(object sender, EventArgs e) private void ComboBoxSelectionCompany_SelectedIndexChanged(object sender, EventArgs e)
{ {
switch (comboBoxSelectionCompany.Text) panelCompanyTools.Enabled = false;
{
case "Ангар":
_company = new PlaneHangar(pictureBox.Width,
pictureBox.Height, new MassiveGenericObject<DrawningPlane>());
break;
}
} }
private void CreateObject(string type) private void CreateObject(string type)
{ {
@ -62,7 +59,7 @@ namespace ProjectAirBomber
return; return;
} }
if (_company + drawningPlane!= -1) if (_company + drawningPlane != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
@ -158,6 +155,80 @@ namespace ProjectAirBomber
} }
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
} }
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) || (!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();
}
private void ButtonCollectionDel_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItems == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RefreshListBoxItems();
}
private void ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObject<DrawningPlane>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectionCompany.Text)
{
case "Ангар":
_company = new PlaneHangar(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RefreshListBoxItems();
}
} }
} }