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

This commit is contained in:
5_fG 2024-04-29 12:21:40 +04:00
parent ede3b91de8
commit 34b237b368
6 changed files with 466 additions and 36 deletions

View File

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

View File

@ -0,0 +1,87 @@
namespace ProjectExcavator.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 (_collection.Count > _maxCount)
{
return -1;
}
_collection.Add(obj);
return 1;
}
public int Insert(T obj, int position)
{
// проверка, что не превышено максимальное количество элементов
// проверка позиции
// вставка по позиции
if ((_collection.Count > _maxCount) || (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? obj = _collection[position];
_collection.RemoveAt(position);
return obj;
}
}

View File

@ -58,15 +58,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
{ {
//Вставка в свободное место набора //Вставка в свободное место набора
for (int i = 0; i < Count -1; i++) return Insert(obj, 0);
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
return -1;
} }
public int Insert(T obj, int position) public int Insert(T obj, int position)
@ -117,17 +109,13 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
// Проверка позиции // Проверка позиции
// Удаление объекта из массива, присвоив элементу массива значение null // Удаление объекта из массива, присвоив элементу массива значение null
T? obj = _collection[position];
if (position < 0 || _collection[position] == null || position > Count -1) if (position < 0 || _collection[position] == null || position > Count -1)
{ {
return null; return null;
} }
T obj = _collection[position];
else _collection[position] = null;
{ return obj;
_collection[position] = null;
return obj;
}
} }
} }

View File

