4 лаб работа

This commit is contained in:
Anastasia Yazykova 2024-04-09 22:45:40 +04:00
parent 0d786c5297
commit 0b855fe3a0
7 changed files with 483 additions and 79 deletions

View File

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

View File

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

View File

@ -16,7 +16,23 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
public int Count => _collection.Length; public int Count => _collection.Length;
public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } public int SetMaxCount
{
set
{
if (value > 0)
{
if (_collection.Length > 0)
{
Array.Resize(ref _collection, value);
}
else
{
_collection = new T?[value];
}
}
}
}
/// <summary> /// <summary>
/// Конструктор /// Конструктор

View File

@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TrolleybusProject.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 (_storages.ContainsKey(name) && name == "")
{
return;
}
if (collectionType == CollectionType.Massive)
{
_storages[name] = new MassiveGenericObjects<T>();
}
else
{
_storages[name] = new ListGenericObjects<T>();
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
_storages.Remove(name);
}
/// <summary>
/// Доступ к коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
/// <returns></returns>
public ICollectionGenericObjects<T>? this[string name]
{
get
{
if (name == "")
{
return null;
}
return _storages[name];
}
}
}

View File

@ -29,26 +29,35 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
panelCompanyTools = new Panel();
buttonCreateCompany = new Button();
buttonRefresh = new Button(); buttonRefresh = new Button();
buttonGoToCheck = new Button();
buttonDelTrolleybus = new Button();
maskedTextBox = new MaskedTextBox();
buttonAddTrolleybus = new Button();
buttonAddBus = new Button();
comboBoxSelectionCompany = new ComboBox(); comboBoxSelectionCompany = new ComboBox();
buttonGoToCheck = new Button();
buttonAddBus = new Button();
buttonDelTrolleybus = new Button();
buttonAddTrolleybus = new Button();
maskedTextBox = new MaskedTextBox();
panelStorage = new Panel();
buttonCollectionDel = new Button();
listBoxCollection = new ListBox();
buttonCollectionAdd = new Button();
radioButtonList = new RadioButton();
radioButtonMassive = new RadioButton();
textBoxCollectionName = new TextBox();
labelCollectionName = new Label();
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(buttonCreateCompany);
groupBoxTools.Controls.Add(buttonGoToCheck); groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonDelTrolleybus); groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddTrolleybus);
groupBoxTools.Controls.Add(buttonAddBus);
groupBoxTools.Controls.Add(comboBoxSelectionCompany); groupBoxTools.Controls.Add(comboBoxSelectionCompany);
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(904, 0); groupBoxTools.Location = new Point(904, 0);
@ -58,82 +67,187 @@
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты"; groupBoxTools.Text = "Инструменты";
// //
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonAddBus);
panelCompanyTools.Controls.Add(buttonDelTrolleybus);
panelCompanyTools.Controls.Add(buttonAddTrolleybus);
panelCompanyTools.Controls.Add(maskedTextBox);
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(0, 397);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(294, 259);
panelCompanyTools.TabIndex = 9;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(63, 345);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(187, 34);
buttonCreateCompany.TabIndex = 8;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
buttonCreateCompany.Click += buttonCreateCompany_Click;
//
// buttonRefresh // buttonRefresh
// //
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(20, 485); buttonRefresh.Location = new Point(24, 215);
buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(249, 34); buttonRefresh.Size = new Size(246, 34);
buttonRefresh.TabIndex = 6; buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить"; buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += buttonRefresh_Click; buttonRefresh.Click += buttonRefresh_Click;
// //
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(20, 395);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(249, 34);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click;
//
// buttonDelTrolleybus
//
buttonDelTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDelTrolleybus.Location = new Point(40, 278);
buttonDelTrolleybus.Name = "buttonDelTrolleybus";
buttonDelTrolleybus.Size = new Size(217, 34);
buttonDelTrolleybus.TabIndex = 4;
buttonDelTrolleybus.Text = "Удалить автобус";
buttonDelTrolleybus.UseVisualStyleBackColor = true;
buttonDelTrolleybus.Click += buttonDelTrolleybus_Click;
//
// maskedTextBox
//
maskedTextBox.Location = new Point(73, 227);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(150, 31);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
//
// buttonAddTrolleybus
//
buttonAddTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddTrolleybus.Location = new Point(19, 165);
buttonAddTrolleybus.Name = "buttonAddTrolleybus";
buttonAddTrolleybus.Size = new Size(249, 34);
buttonAddTrolleybus.TabIndex = 2;
buttonAddTrolleybus.Text = "Добавление троллейбуса";
buttonAddTrolleybus.UseVisualStyleBackColor = true;
buttonAddTrolleybus.Click += buttonAddTrolleybus_Click;
//
// buttonAddBus
//
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddBus.Location = new Point(20, 97);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(249, 34);
buttonAddBus.TabIndex = 1;
buttonAddBus.Text = "Добавление автобуса";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += buttonAddBus_Click;
//
// comboBoxSelectionCompany // comboBoxSelectionCompany
// //
comboBoxSelectionCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; comboBoxSelectionCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxSelectionCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectionCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectionCompany.FormattingEnabled = true; comboBoxSelectionCompany.FormattingEnabled = true;
comboBoxSelectionCompany.Items.AddRange(new object[] { "Хранилище" }); comboBoxSelectionCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectionCompany.Location = new Point(19, 30); comboBoxSelectionCompany.Location = new Point(26, 306);
comboBoxSelectionCompany.Name = "comboBoxSelectionCompany"; comboBoxSelectionCompany.Name = "comboBoxSelectionCompany";
comboBoxSelectionCompany.Size = new Size(250, 33); comboBoxSelectionCompany.Size = new Size(244, 33);
comboBoxSelectionCompany.TabIndex = 0; comboBoxSelectionCompany.TabIndex = 0;
comboBoxSelectionCompany.SelectedIndexChanged += comboBoxSelectionCompany_SelectedIndexChanged; comboBoxSelectionCompany.SelectedIndexChanged += comboBoxSelectionCompany_SelectedIndexChanged;
// //
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(24, 175);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(246, 34);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click;
//
// buttonAddBus
//
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddBus.Location = new Point(26, 18);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(244, 34);
buttonAddBus.TabIndex = 1;
buttonAddBus.Text = "Добавление автобуса";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += buttonAddBus_Click;
//
// buttonDelTrolleybus
//
buttonDelTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDelTrolleybus.Location = new Point(22, 135);
buttonDelTrolleybus.Name = "buttonDelTrolleybus";
buttonDelTrolleybus.Size = new Size(244, 34);
buttonDelTrolleybus.TabIndex = 4;
buttonDelTrolleybus.Text = "Удалить автобус";
buttonDelTrolleybus.UseVisualStyleBackColor = true;
buttonDelTrolleybus.Click += buttonDelTrolleybus_Click;
//
// buttonAddTrolleybus
//
buttonAddTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddTrolleybus.Location = new Point(24, 58);
buttonAddTrolleybus.Name = "buttonAddTrolleybus";
buttonAddTrolleybus.Size = new Size(246, 34);
buttonAddTrolleybus.TabIndex = 2;
buttonAddTrolleybus.Text = "Добавление троллейбуса";
buttonAddTrolleybus.UseVisualStyleBackColor = true;
buttonAddTrolleybus.Click += buttonAddTrolleybus_Click;
//
// maskedTextBox
//
maskedTextBox.Location = new Point(68, 98);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(150, 31);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
//
// 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(294, 273);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(8, 225);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(277, 34);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += buttonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.ItemHeight = 25;
listBoxCollection.Location = new Point(11, 140);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(274, 79);
listBoxCollection.TabIndex = 7;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(8, 100);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(277, 34);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += buttonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(171, 65);
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(11, 65);
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(3, 28);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(288, 31);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(65, 0);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(182, 25);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции";
//
// pictureBox // pictureBox
// //
pictureBox.Dock = DockStyle.Fill; pictureBox.Dock = DockStyle.Fill;
@ -153,7 +267,10 @@
Name = "FormBusCollection"; Name = "FormBusCollection";
Text = "Коллекция автобусов"; Text = "Коллекция автобусов";
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);
} }
@ -169,5 +286,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 RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
private Panel panelCompanyTools;
} }
} }

