LabWork04

This commit is contained in:
cleverman1337 2024-05-16 09:36:27 +04:00
parent 87a93c30f8
commit 6dcd4ac6d9
7 changed files with 416 additions and 48 deletions

View File

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

View File

@ -36,7 +36,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="obj">Добавляемый объект</param>
/// <param name="position">Позиция</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj, int position);
int Insert(T obj, int position);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции

View File

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

View File

@ -62,32 +62,37 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return -1;
}
public bool Insert(T obj, int position)
public int Insert(T obj, int position)
{
if (position < 0 || position >= _collection.Length)
return false;
if (position < 0 || position >= _collection.Length) { return position; }
if (_collection[position] == null)
{
_collection[position] = obj;
return true;
return position;
}
for (int i = position; i < _collection.Length; i++)
else
{
if (_collection[i] == null)
for (int i = position + 1; i < _collection.Length; i++)
{
_collection[i] = obj;
return true;
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
for (int i = position - 1; i >= 0; i--)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
}
for (int i = 0; i < position; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
}
return false;
return -1;
}
public T? Remove(int position)

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SelfPropelledArtilleryUnit.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 (string.IsNullOrEmpty(name)) return;
if (_storages.ContainsKey(name)) return;
if (collectionType == CollectionType.None) return;
if (collectionType == CollectionType.Massive)
{
_storages[name] = new MassiveGenericObjects<T>();
}
else if (collectionType == CollectionType.List)
{
_storages[name] = new ListGenericObjects<T>();
}
}
/// <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 (_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();
buttonGoToCheak = new Button();
buttonRemoveTank = new Button();
@ -37,18 +46,18 @@
buttonAddArtillery = 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(buttonGoToCheak);
groupBoxTools.Controls.Add(buttonRemoveTank);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddTank);
groupBoxTools.Controls.Add(buttonAddArtillery);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(715, 0);
@ -58,12 +67,104 @@
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(5, 267);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(189, 24);
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, 19);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(194, 213);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(5, 186);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(189, 24);
buttonCollectionDel.TabIndex = 7;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(6, 115);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(185, 64);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(2, 81);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(189, 24);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(78, 57);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(66, 19);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(5, 57);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(67, 19);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 30);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(188, 23);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(25, 12);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(125, 15);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(12, 423);
buttonRefresh.Location = new Point(12, 174);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(176, 41);
buttonRefresh.Size = new Size(170, 23);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
@ -72,9 +173,9 @@
// buttonGoToCheak
//
buttonGoToCheak.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheak.Location = new Point(12, 337);
buttonGoToCheak.Location = new Point(12, 143);
buttonGoToCheak.Name = "buttonGoToCheak";
buttonGoToCheak.Size = new Size(176, 41);
buttonGoToCheak.Size = new Size(170, 25);
buttonGoToCheak.TabIndex = 5;
buttonGoToCheak.Text = "Передать на тесты";
buttonGoToCheak.UseVisualStyleBackColor = true;
@ -83,9 +184,9 @@
// buttonRemoveTank
//
buttonRemoveTank.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveTank.Location = new Point(12, 215);
buttonRemoveTank.Location = new Point(12, 110);
buttonRemoveTank.Name = "buttonRemoveTank";
buttonRemoveTank.Size = new Size(176, 41);
buttonRemoveTank.Size = new Size(170, 27);
buttonRemoveTank.TabIndex = 4;
buttonRemoveTank.Text = "Удалить танк";
buttonRemoveTank.UseVisualStyleBackColor = true;
@ -93,7 +194,7 @@
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(12, 186);
maskedTextBoxPosition.Location = new Point(12, 82);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(176, 23);
@ -103,9 +204,9 @@
// buttonAddTank
//
buttonAddTank.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddTank.Location = new Point(12, 82);
buttonAddTank.Location = new Point(12, 9);
buttonAddTank.Name = "buttonAddTank";
buttonAddTank.Size = new Size(176, 41);
buttonAddTank.Size = new Size(170, 30);
buttonAddTank.TabIndex = 2;
buttonAddTank.Text = "Добавление танка";
buttonAddTank.UseVisualStyleBackColor = true;
@ -114,9 +215,9 @@
// buttonAddArtillery
//
buttonAddArtillery.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddArtillery.Location = new Point(12, 129);
buttonAddArtillery.Location = new Point(12, 45);
buttonAddArtillery.Name = "buttonAddArtillery";
buttonAddArtillery.Size = new Size(176, 41);
buttonAddArtillery.Size = new Size(170, 31);
buttonAddArtillery.TabIndex = 1;
buttonAddArtillery.Text = "Добавление сау";
buttonAddArtillery.UseVisualStyleBackColor = true;
@ -128,7 +229,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(12, 33);
comboBoxSelectorCompany.Location = new Point(6, 238);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(182, 23);
comboBoxSelectorCompany.TabIndex = 0;
@ -143,6 +244,21 @@
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddTank);
panelCompanyTools.Controls.Add(buttonAddArtillery);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Controls.Add(buttonGoToCheak);
panelCompanyTools.Controls.Add(buttonRemoveTank);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 300);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(194, 205);
panelCompanyTools.TabIndex = 9;
//
// FormTankCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -153,8 +269,11 @@
Name = "FormTankCollection";
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 buttonRemoveTank;
private Button buttonRefresh;
private Button buttonGoToCheak;
private Panel panelStorage;
private TextBox textBoxCollectionName;
private Label labelCollectionName;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
private Panel panelCompanyTools;
}
}

View File

@ -11,6 +11,7 @@ public partial class FormTankCollection : Form
/// Компания
/// </summary>
private AbstractCompany? _company = null;
private readonly StorageCollection<DrawningTank> _storageCollection;
/// <summary>
/// Конструктор
@ -18,6 +19,7 @@ public partial class FormTankCollection : Form
public FormTankCollection()
{
InitializeComponent();
_storageCollection = new();
}
/// <summary>
/// Выбор компании
@ -28,12 +30,7 @@ public partial class FormTankCollection : Form
private void comboBoxSelectorCompany_SelectedIndexChanged_1(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new TankSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningTank>());
break;
}
panelCompanyTools.Enabled = false;
}
private void CreateObject(string type)
@ -51,17 +48,14 @@ public partial class FormTankCollection : Form
drawningTank = new DrawningTank(random.Next(100, 300), random.Next(1000, 3000), GetColor(random));
break;
case nameof(DrawningArtillery):
// TODO вызов диалогового окна для выбора цвета
drawningTank = new DrawningArtillery(random.Next(100, 300), random.Next(1000, 3000),
GetColor(random),
GetColor(random),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
GetColor(random),GetColor(random),Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
break;
default:
return;
}
if (_company + drawningTank !=-1)
if (_company + drawningTank != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
@ -163,4 +157,79 @@ public partial class FormTankCollection : 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);
RerfreshListBoxItems();
}
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)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
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<DrawningTank>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new TankSharingService(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
}