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

This commit is contained in:
ILYAkuznetsov73 2024-04-10 19:54:20 +04:00
parent fde8810228
commit e80a90cc35
5 changed files with 390 additions and 30 deletions

View File

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

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectGasolineTanker.CollectionGenericObjects;
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;
switch (collectionType)
{
case CollectionType.None:
return;
case CollectionType.Massive:
_storages[name] = new MassiveGenericObjects<T>();
return;
case CollectionType.List:
_storages[name] = new ListGenericObjects<T>();
return;
}
}
/// <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 (name == null || !_storages.ContainsKey(name))
return null;
return _storages[name];
}
}
}

View File

@ -29,6 +29,15 @@
private void InitializeComponent()
{
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();
buttonGoToCheck = new Button();
buttonDelTanker = new Button();
@ -37,31 +46,123 @@
buttonAddTanker = new Button();
comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox();
panelCompanyTools = new Panel();
groupBoxTools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
panelCompanyTools.SuspendLayout();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonDelTanker);
groupBoxTools.Controls.Add(maskedTextBox1);
groupBoxTools.Controls.Add(buttonAddGasolineTanker);
groupBoxTools.Controls.Add(buttonAddTanker);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(1095, 0);
groupBoxTools.Location = new Point(1222, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(254, 681);
groupBoxTools.Size = new Size(258, 810);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(16, 374);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(226, 34);
buttonCreateCompany.TabIndex = 8;
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, 27);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(252, 328);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(13, 284);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(226, 34);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += buttonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 25;
listBoxCollection.Location = new Point(13, 149);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(226, 129);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(13, 109);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(226, 34);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += buttonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(107, 74);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(96, 29);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(13, 74);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(98, 29);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(13, 37);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(226, 31);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(33, 9);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(186, 25);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(16, 522);
buttonRefresh.Location = new Point(13, 268);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(226, 55);
buttonRefresh.TabIndex = 6;
@ -72,9 +173,9 @@
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(16, 426);
buttonGoToCheck.Location = new Point(13, 207);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(226, 55);
buttonGoToCheck.Size = new Size(230, 55);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
@ -83,9 +184,9 @@
// buttonDelTanker
//
buttonDelTanker.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDelTanker.Location = new Point(16, 325);
buttonDelTanker.Location = new Point(13, 146);
buttonDelTanker.Name = "buttonDelTanker";
buttonDelTanker.Size = new Size(226, 55);
buttonDelTanker.Size = new Size(230, 55);
buttonDelTanker.TabIndex = 4;
buttonDelTanker.Text = "Удаление грузовика";
buttonDelTanker.UseVisualStyleBackColor = true;
@ -93,19 +194,19 @@
//
// maskedTextBox1
//
maskedTextBox1.Location = new Point(16, 262);
maskedTextBox1.Location = new Point(13, 109);
maskedTextBox1.Mask = "00";
maskedTextBox1.Name = "maskedTextBox1";
maskedTextBox1.Size = new Size(150, 31);
maskedTextBox1.Size = new Size(230, 31);
maskedTextBox1.TabIndex = 3;
maskedTextBox1.ValidatingType = typeof(int);
//
// buttonAddGasolineTanker
//
buttonAddGasolineTanker.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddGasolineTanker.Location = new Point(16, 179);
buttonAddGasolineTanker.Location = new Point(13, 63);
buttonAddGasolineTanker.Name = "buttonAddGasolineTanker";
buttonAddGasolineTanker.Size = new Size(226, 55);
buttonAddGasolineTanker.Size = new Size(230, 40);
buttonAddGasolineTanker.TabIndex = 2;
buttonAddGasolineTanker.Text = "Добавление Бензовоза";
buttonAddGasolineTanker.UseVisualStyleBackColor = true;
@ -114,9 +215,9 @@
// buttonAddTanker
//
buttonAddTanker.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddTanker.Location = new Point(16, 118);
buttonAddTanker.Location = new Point(13, 14);
buttonAddTanker.Name = "buttonAddTanker";
buttonAddTanker.Size = new Size(226, 55);
buttonAddTanker.Size = new Size(230, 43);
buttonAddTanker.TabIndex = 1;
buttonAddTanker.Text = "Добавление грузовика";
buttonAddTanker.UseVisualStyleBackColor = true;
@ -128,9 +229,9 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(16, 30);
comboBoxSelectorCompany.Location = new Point(16, 417);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(226, 33);
comboBoxSelectorCompany.Size = new Size(230, 33);
comboBoxSelectorCompany.TabIndex = 0;
comboBoxSelectorCompany.SelectedIndexChanged += comboBoxSelectorCompany_SelectedIndexChanged_1;
//
@ -139,22 +240,40 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(1095, 681);
pictureBox.Size = new Size(1222, 810);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddTanker);
panelCompanyTools.Controls.Add(buttonAddGasolineTanker);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(maskedTextBox1);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonDelTanker);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 456);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(252, 351);
panelCompanyTools.TabIndex = 2;
//
// FormTankerCollection
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1349, 681);
ClientSize = new Size(1480, 810);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormTankerCollection";
Text = "Коллекция Грузовиков";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
ResumeLayout(false);
}
@ -169,5 +288,15 @@
private Button buttonRefresh;
private Button buttonGoToCheck;
private Button buttonDelTanker;
private Panel panelStorage;
private TextBox textBoxCollectionName;
private Label labelCollectionName;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Panel panelCompanyTools;
}
}

View File

@ -14,6 +14,7 @@ namespace ProjectGasolineTanker;
public partial class FormTankerCollection : Form
{
private readonly StorageCollection<DrawingTanker> _storageCollection;
/// <summary>
/// Компания
/// </summary>
@ -25,6 +26,7 @@ public partial class FormTankerCollection : Form
public FormTankerCollection()
{
InitializeComponent();
_storageCollection = new();
}
/// <summary>
@ -34,12 +36,7 @@ public partial class FormTankerCollection : Form
/// <param name="e"></param>
private void comboBoxSelectorCompany_SelectedIndexChanged_1(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new TankerSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawingTanker>());
break;
}
panelCompanyTools.Enabled = false;
}
/// <summary>
@ -191,4 +188,77 @@ public partial class FormTankerCollection : Form
pictureBox.Image = _company.Show();
}
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 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 buttonCollectionDel_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedItems == null || listBoxCollection.SelectedIndex < 0)
{
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;
}
ICollectionGenericObjects<DrawingTanker>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new TankerSharingService(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RefreshListBoxItems();
}
}