View File

@ -14,14 +14,25 @@ namespace TrolleybusProject;
public partial class FormBusCollection : Form public partial class FormBusCollection : Form
{ { /// <summary>
/// Хранилише коллекций
/// </summary>
private readonly StorageCollection<DrawningBus> _storageCollection;
/// <summary>
/// Компания
/// </summary>
private AbstractCompany? _company = null; private AbstractCompany? _company = null;
public FormBusCollection() public FormBusCollection()
{ {
InitializeComponent(); InitializeComponent();
_storageCollection = new();
} }
private ListBox GetListBoxCollection()
{
return listBoxCollection;
}
private void CreateObject(string type) private void CreateObject(string type)
{ {
if (_company == null) if (_company == null)
@ -59,13 +70,7 @@ public partial class FormBusCollection : Form
private void comboBoxSelectionCompany_SelectedIndexChanged(object sender, EventArgs e) private void comboBoxSelectionCompany_SelectedIndexChanged(object sender, EventArgs e)
{ {
switch (comboBoxSelectionCompany.Text) panelCompanyTools.Enabled = false;
{
case "Хранилище":
_company = new BusStation(pictureBox.Width,
pictureBox.Height, new MassiveGenericObjects<DrawningBus>());
break;
}
} }
@ -141,5 +146,89 @@ public partial class FormBusCollection : Form
} }
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>
/// Обновление списка в 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);
}
}
}
private void buttonCollectionDel_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedItem == null) return;
if (listBoxCollection.SelectedIndex < 0 )
{
MessageBox.Show("Коллекция не выбрана");
return;
} }
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
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<DrawningBus>? collection =
_storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectionCompany.Text)
{
case "Хранилище":
_company = new BusStation(pictureBox.Width,
pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
}