@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Xml.Linq;
namespace ProjectExcavator.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;
}
else
{
switch (collectionType)
{
case CollectionType.None:
return;
case CollectionType.List:
_storages.Add(name, new ListGenericObjects<T> { });
return;
case CollectionType.Massive:
_storages.Add(name, new MassiveGenericObjects<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,6 +29,15 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); 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(); buttonRefresh = new Button();
buttonGoToCheck = new Button(); buttonGoToCheck = new Button();
ButtonRemoveExcavator = new Button(); ButtonRemoveExcavator = new Button();
@ -37,18 +46,18 @@
buttonAddTrackedVehicle = new Button(); buttonAddTrackedVehicle = new Button();
comboBoxSelectorCompany = new ComboBox(); comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox(); pictureBox = new PictureBox();
panelCompanyTools = new Panel();
groupBoxTools.SuspendLayout(); groupBoxTools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
panelCompanyTools.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// groupBoxTools // groupBoxTools
// //
groupBoxTools.Controls.Add(buttonRefresh); groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonGoToCheck); groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(ButtonRemoveExcavator); groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddExcavator);
groupBoxTools.Controls.Add(buttonAddTrackedVehicle);
groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(777, 0); groupBoxTools.Location = new Point(777, 0);
@ -58,11 +67,115 @@
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты"; groupBoxTools.Text = "Инструменты";
// //
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(13, 317);
buttonCreateCompany.Margin = new Padding(3, 4, 3, 4);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(200, 30);
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, 23);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(219, 253);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Anchor = AnchorStyles.Top;
buttonCollectionDel.Location = new Point(3, 222);
buttonCollectionDel.Margin = new Padding(3, 4, 3, 4);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(215, 27);
buttonCollectionDel.TabIndex = 7;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.Anchor = AnchorStyles.Top;
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 20;
listBoxCollection.Location = new Point(3, 135);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(215, 84);
listBoxCollection.TabIndex = 6;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Anchor = AnchorStyles.Top;
buttonCollectionAdd.Location = new Point(3, 100);
buttonCollectionAdd.Margin = new Padding(3, 4, 3, 4);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(215, 27);
buttonCollectionAdd.TabIndex = 5;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(130, 70);
radioButtonList.Margin = new Padding(3, 4, 3, 4);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(80, 24);
radioButtonList.TabIndex = 4;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(10, 70);
radioButtonMassive.Margin = new Padding(3, 4, 3, 4);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(82, 24);
radioButtonMassive.TabIndex = 3;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Anchor = AnchorStyles.Top;
textBoxCollectionName.Location = new Point(3, 34);
textBoxCollectionName.Margin = new Padding(3, 4, 3, 4);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(215, 27);
textBoxCollectionName.TabIndex = 2;
//
// labelCollectionName
//
labelCollectionName.Anchor = AnchorStyles.Top;
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(31, 11);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(158, 20);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// buttonRefresh // buttonRefresh
// //
buttonRefresh.Location = new Point(13, 554); buttonRefresh.Anchor = AnchorStyles.Top;
buttonRefresh.Location = new Point(3, 240);
buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(200, 60); buttonRefresh.Size = new Size(215, 40);
buttonRefresh.TabIndex = 6; buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить"; buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.UseVisualStyleBackColor = true;
@ -70,9 +183,10 @@
// //
// buttonGoToCheck // buttonGoToCheck
// //
buttonGoToCheck.Location = new Point(13, 469); buttonGoToCheck.Anchor = AnchorStyles.Top;
buttonGoToCheck.Location = new Point(3, 194);
buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(200, 60); buttonGoToCheck.Size = new Size(215, 40);
buttonGoToCheck.TabIndex = 5; buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true; buttonGoToCheck.UseVisualStyleBackColor = true;
@ -80,9 +194,10 @@
// //
// ButtonRemoveExcavator // ButtonRemoveExcavator
// //
ButtonRemoveExcavator.Location = new Point(10, 386); ButtonRemoveExcavator.Anchor = AnchorStyles.Top;
ButtonRemoveExcavator.Location = new Point(3, 148);
ButtonRemoveExcavator.Name = "ButtonRemoveExcavator"; ButtonRemoveExcavator.Name = "ButtonRemoveExcavator";
ButtonRemoveExcavator.Size = new Size(200, 60); ButtonRemoveExcavator.Size = new Size(215, 40);
ButtonRemoveExcavator.TabIndex = 4; ButtonRemoveExcavator.TabIndex = 4;
ButtonRemoveExcavator.Text = "Удаление транспорта"; ButtonRemoveExcavator.Text = "Удаление транспорта";
ButtonRemoveExcavator.UseVisualStyleBackColor = true; ButtonRemoveExcavator.UseVisualStyleBackColor = true;
@ -90,18 +205,20 @@
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //
maskedTextBoxPosition.Location = new Point(13, 281); maskedTextBoxPosition.Anchor = AnchorStyles.Top;
maskedTextBoxPosition.Location = new Point(3, 115);
maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(200, 27); maskedTextBoxPosition.Size = new Size(215, 27);
maskedTextBoxPosition.TabIndex = 3; maskedTextBoxPosition.TabIndex = 3;
maskedTextBoxPosition.ValidatingType = typeof(int); maskedTextBoxPosition.ValidatingType = typeof(int);
// //
// buttonAddExcavator // buttonAddExcavator
// //
buttonAddExcavator.Location = new Point(13, 164); buttonAddExcavator.Anchor = AnchorStyles.Top;
buttonAddExcavator.Location = new Point(2, 59);
buttonAddExcavator.Name = "buttonAddExcavator"; buttonAddExcavator.Name = "buttonAddExcavator";
buttonAddExcavator.Size = new Size(200, 65); buttonAddExcavator.Size = new Size(215, 50);
buttonAddExcavator.TabIndex = 2; buttonAddExcavator.TabIndex = 2;
buttonAddExcavator.Text = "Добавление экскаватора"; buttonAddExcavator.Text = "Добавление экскаватора";
buttonAddExcavator.UseVisualStyleBackColor = true; buttonAddExcavator.UseVisualStyleBackColor = true;
@ -109,9 +226,10 @@
// //
// buttonAddTrackedVehicle // buttonAddTrackedVehicle
// //
buttonAddTrackedVehicle.Location = new Point(10, 93); buttonAddTrackedVehicle.Anchor = AnchorStyles.Top;
buttonAddTrackedVehicle.Location = new Point(3, 3);
buttonAddTrackedVehicle.Name = "buttonAddTrackedVehicle"; buttonAddTrackedVehicle.Name = "buttonAddTrackedVehicle";
buttonAddTrackedVehicle.Size = new Size(200, 65); buttonAddTrackedVehicle.Size = new Size(215, 50);
buttonAddTrackedVehicle.TabIndex = 1; buttonAddTrackedVehicle.TabIndex = 1;
buttonAddTrackedVehicle.Text = "Добавление гусеничной машины"; buttonAddTrackedVehicle.Text = "Добавление гусеничной машины";
buttonAddTrackedVehicle.UseVisualStyleBackColor = true; buttonAddTrackedVehicle.UseVisualStyleBackColor = true;
@ -123,7 +241,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(10, 26); comboBoxSelectorCompany.Location = new Point(13, 282);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(200, 28); comboBoxSelectorCompany.Size = new Size(200, 28);
comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.TabIndex = 0;
@ -138,6 +256,20 @@
pictureBox.TabIndex = 1; pictureBox.TabIndex = 1;
pictureBox.TabStop = false; pictureBox.TabStop = false;
// //
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddTrackedVehicle);
panelCompanyTools.Controls.Add(buttonAddExcavator);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(ButtonRemoveExcavator);
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(2, 350);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(220, 289);
panelCompanyTools.TabIndex = 2;
//
// FormExcavatorCollection // FormExcavatorCollection
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
@ -148,8 +280,11 @@
Name = "FormExcavatorCollection"; Name = "FormExcavatorCollection";
Text = "Коллекция экскаваторов"; Text = "Коллекция экскаваторов";
groupBoxTools.ResumeLayout(false); groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout(); panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
} }
@ -164,5 +299,15 @@
private MaskedTextBox maskedTextBoxPosition; private MaskedTextBox maskedTextBoxPosition;
private Button buttonGoToCheck; private Button buttonGoToCheck;
private Button buttonRefresh; private Button buttonRefresh;
private Panel panelStorage;
private Label labelCollectionName;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private Button buttonCollectionDel;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
} }
} }

View File

@ -7,6 +7,11 @@ namespace ProjectExcavator;
/// </summary> /// </summary>
public partial class FormExcavatorCollection : Form public partial class FormExcavatorCollection : Form
{ {
/// <summary>
/// Хранилише коллекций
/// </summary>
private readonly StorageCollection<DrawningTrackedVehicle> _storageCollection;
/// <summary> /// <summary>
/// Компания /// Компания
/// </summary> /// </summary>
@ -18,6 +23,7 @@ public partial class FormExcavatorCollection : Form
public FormExcavatorCollection() public FormExcavatorCollection()
{ {
InitializeComponent(); InitializeComponent();
_storageCollection = new();
} }
/// <summary> /// <summary>
@ -181,4 +187,104 @@ public partial class FormExcavatorCollection : Form
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);
RerfreshListBoxItems();
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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());
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<DrawningTrackedVehicle>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new GarageService(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
} }