PI-12_Zaytsev_A.V_LabWork04_Base #7

Closed
tyxzo wants to merge 3 commits from lab4 into lab3
5 changed files with 462 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,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.CollectionGenericObjects;
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
{
private readonly List<T?> _collection;
private int _maxCount;
public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
public ListGenericObjects()
{
_collection = new();
}
public T? Get(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
return _collection[position];
}
public int Insert(T obj)
{
if (Count >= _maxCount)
{
return -1;
}
_collection.Add(obj);
return _collection.IndexOf(obj);
}
public int Insert(T obj, int position)
{
if (Count >= _maxCount || position < 0 || position >= _maxCount)
{
return -1;
}
if (position > Count && position < _maxCount)
{
return Insert(obj);
}
int copy_of_position = position - 1;
while (position < Count)
{
if (_collection[position] == null)
Review

Не требуется, просто вызов Insert, он не заменит, он вставит

Не требуется, просто вызов Insert, он не заменит, он вставит
{
_collection.Insert(position, obj);
return position;
}
position++;
}
while (copy_of_position > 0)
{
if (_collection[copy_of_position] == null)
{
_collection.Insert(copy_of_position, obj);
return copy_of_position;
}
copy_of_position--;
}
return -1;
}
public T? Remove(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
T? removed_object = Get(position);
_collection.RemoveAt(position);
return removed_object;
}
}

View File

@ -0,0 +1,72 @@
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)
{
if (_storages.ContainsKey(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()
{
groupBoxTools = new GroupBox();
buttonRefresh = new Button();
buttonGoToCheck = new Button();
buttonRemoveBus = new Button();
maskedTextBox = new MaskedTextBox();
buttonAddDoublrDeckerBus = new Button();
panelCompanyTools = new Panel();
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();
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(buttonRemoveBus);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddDoublrDeckerBus);
groupBoxTools.Controls.Add(buttonAddBus);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(870, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(200, 450);
groupBoxTools.Size = new Size(200, 626);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Tools";
//
// buttonRefresh
// panelCompanyTools
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(6, 390);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(188, 35);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Refresh";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
panelCompanyTools.Controls.Add(buttonAddBus);
panelCompanyTools.Controls.Add(buttonAddDoublrDeckerBus);
panelCompanyTools.Controls.Add(maskedTextBox);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonRemoveBus);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Dock = DockStyle.Bottom;
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;
buttonGoToCheck.Location = new Point(6, 312);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(188, 35);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Send to check";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += ButtonGoToCheck_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);
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddBus.Location = new Point(0, 13);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(191, 35);
buttonAddBus.TabIndex = 1;
buttonAddBus.Text = "Add bus";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += ButtonAddBus_Click;
//
// buttonAddDoublrDeckerBus
//
buttonAddDoublrDeckerBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddDoublrDeckerBus.Location = new Point(6, 126);
buttonAddDoublrDeckerBus.Location = new Point(0, 54);
buttonAddDoublrDeckerBus.Name = "buttonAddDoublrDeckerBus";
buttonAddDoublrDeckerBus.Size = new Size(188, 35);
buttonAddDoublrDeckerBus.Size = new Size(191, 35);
buttonAddDoublrDeckerBus.TabIndex = 2;
buttonAddDoublrDeckerBus.Text = "Add double-decker bus";
buttonAddDoublrDeckerBus.UseVisualStyleBackColor = true;
buttonAddDoublrDeckerBus.Click += ButtonAddDoublrDeckerBus_Click;
//
// buttonAddBus
// maskedTextBox
//
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddBus.Location = new Point(6, 85);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(188, 35);
buttonAddBus.TabIndex = 1;
buttonAddBus.Text = "Add bus";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += ButtonAddBus_Click;
maskedTextBox.Location = new Point(0, 95);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(188, 23);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
//
// 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.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectCompany.FormattingEnabled = true;
comboBoxSelectCompany.Items.AddRange(new object[] { "Storage" });
comboBoxSelectCompany.Location = new Point(6, 22);
comboBoxSelectCompany.Location = new Point(6, 267);
comboBoxSelectCompany.Name = "comboBoxSelectCompany";
comboBoxSelectCompany.Size = new Size(188, 23);
comboBoxSelectCompany.TabIndex = 0;
@ -138,7 +254,7 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(870, 450);
pictureBox.Size = new Size(870, 626);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
@ -146,13 +262,16 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1070, 450);
ClientSize = new Size(1070, 626);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormBusCollection";
Text = "Bus collection";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
ResumeLayout(false);
}
@ -168,5 +287,15 @@
private PictureBox pictureBox;
private Button buttonRefresh;
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 readonly StorageCollection<DrawingBus> _storageCollection;
/// <summary>
/// Конструктор
/// </summary>
public FormBusCollection()
{
InitializeComponent();
_storageCollection = new();
}
private void comboBoxSelectCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectCompany.Text)
{
case "Storage":
_company = new BusStation(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawingBus>());
break;
}
panelCompanyTools.Enabled = false;
}
private void CreateObject(string type)
@ -150,4 +148,86 @@ public partial class FormBusCollection : Form
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;
}
}