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

This commit is contained in:
PIBD14CHERTOVANDREY 2024-04-18 13:44:36 +04:00
parent 9048bc6077
commit 1ef7d5694a
5 changed files with 420 additions and 64 deletions

View File

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

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRoadTrain.CollectionGenericObjects;
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 > _collection.Count - 1)
{
return null;
}
return _collection[position];
}
public int Insert(T obj)
{
if (_collection.Count + 1 > _maxCount) { return 0; }
_collection.Add(obj);
return 1;
}
public int Insert(T obj, int position)
{
if (_collection.Count + 1 < _maxCount) { return 0; }
if (position > _collection.Count || position < 0)
{
return 0;
}
_collection.Insert(position, obj);
return 1;
}
public T Remove(int position)
{
if (position > _collection.Count || position < 0)
{
return null;
}
T temp = _collection[position];
_collection.RemoveAt(position);
return temp;
}
}

View File

@ -0,0 +1,53 @@
using ProjectRoadTrain.CollectionGenericObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class StorageCollection<T>
where T : class
{
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
public List<string> Keys => _storages.Keys.ToList();
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
}
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;
}
}
public void DelCollection(string name)
{
if (name == null || !_storages.ContainsKey(name)) { return; }
_storages.Remove(name);
}
public ICollectionGenericObjects<T>? this[string name]
{
get
{
if (name == null || !_storages.ContainsKey(name)) { return null; }
return _storages[name];
}
}
}

View File

@ -29,97 +29,214 @@
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
buttonRefresh = new Button();
buttonGoToCheck = new Button();
buttonDelTrain = new Button();
maskedTextBox = new MaskedTextBox();
panelCompanyTools = new Panel();
buttonAddRoadTrain = new Button();
buttonAddTrain = new Button();
maskedTextBox = new MaskedTextBox();
buttonRefresh = new Button();
buttonDelTrain = 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();
comboBoxSelectCompany = new ComboBox();
pictureBox = new PictureBox();
groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonDelTrain);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddRoadTrain);
groupBoxTools.Controls.Add(buttonAddTrain);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(816, 0);
groupBoxTools.Location = new Point(820, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(218, 583);
groupBoxTools.Size = new Size(218, 780);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "инструменты";
//
// buttonRefresh
// panelCompanyTools
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(6, 470);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(206, 56);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
//
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 358);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(206, 56);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click;
//
// buttonDelTrain
//
buttonDelTrain.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDelTrain.Location = new Point(6, 296);
buttonDelTrain.Name = "buttonDelTrain";
buttonDelTrain.Size = new Size(206, 56);
buttonDelTrain.TabIndex = 4;
buttonDelTrain.Text = "Удаление камаза";
buttonDelTrain.UseVisualStyleBackColor = true;
buttonDelTrain.Click += buttonDelTrain_Click;
//
// maskedTextBox
//
maskedTextBox.Location = new Point(6, 263);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(206, 27);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
panelCompanyTools.Controls.Add(buttonAddRoadTrain);
panelCompanyTools.Controls.Add(buttonAddTrain);
panelCompanyTools.Controls.Add(maskedTextBox);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonDelTrain);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 407);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(212, 370);
panelCompanyTools.TabIndex = 8;
//
// buttonAddRoadTrain
//
buttonAddRoadTrain.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddRoadTrain.Location = new Point(6, 156);
buttonAddRoadTrain.Location = new Point(3, 75);
buttonAddRoadTrain.Name = "buttonAddRoadTrain";
buttonAddRoadTrain.Size = new Size(206, 56);
buttonAddRoadTrain.TabIndex = 2;
buttonAddRoadTrain.Text = "Добавление моющего камаза";
buttonAddRoadTrain.UseVisualStyleBackColor = true;
buttonAddRoadTrain.Click += buttonAddRoadTrain_Click;
buttonAddRoadTrain.Click += ButtonAddRoadTrain_Click;
//
// buttonAddTrain
//
buttonAddTrain.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddTrain.Location = new Point(6, 94);
buttonAddTrain.Location = new Point(3, 13);
buttonAddTrain.Name = "buttonAddTrain";
buttonAddTrain.Size = new Size(206, 56);
buttonAddTrain.TabIndex = 1;
buttonAddTrain.Text = "Добавление камаза";
buttonAddTrain.UseVisualStyleBackColor = true;
buttonAddTrain.Click += buttonAddTrain_Click;
buttonAddTrain.Click += ButtonAddTrain_Click;
//
// maskedTextBox
//
maskedTextBox.Location = new Point(3, 137);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(244, 27);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(3, 294);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(206, 56);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
//
// buttonDelTrain
//
buttonDelTrain.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDelTrain.Location = new Point(3, 170);
buttonDelTrain.Name = "buttonDelTrain";
buttonDelTrain.Size = new Size(206, 56);
buttonDelTrain.TabIndex = 4;
buttonDelTrain.Text = "Удаление камаза";
buttonDelTrain.UseVisualStyleBackColor = true;
buttonDelTrain.Click += ButtonDelTrain_Click;
//
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(3, 232);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(206, 56);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += ButtonGoToCheck_Click;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 372);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(206, 29);
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, 23);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(212, 280);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 241);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(206, 29);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 20;
listBoxCollection.Location = new Point(3, 131);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(206, 104);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 96);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(206, 29);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(101, 66);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(80, 24);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(13, 66);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(82, 24);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 33);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(206, 27);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(3, 10);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(155, 20);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции";
//
// comboBoxSelectCompany
//
@ -127,7 +244,7 @@
comboBoxSelectCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectCompany.FormattingEnabled = true;
comboBoxSelectCompany.Items.AddRange(new object[] { "хранилище" });
comboBoxSelectCompany.Location = new Point(6, 26);
comboBoxSelectCompany.Location = new Point(6, 338);
comboBoxSelectCompany.Name = "comboBoxSelectCompany";
comboBoxSelectCompany.Size = new Size(206, 28);
comboBoxSelectCompany.TabIndex = 0;
@ -138,7 +255,7 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(816, 583);
pictureBox.Size = new Size(820, 780);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
@ -146,13 +263,16 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1034, 583);
ClientSize = new Size(1038, 780);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormTrainCollection";
Text = "Коллекция камазов";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
ResumeLayout(false);
}
@ -168,5 +288,15 @@
private MaskedTextBox maskedTextBox;
private Button buttonRefresh;
private Button buttonGoToCheck;
private Panel panelStorage;
private Label labelCollectionName;
private TextBox textBoxCollectionName;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private Button buttonCollectionDel;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
}
}

