PIbd-13 Kobin V.O. LabWork04 Simple #4

Closed
vkobi wants to merge 1 commits from LabWork04 into LabWork03
5 changed files with 438 additions and 50 deletions

View File

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

View File

@ -0,0 +1,82 @@
namespace WarmlyLocomotive.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)
{
// TODO проверка позиции
if (position >= Count || position < 0)
{
return null;
}
return _collection[position];
}
public int Insert(T obj)
{
// TODO проверка, что не превышено максимальное количество элементов
if (Count == _maxCount)
{
return -1;
}
// TODO вставка в конец набора
_collection.Add(obj);
return _collection.Count;
}
public int Insert(T obj, int position)
{
// TODO проверка, что не превышено максимальное количество элементов
if (Count == _maxCount)
{
return -1;
}
// TODO проверка позиции
if (position >= Count || position < 0)
{
return -1;
}
// TODO вставка по позиции
_collection.Insert(position, obj);
return position;
}
public T? Remove(int position)
{
// TODO проверка позиции
if (position >= Count || position < 0)
{
return null;
}
// TODO удаление объекта из списка
T? obj = _collection[position];
_collection.RemoveAt(position);
return obj;
}
}

View File

@ -0,0 +1,83 @@
namespace WarmlyLocomotive.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)
{
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
if (name == null || _storages.ContainsKey(name))
{
return;
}
// TODO Прописать логику для добавления
if (collectionType == CollectionType.List)
{
_storages.Add(name, new ListGenericObjects<T>());
}
if (collectionType == CollectionType.Massive)
{
_storages.Add(name, new MassiveGenericObjects<T>());
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
if (name == null || !_storages.ContainsKey(name))
{
return;
}
// TODO Прописать логику для удаления коллекции
_storages.Remove(name);
}
/// <summary>
/// Доступ к коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
/// <returns></returns>
public ICollectionGenericObjects<T>? this[string name]
{
get
{
// TODO Продумать логику получения объекта
if (_storages.ContainsKey(name))
{
return _storages[name];
}
return null;
}
}
}

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();
buttonRemoveLocomotive = new Button();
@ -37,34 +46,125 @@
buttonAddLocomotive = 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(buttonRemoveLocomotive);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddWarmlyLocomotive);
groupBoxTools.Controls.Add(buttonAddLocomotive);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(808, 0);
groupBoxTools.Location = new Point(821, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(193, 527);
groupBoxTools.Size = new Size(193, 618);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
groupBoxTools.Enter += groupBoxTools_Enter;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 309);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(181, 29);
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, 23);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(187, 246);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 205);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(181, 29);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 20;
listBoxCollection.Location = new Point(6, 135);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(172, 64);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 95);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(181, 29);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(104, 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(3, 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(181, 27);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(20, 10);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(158, 20);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(6, 440);
buttonRefresh.Location = new Point(6, 231);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(175, 48);
buttonRefresh.Size = new Size(172, 34);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
@ -73,9 +173,9 @@
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 348);
buttonGoToCheck.Location = new Point(6, 195);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(175, 48);
buttonGoToCheck.Size = new Size(172, 33);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
@ -84,9 +184,9 @@
// buttonRemoveLocomotive
//
buttonRemoveLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveLocomotive.Location = new Point(6, 294);
buttonRemoveLocomotive.Location = new Point(6, 144);
buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
buttonRemoveLocomotive.Size = new Size(175, 48);
buttonRemoveLocomotive.Size = new Size(172, 48);
buttonRemoveLocomotive.TabIndex = 4;
buttonRemoveLocomotive.Text = "Удалить локомотив";
buttonRemoveLocomotive.UseVisualStyleBackColor = true;
@ -94,20 +194,19 @@
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(6, 234);
maskedTextBoxPosition.Location = new Point(6, 111);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(175, 27);
maskedTextBoxPosition.TabIndex = 3;
maskedTextBoxPosition.ValidatingType = typeof(int);
maskedTextBoxPosition.MaskInputRejected += maskedTextBoxPosition_MaskInputRejected;
//
// buttonAddWarmlyLocomotive
//
buttonAddWarmlyLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddWarmlyLocomotive.Location = new Point(6, 142);
buttonAddWarmlyLocomotive.Location = new Point(6, 57);
buttonAddWarmlyLocomotive.Name = "buttonAddWarmlyLocomotive";
buttonAddWarmlyLocomotive.Size = new Size(175, 48);
buttonAddWarmlyLocomotive.Size = new Size(172, 48);
buttonAddWarmlyLocomotive.TabIndex = 2;
buttonAddWarmlyLocomotive.Text = "Добавление локомотива";
buttonAddWarmlyLocomotive.UseVisualStyleBackColor = true;
@ -116,9 +215,9 @@
// buttonAddLocomotive
//
buttonAddLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddLocomotive.Location = new Point(6, 88);
buttonAddLocomotive.Location = new Point(6, 3);
buttonAddLocomotive.Name = "buttonAddLocomotive";
buttonAddLocomotive.Size = new Size(175, 48);
buttonAddLocomotive.Size = new Size(172, 48);
buttonAddLocomotive.TabIndex = 1;
buttonAddLocomotive.Text = "Добавление тепловоза";
buttonAddLocomotive.UseVisualStyleBackColor = true;
@ -130,7 +229,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Депо" });
comboBoxSelectorCompany.Location = new Point(6, 26);
comboBoxSelectorCompany.Location = new Point(6, 275);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(175, 28);
comboBoxSelectorCompany.TabIndex = 0;
@ -141,24 +240,40 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(808, 527);
pictureBox.Size = new Size(821, 618);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
pictureBox.Click += pictureBox_Click;
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddLocomotive);
panelCompanyTools.Controls.Add(buttonAddWarmlyLocomotive);
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonRemoveLocomotive);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 347);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(187, 268);
panelCompanyTools.TabIndex = 9;
//
// FormLocomotiveCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1001, 527);
ClientSize = new Size(1014, 618);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormLocomotiveCollection";
Text = "Коллекция локомотивов";
Load += FormLocomotiveCollection_Load;
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
ResumeLayout(false);
}
@ -173,5 +288,15 @@
private Button buttonGoToCheck;
private Button buttonRemoveLocomotive;
private MaskedTextBox maskedTextBoxPosition;
private Panel panelStorage;
private TextBox textBoxCollectionName;
private Label labelCollectionName;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private Button buttonCollectionAdd;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
}
}

