Законченная 4ая лабораторная

This commit is contained in:
tyxz0 2024-04-26 00:40:25 +04:00
parent 85d39d43aa
commit 5aa1c71b4c
4 changed files with 368 additions and 71 deletions

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.CollectionGenericObjects;
/// <summary>
/// Тип коллекции
/// </summary>
public enum CollectionType
{
None = 0,
Massive = 1,
List = 2
}

View File

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.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();
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
}
public void AddCollection(string name, CollectionType collectionType)
{
if (string.IsNullOrEmpty(name) || _storages.ContainsKey(name))
{
return;
}
switch (collectionType)
{
case CollectionType.List:
ListGenericObjects<T> _listToAdd = new ListGenericObjects<T>();
_storages.Add(name, _listToAdd);
return;
case CollectionType.Massive:
MassiveGenericObjects<T> _arrayToAdd = new MassiveGenericObjects<T>();
_storages.Add(name, _arrayToAdd);
return;
case CollectionType.None:
return;
}
}
public void DelCollection(string name)
{
_storages.Remove(name);
}
public ICollectionGenericObjects<T>? this[string name]
{
get
{
if (_storages.ContainsKey(name))
{
return _storages[name];
}
return null;
}
}
}

View File

@ -29,105 +29,221 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
buttonRefresh = new Button(); panelCompanyTools = new Panel();
buttonGoToCheck = new Button();
buttonRemoveBus = new Button();
maskedTextBox = new MaskedTextBox();
buttonAddDoublrDeckerBus = new Button();
buttonAddBus = new Button(); buttonAddBus = new Button();
buttonAddDoublrDeckerBus = new Button();
maskedTextBox = new MaskedTextBox();
buttonRefresh = new Button();
buttonRemoveBus = new Button();
buttonGoToCheck = new Button();
buttonCreateCompany = new Button();
panelStorage = new Panel();
buttonRemoveCollection = new Button();
listBoxCollectionItems = new ListBox();
buttonCollectionAdd = new Button();
radioButtonList = new RadioButton();
radioButtonArray = new RadioButton();
textBoxCollectionName = new TextBox();
labelCollectionName = new Label();
comboBoxSelectCompany = new ComboBox(); comboBoxSelectCompany = 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(panelCompanyTools);
groupBoxTools.Controls.Add(buttonGoToCheck); groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(buttonRemoveBus); groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddDoublrDeckerBus);
groupBoxTools.Controls.Add(buttonAddBus);
groupBoxTools.Controls.Add(comboBoxSelectCompany); groupBoxTools.Controls.Add(comboBoxSelectCompany);
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(870, 0); groupBoxTools.Location = new Point(870, 0);
groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(200, 450); groupBoxTools.Size = new Size(200, 626);
groupBoxTools.TabIndex = 0; groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Tools"; groupBoxTools.Text = "Tools";
// //
// buttonRefresh // panelCompanyTools
// //
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; panelCompanyTools.Controls.Add(buttonAddBus);
buttonRefresh.Location = new Point(6, 390); panelCompanyTools.Controls.Add(buttonAddDoublrDeckerBus);
buttonRefresh.Name = "buttonRefresh"; panelCompanyTools.Controls.Add(maskedTextBox);
buttonRefresh.Size = new Size(188, 35); panelCompanyTools.Controls.Add(buttonRefresh);
buttonRefresh.TabIndex = 6; panelCompanyTools.Controls.Add(buttonRemoveBus);
buttonRefresh.Text = "Refresh"; panelCompanyTools.Controls.Add(buttonGoToCheck);
buttonRefresh.UseVisualStyleBackColor = true; panelCompanyTools.Dock = DockStyle.Bottom;
buttonRefresh.Click += ButtonRefresh_Click; panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 358);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(194, 265);
panelCompanyTools.TabIndex = 16;
// //
// buttonGoToCheck // buttonAddBus
// //
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 312); buttonAddBus.Location = new Point(0, 13);
buttonGoToCheck.Name = "buttonGoToCheck"; buttonAddBus.Name = "buttonAddBus";
buttonGoToCheck.Size = new Size(188, 35); buttonAddBus.Size = new Size(191, 35);
buttonGoToCheck.TabIndex = 5; buttonAddBus.TabIndex = 1;
buttonGoToCheck.Text = "Send to check"; buttonAddBus.Text = "Add bus";
buttonGoToCheck.UseVisualStyleBackColor = true; buttonAddBus.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += ButtonGoToCheck_Click; buttonAddBus.Click += ButtonAddBus_Click;
//
// buttonRemoveBus
//
buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveBus.Location = new Point(6, 225);
buttonRemoveBus.Name = "buttonRemoveBus";
buttonRemoveBus.Size = new Size(188, 35);
buttonRemoveBus.TabIndex = 4;
buttonRemoveBus.Text = "Remove bus";
buttonRemoveBus.UseVisualStyleBackColor = true;
buttonRemoveBus.Click += ButtonRemoveBus_Click;
//
// maskedTextBox
//
maskedTextBox.Location = new Point(6, 196);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(182, 23);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
// //
// buttonAddDoublrDeckerBus // buttonAddDoublrDeckerBus
// //
buttonAddDoublrDeckerBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonAddDoublrDeckerBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddDoublrDeckerBus.Location = new Point(6, 126); buttonAddDoublrDeckerBus.Location = new Point(0, 54);
buttonAddDoublrDeckerBus.Name = "buttonAddDoublrDeckerBus"; buttonAddDoublrDeckerBus.Name = "buttonAddDoublrDeckerBus";
buttonAddDoublrDeckerBus.Size = new Size(188, 35); buttonAddDoublrDeckerBus.Size = new Size(191, 35);
buttonAddDoublrDeckerBus.TabIndex = 2; buttonAddDoublrDeckerBus.TabIndex = 2;
buttonAddDoublrDeckerBus.Text = "Add double-decker bus"; buttonAddDoublrDeckerBus.Text = "Add double-decker bus";
buttonAddDoublrDeckerBus.UseVisualStyleBackColor = true; buttonAddDoublrDeckerBus.UseVisualStyleBackColor = true;
buttonAddDoublrDeckerBus.Click += ButtonAddDoublrDeckerBus_Click; buttonAddDoublrDeckerBus.Click += ButtonAddDoublrDeckerBus_Click;
// //
// buttonAddBus // maskedTextBox
// //
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; maskedTextBox.Location = new Point(0, 95);
buttonAddBus.Location = new Point(6, 85); maskedTextBox.Mask = "00";
buttonAddBus.Name = "buttonAddBus"; maskedTextBox.Name = "maskedTextBox";
buttonAddBus.Size = new Size(188, 35); maskedTextBox.Size = new Size(188, 23);
buttonAddBus.TabIndex = 1; maskedTextBox.TabIndex = 3;
buttonAddBus.Text = "Add bus"; maskedTextBox.ValidatingType = typeof(int);
buttonAddBus.UseVisualStyleBackColor = true; //
buttonAddBus.Click += ButtonAddBus_Click; // buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(0, 206);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(191, 35);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Refresh";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
//
// buttonRemoveBus
//
buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveBus.Location = new Point(0, 124);
buttonRemoveBus.Name = "buttonRemoveBus";
buttonRemoveBus.Size = new Size(191, 35);
buttonRemoveBus.TabIndex = 4;
buttonRemoveBus.Text = "Remove bus";
buttonRemoveBus.UseVisualStyleBackColor = true;
buttonRemoveBus.Click += ButtonRemoveBus_Click;
//
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(0, 165);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(191, 35);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Send to check";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += ButtonGoToCheck_Click;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 296);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(188, 23);
buttonCreateCompany.TabIndex = 15;
buttonCreateCompany.Text = "Create compnay";
buttonCreateCompany.UseVisualStyleBackColor = true;
buttonCreateCompany.Click += ButtonCreateCompany_Click;
//
// panelStorage
//
panelStorage.Controls.Add(buttonRemoveCollection);
panelStorage.Controls.Add(listBoxCollectionItems);
panelStorage.Controls.Add(buttonCollectionAdd);
panelStorage.Controls.Add(radioButtonList);
panelStorage.Controls.Add(radioButtonArray);
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(194, 242);
panelStorage.TabIndex = 7;
//
// buttonRemoveCollection
//
buttonRemoveCollection.Location = new Point(3, 208);
buttonRemoveCollection.Name = "buttonRemoveCollection";
buttonRemoveCollection.Size = new Size(188, 23);
buttonRemoveCollection.TabIndex = 14;
buttonRemoveCollection.Text = "Remove Collection";
buttonRemoveCollection.UseVisualStyleBackColor = true;
buttonRemoveCollection.Click += ButtonRemoveCollection_Click;
//
// listBoxCollectionItems
//
listBoxCollectionItems.FormattingEnabled = true;
listBoxCollectionItems.ItemHeight = 15;
listBoxCollectionItems.Location = new Point(3, 108);
listBoxCollectionItems.Name = "listBoxCollectionItems";
listBoxCollectionItems.Size = new Size(188, 94);
listBoxCollectionItems.TabIndex = 13;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 79);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(188, 23);
buttonCollectionAdd.TabIndex = 12;
buttonCollectionAdd.Text = "Add Collection";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(126, 47);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(43, 19);
radioButtonList.TabIndex = 11;
radioButtonList.TabStop = true;
radioButtonList.Text = "List";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonArray
//
radioButtonArray.AutoSize = true;
radioButtonArray.Location = new Point(22, 47);
radioButtonArray.Name = "radioButtonArray";
radioButtonArray.Size = new Size(53, 19);
radioButtonArray.TabIndex = 10;
radioButtonArray.TabStop = true;
radioButtonArray.Text = "Array";
radioButtonArray.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 18);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(188, 23);
textBoxCollectionName.TabIndex = 9;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(40, 0);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(108, 15);
labelCollectionName.TabIndex = 8;
labelCollectionName.Text = "Name of collection\r\n";
// //
// comboBoxSelectCompany // comboBoxSelectCompany
// //
comboBoxSelectCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectCompany.FormattingEnabled = true; comboBoxSelectCompany.FormattingEnabled = true;
comboBoxSelectCompany.Items.AddRange(new object[] { "Storage" }); comboBoxSelectCompany.Items.AddRange(new object[] { "Storage" });
comboBoxSelectCompany.Location = new Point(6, 22); comboBoxSelectCompany.Location = new Point(6, 267);
comboBoxSelectCompany.Name = "comboBoxSelectCompany"; comboBoxSelectCompany.Name = "comboBoxSelectCompany";
comboBoxSelectCompany.Size = new Size(188, 23); comboBoxSelectCompany.Size = new Size(188, 23);
comboBoxSelectCompany.TabIndex = 0; comboBoxSelectCompany.TabIndex = 0;
@ -138,7 +254,7 @@
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(870, 450); pictureBox.Size = new Size(870, 626);
pictureBox.TabIndex = 1; pictureBox.TabIndex = 1;
pictureBox.TabStop = false; pictureBox.TabStop = false;
// //
@ -146,13 +262,16 @@
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1070, 450); ClientSize = new Size(1070, 626);
Controls.Add(pictureBox); Controls.Add(pictureBox);
Controls.Add(groupBoxTools); Controls.Add(groupBoxTools);
Name = "FormBusCollection"; Name = "FormBusCollection";
Text = "Bus collection"; Text = "Bus collection";
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);
} }
@ -168,5 +287,15 @@
private PictureBox pictureBox; private PictureBox pictureBox;
private Button buttonRefresh; private Button buttonRefresh;
private Button buttonGoToCheck; private Button buttonGoToCheck;
private Panel panelStorage;
private Label labelCollectionName;
private TextBox textBoxCollectionName;
private RadioButton radioButtonArray;
private RadioButton radioButtonList;
private Button buttonCollectionAdd;
private ListBox listBoxCollectionItems;
private Button buttonCreateCompany;
private Button buttonRemoveCollection;
private Panel panelCompanyTools;
} }
} }