View File

@ -15,30 +15,44 @@ namespace ProjectRoadTrain
{
public partial class FormTrainCollection : Form
{
private readonly StorageCollection<DrawningTrain> _storageCollection;
private AbstractCompany? _company = null;
public FormTrainCollection()
{
InitializeComponent();
_storageCollection = new();
}
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);
}
}
}
private void comboBoxSelectCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectCompany.Text)
{
case "хранилище":
case "Хранилище":
_company = new AutoParkService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningTrain>());
break;
}
}
private void buttonAddTrain_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTrain));
private void ButtonAddTrain_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTrain));
/// <summary>
/// Добавление спортивного автомобиля
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddRoadTrain_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningRoadTrain));
private void ButtonAddRoadTrain_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningRoadTrain));
/// <summary>
/// Создание объекта класса-перемещения
@ -86,7 +100,7 @@ namespace ProjectRoadTrain
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonGoToCheck_Click(object sender, EventArgs e)
private void ButtonGoToCheck_Click(object sender, EventArgs e)
{
if (_company == null)
{
@ -179,7 +193,9 @@ namespace ProjectRoadTrain
return color;
}
private void buttonDelTrain_Click(object sender, EventArgs e)
private void ButtonDelTrain_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null)
{
@ -203,6 +219,69 @@ namespace ProjectRoadTrain
}
}
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 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 ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawningTrain>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectCompany.Text)
{
case "хранилище":
_company = new AutoParkService(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
}
}