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

This commit is contained in:
egorvasin 2024-05-24 19:31:21 +04:00
parent f2f631d80f
commit 6f1fb9ec3b
6 changed files with 358 additions and 13 deletions

View File

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

View File

@ -37,7 +37,7 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <param name="position">Позиция</param>
/// <returns>true - удаление прошло удачно, false - удаление не удалось</returns>
T Remove(int position);
T? Remove(int position);
/// <summary>
/// Получение объекта по позиции

View File

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

View File

@ -0,0 +1,71 @@
namespace ProjectBuldozer.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 (_storages.ContainsKey(name)) return;
if (collectionType == CollectionType.None) return;
else if (collectionType == CollectionType.Massive) _storages[name] = new MassiveGenericObjects<T>();
else if (collectionType == CollectionType.List) _storages[name] = new ListGenericObjects<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

@ -37,12 +37,24 @@
buttonAddUsualBuldozer = new Button();
comboBoxSelectCompany = new ComboBox();
pictureBox = new PictureBox();
panelStorage = new Panel();
labelCollectionName = new Label();
textBoxCollectionName = new TextBox();
radioButtonMassive = new RadioButton();
radioButtonList = new RadioButton();
buttonCollectionAdd = new Button();
listBoxCollection = new ListBox();
buttonCollectionDel = new Button();
buttonCreateCompany = new Button();
groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
panelStorage.SuspendLayout();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonRemoveBuldozer);
@ -53,7 +65,7 @@
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(705, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(220, 450);
groupBoxTools.Size = new Size(220, 611);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
@ -61,7 +73,7 @@
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(6, 371);
buttonRefresh.Location = new Point(6, 570);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(208, 39);
buttonRefresh.TabIndex = 6;
@ -72,7 +84,7 @@
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 257);
buttonGoToCheck.Location = new Point(6, 528);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(208, 39);
buttonGoToCheck.TabIndex = 5;
@ -83,7 +95,7 @@
// buttonRemoveBuldozer
//
buttonRemoveBuldozer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveBuldozer.Location = new Point(6, 212);
buttonRemoveBuldozer.Location = new Point(6, 483);
buttonRemoveBuldozer.Name = "buttonRemoveBuldozer";
buttonRemoveBuldozer.Size = new Size(208, 39);
buttonRemoveBuldozer.TabIndex = 4;
@ -93,7 +105,7 @@
//
// maskedTextBox
//
maskedTextBox.Location = new Point(6, 183);
maskedTextBox.Location = new Point(6, 454);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(208, 23);
@ -103,7 +115,7 @@
// buttonAddBuldozer
//
buttonAddBuldozer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddBuldozer.Location = new Point(6, 128);
buttonAddBuldozer.Location = new Point(6, 399);
buttonAddBuldozer.Name = "buttonAddBuldozer";
buttonAddBuldozer.Size = new Size(208, 39);
buttonAddBuldozer.TabIndex = 2;
@ -114,7 +126,7 @@
// buttonAddUsualBuldozer
//
buttonAddUsualBuldozer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddUsualBuldozer.Location = new Point(6, 83);
buttonAddUsualBuldozer.Location = new Point(6, 354);
buttonAddUsualBuldozer.Name = "buttonAddUsualBuldozer";
buttonAddUsualBuldozer.Size = new Size(208, 39);
buttonAddUsualBuldozer.TabIndex = 1;
@ -128,7 +140,7 @@
comboBoxSelectCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectCompany.FormattingEnabled = true;
comboBoxSelectCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectCompany.Location = new Point(6, 22);
comboBoxSelectCompany.Location = new Point(6, 281);
comboBoxSelectCompany.Name = "comboBoxSelectCompany";
comboBoxSelectCompany.Size = new Size(208, 23);
comboBoxSelectCompany.TabIndex = 0;
@ -139,16 +151,108 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(705, 450);
pictureBox.Size = new Size(705, 611);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
pictureBox.Click += pictureBox_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(214, 256);
panelStorage.TabIndex = 7;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(52, 12);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(122, 15);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции";
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 39);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(208, 23);
textBoxCollectionName.TabIndex = 1;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(12, 68);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(67, 19);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(111, 68);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(66, 19);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
radioButtonList.CheckedChanged += radioButton_CheckedChanged;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 98);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(208, 23);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавление коллекции";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += buttonCollectionAdd_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(3, 127);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(208, 94);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 227);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(208, 23);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += buttonCollectionDel_Click;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 310);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(208, 23);
buttonCreateCompany.TabIndex = 7;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
buttonCreateCompany.Click += buttonCreateCompany_Click;
//
// FormBuldozerCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(925, 450);
ClientSize = new Size(925, 611);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormBuldozerCollection";
@ -156,6 +260,8 @@
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
ResumeLayout(false);
}
@ -170,5 +276,14 @@
private PictureBox pictureBox;
private Button buttonRefresh;
private Button buttonGoToCheck;
private Panel panelStorage;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Label labelCollectionName;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
}
}

View File

@ -5,6 +5,8 @@ namespace ProjectBuldozer;
public partial class FormBuldozerCollection : Form
{
private readonly StorageCollection<DrawingUsualBuldozer> _storageCollection;
/// <summary>
/// компания
/// </summary>
@ -15,6 +17,7 @@ public partial class FormBuldozerCollection : Form
public FormBuldozerCollection()
{
InitializeComponent();
_storageCollection = new();
}
/// <summary>
/// Выбор компании
@ -84,6 +87,19 @@ public partial class FormBuldozerCollection : Form
}
}
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>
@ -173,11 +189,73 @@ public partial class FormBuldozerCollection : Form
pictureBox.Image = _company.Show();
}
private void pictureBox_Click(object sender, EventArgs e)
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
}
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 buttonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawingUsualBuldozer>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectCompany.Text)
{
case "Хранилище":
_company = new BuldozerSharingServise(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelStorage.Enabled = true;
RerfreshListBoxItems();
}
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();
}
//private void buttonAddUsualBuldozer_Click_1(object sender, EventArgs e)
//{