View File

@ -8,6 +8,11 @@ namespace WarmlyLocomotive;
/// </summary>
public partial class FormLocomotiveCollection : Form
{
/// <summary>
/// Хранилише коллекций
/// </summary>
private readonly StorageCollection<DrawningLocomotive> _storageCollection;
/// <summary>
/// Компания
/// </summary>
@ -19,6 +24,7 @@ public partial class FormLocomotiveCollection : Form
public FormLocomotiveCollection()
{
InitializeComponent();
_storageCollection = new();
}
/// <summary>
@ -28,12 +34,7 @@ public partial class FormLocomotiveCollection : Form
/// <param name="e"></param>
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Депо":
_company = new LocomotiveSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningLocomotive>());
break;
}
panelCompanyTools.Enabled = false;
}
/// <summary>
@ -89,22 +90,6 @@ public partial class FormLocomotiveCollection : Form
return color;
}
private void pictureBox_Click(object sender, EventArgs e)
{
}
private void FormLocomotiveCollection_Load(object sender, EventArgs e)
{
}
private void groupBoxTools_Enter(object sender, EventArgs e)
{
}
private void ButtonAddWarmlyLocomotive_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarmlyLocomotive));
private void ButtonAddLocomotive_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive));
@ -180,8 +165,99 @@ public partial class FormLocomotiveCollection : Form
pictureBox.Image = _company.Show();
}
private void maskedTextBoxPosition_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
/// <summary>
/// Добавление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCollectionDel_Click(object sender, EventArgs e)
{
// TODO прописать логику удаления элемента из коллекции
// нужно убедиться, что есть выбранная коллекция
// спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись
// удалить и обновить ListBox
if (!radioButtonList.Checked && !radioButtonMassive.Checked || string.IsNullOrEmpty(textBoxCollectionName.Text))
{
return;
}
ICollectionGenericObjects<DrawningLocomotive>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RerfreshListBoxItems();
}
/// <summary>
/// Создание компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawningLocomotive>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Депо":
_company = new LocomotiveSharingService(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
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);
}
}
}
}