View File

@ -16,22 +16,20 @@ public partial class FormBusCollection : Form
{ {
private AbstractCompany? _company = null; private AbstractCompany? _company = null;
private readonly StorageCollection<DrawingBus> _storageCollection;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormBusCollection() public FormBusCollection()
{ {
InitializeComponent(); InitializeComponent();
_storageCollection = new();
} }
private void comboBoxSelectCompany_SelectedIndexChanged(object sender, EventArgs e) private void comboBoxSelectCompany_SelectedIndexChanged(object sender, EventArgs e)
{ {
switch (comboBoxSelectCompany.Text) panelCompanyTools.Enabled = false;
{
case "Storage":
_company = new BusStation(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawingBus>());
break;
}
} }
private void CreateObject(string type) private void CreateObject(string type)
@ -150,4 +148,86 @@ public partial class FormBusCollection : Form
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
} }
private void ButtonCollectionAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonArray.Checked && !radioButtonList.Checked))
{
MessageBox.Show("Not all data is filled in", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CollectionType collectionType = CollectionType.None;
if (radioButtonArray.Checked)
{
collectionType = CollectionType.Massive;
}
else if (radioButtonList.Checked)
{
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
RefreshListBoxItems();
}
private void RefreshListBoxItems()
{
listBoxCollectionItems.Items.Clear();
for (int i = 0; i < _storageCollection.Keys?.Count; i++)
{
string? colName = _storageCollection.Keys?[i];
if (!string.IsNullOrEmpty(colName))
{
listBoxCollectionItems.Items.Add(colName);
}
}
}
private void ButtonRemoveCollection_Click(object sender, EventArgs e)
{
if (listBoxCollectionItems.SelectedIndex < 0)
{
MessageBox.Show("No collection selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (listBoxCollectionItems.SelectedItems.Count == 1)
{
DialogResult result = MessageBox.Show("Are you sure you want to delete the selected collection?", "Confirm and remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
_storageCollection.DelCollection(listBoxCollectionItems.Text);
RefreshListBoxItems();
}
else
{
return;
}
}
}
private void ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollectionItems.SelectedIndex < 0 || listBoxCollectionItems.SelectedItem == null)
{
MessageBox.Show("No collection selected");
return;
}
ICollectionGenericObjects<DrawingBus>? collection = _storageCollection[listBoxCollectionItems.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("The collection is not initialized");
return;
}
switch (comboBoxSelectCompany.Text)
{
case "Storage":
_company = new BusStation(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
}
} }