WIP: ПИбд-12, Шляпин Юрий Александрович, 4 простая #4

Draft
yu.shlyapin wants to merge 2 commits from FourthLab into ThirdLab
6 changed files with 359 additions and 36 deletions
Showing only changes of commit 16fd1c55d9 - Show all commits

View File

@ -0,0 +1,8 @@
namespace AircraftCarrier.CollectionGenericObjects;
public enum CollectionType
{
None,
Massive,
List
}

View File

@ -0,0 +1,44 @@
namespace AircraftCarrier.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 < _collection.Count)) return null;
return _collection[position];
}
public bool Insert(T obj)
{
if (_collection.Count >= _maxCount) return false;
_collection.Add(obj);
return true;
}
public bool Insert(T obj, int position)
{
if ((_collection.Count >= _maxCount) || !(position >= 0 && position < _collection.Count)) return false;
_collection.Insert(position, obj);
return true;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < _collection.Count)) return false;
_collection.RemoveAt(position);
return true;
}
}

View File

@ -6,7 +6,20 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
private T?[] _collection;
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];
}
}
}
}
public MassiveGenericObjects()
{

View File

@ -0,0 +1,44 @@
using AircraftCarrier.Drawing;
namespace AircraftCarrier.CollectionGenericObjects;
public class StorageColletion<T>
where T : class
{
private Dictionary<string, ICollectionGenericObjects<T>> _storage;
public List<string> Keys => _storage.Keys.ToList();
public StorageColletion()
{
_storage = new Dictionary<string, ICollectionGenericObjects<T>>();
}
public void AddCollection(string name, CollectionType type)
{
if (_storage == null || _storage.ContainsKey(name)) return;
switch (type)
{
case CollectionType.Massive:
_storage.Add(name, new MassiveGenericObjects<T>());
break;
case CollectionType.List:
_storage.Add(name, new ListGenericObjects<T>());
break;
default: return;
}
}
public void DelCollection(string? name)
{
if (name == null) return;
if (_storage != null && _storage.ContainsKey(name)) _storage.Remove(name);
}
public ICollectionGenericObjects<T>? this[string name]
{
get {
if (_storage.ContainsKey(name)) return _storage.GetValueOrDefault(name);
return null;
}
}
}

View File

@ -29,6 +29,15 @@
private void InitializeComponent()
{
groupBox1 = new GroupBox();
panel1 = new Panel();
buttonDelCollection = new Button();
textBoxNameCollection = new TextBox();
buttonCreateCollection = new Button();
listBoxCollection = new ListBox();
label1 = new Label();
radioButtonList = new RadioButton();
radioButtonMassive = new RadioButton();
buttonCreateCompany = new Button();
TextBoxIdAircraft = new MaskedTextBox();
buttonRefresh = new Button();
buttonTest = new Button();
@ -37,30 +46,124 @@
buttonCreateSimpleAircraft = new Button();
comboBoxStorage = new ComboBox();
pictureBox = new PictureBox();
panelTools = new Panel();
groupBox1.SuspendLayout();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
panelTools.SuspendLayout();
SuspendLayout();
//
// groupBox1
//
groupBox1.Controls.Add(TextBoxIdAircraft);
groupBox1.Controls.Add(buttonRefresh);
groupBox1.Controls.Add(buttonTest);
groupBox1.Controls.Add(buttonDeleteAircraft);
groupBox1.Controls.Add(buttonCreateAircraft);
groupBox1.Controls.Add(buttonCreateSimpleAircraft);
groupBox1.Controls.Add(buttonCreateCompany);
groupBox1.Controls.Add(comboBoxStorage);
groupBox1.Controls.Add(panel1);
groupBox1.Dock = DockStyle.Right;
groupBox1.Location = new Point(835, 0);
groupBox1.Location = new Point(905, 0);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(250, 638);
groupBox1.Size = new Size(250, 780);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// panel1
//
panel1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
panel1.Controls.Add(buttonDelCollection);
panel1.Controls.Add(textBoxNameCollection);
panel1.Controls.Add(buttonCreateCollection);
panel1.Controls.Add(listBoxCollection);
panel1.Controls.Add(label1);
panel1.Controls.Add(radioButtonList);
panel1.Controls.Add(radioButtonMassive);
panel1.Location = new Point(0, 26);
panel1.Name = "panel1";
panel1.Size = new Size(244, 320);
panel1.TabIndex = 7;
//
// buttonDelCollection
//
buttonDelCollection.Location = new Point(10, 281);
buttonDelCollection.Name = "buttonDelCollection";
buttonDelCollection.Size = new Size(217, 29);
buttonDelCollection.TabIndex = 6;
buttonDelCollection.Text = "Удалить Коллекцию";
buttonDelCollection.UseVisualStyleBackColor = true;
buttonDelCollection.Click += buttonDelCollection_Click;
//
// textBoxNameCollection
//
textBoxNameCollection.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
textBoxNameCollection.Location = new Point(10, 46);
textBoxNameCollection.Name = "textBoxNameCollection";
textBoxNameCollection.Size = new Size(223, 27);
textBoxNameCollection.TabIndex = 5;
//
// buttonCreateCollection
//
buttonCreateCollection.Location = new Point(10, 116);
buttonCreateCollection.Name = "buttonCreateCollection";
buttonCreateCollection.Size = new Size(217, 29);
buttonCreateCollection.TabIndex = 4;
buttonCreateCollection.Text = "Добавить Коллекцию";
buttonCreateCollection.UseVisualStyleBackColor = true;
buttonCreateCollection.Click += buttonCreateCollection_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.Location = new Point(10, 151);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(217, 124);
listBoxCollection.TabIndex = 3;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(39, 23);
label1.Name = "label1";
label1.Size = new Size(157, 20);
label1.TabIndex = 2;
label1.Text = "Название Коллекции";
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(147, 79);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(80, 24);
radioButtonList.TabIndex = 1;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(10, 79);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.RightToLeft = RightToLeft.No;
radioButtonMassive.Size = new Size(82, 24);
radioButtonMassive.TabIndex = 0;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// buttonCreateCompany
//
buttonCreateCompany.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
buttonCreateCompany.Location = new Point(14, 511);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(224, 30);
buttonCreateCompany.TabIndex = 8;
buttonCreateCompany.Text = "Создать Компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
buttonCreateCompany.Click += buttonCreateCompany_Click;
//
// TextBoxIdAircraft
//
TextBoxIdAircraft.Location = new Point(15, 221);
TextBoxIdAircraft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
TextBoxIdAircraft.Location = new Point(14, 80);
TextBoxIdAircraft.Mask = "00";
TextBoxIdAircraft.Name = "TextBoxIdAircraft";
TextBoxIdAircraft.Size = new Size(224, 27);
@ -68,9 +171,10 @@
//
// buttonRefresh
//
buttonRefresh.Location = new Point(14, 397);
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(14, 185);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(224, 48);
buttonRefresh.Size = new Size(224, 30);
buttonRefresh.TabIndex = 5;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
@ -78,9 +182,10 @@
//
// buttonTest
//
buttonTest.Location = new Point(15, 343);
buttonTest.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonTest.Location = new Point(14, 149);
buttonTest.Name = "buttonTest";
buttonTest.Size = new Size(224, 48);
buttonTest.Size = new Size(224, 30);
buttonTest.TabIndex = 4;
buttonTest.Text = "Тесты";
buttonTest.UseVisualStyleBackColor = true;
@ -88,9 +193,10 @@
//
// buttonDeleteAircraft
//
buttonDeleteAircraft.Location = new Point(15, 258);
buttonDeleteAircraft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDeleteAircraft.Location = new Point(14, 113);
buttonDeleteAircraft.Name = "buttonDeleteAircraft";
buttonDeleteAircraft.Size = new Size(224, 48);
buttonDeleteAircraft.Size = new Size(224, 30);
buttonDeleteAircraft.TabIndex = 3;
buttonDeleteAircraft.Text = "Удалить Авианосец";
buttonDeleteAircraft.UseVisualStyleBackColor = true;
@ -98,9 +204,10 @@
//
// buttonCreateAircraft
//
buttonCreateAircraft.Location = new Point(14, 139);
buttonCreateAircraft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonCreateAircraft.Location = new Point(14, 44);
buttonCreateAircraft.Name = "buttonCreateAircraft";
buttonCreateAircraft.Size = new Size(224, 48);
buttonCreateAircraft.Size = new Size(224, 30);
buttonCreateAircraft.TabIndex = 2;
buttonCreateAircraft.Text = "Создать супер Авианосец";
buttonCreateAircraft.UseVisualStyleBackColor = true;
@ -108,9 +215,10 @@
//
// buttonCreateSimpleAircraft
//
buttonCreateSimpleAircraft.Location = new Point(15, 85);
buttonCreateSimpleAircraft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonCreateSimpleAircraft.Location = new Point(14, 8);
buttonCreateSimpleAircraft.Name = "buttonCreateSimpleAircraft";
buttonCreateSimpleAircraft.Size = new Size(224, 48);
buttonCreateSimpleAircraft.Size = new Size(224, 30);
buttonCreateSimpleAircraft.TabIndex = 1;
buttonCreateSimpleAircraft.Text = "Создать Авианосец";
buttonCreateSimpleAircraft.UseVisualStyleBackColor = true;
@ -118,11 +226,11 @@
//
// comboBoxStorage
//
comboBoxStorage.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxStorage.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
comboBoxStorage.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStorage.FormattingEnabled = true;
comboBoxStorage.Items.AddRange(new object[] { "Хранилище" });
comboBoxStorage.Location = new Point(15, 30);
comboBoxStorage.Location = new Point(14, 477);
comboBoxStorage.Name = "comboBoxStorage";
comboBoxStorage.Size = new Size(224, 28);
comboBoxStorage.TabIndex = 0;
@ -133,22 +241,40 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(835, 638);
pictureBox.Size = new Size(905, 780);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// panelTools
//
panelTools.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
panelTools.Controls.Add(buttonCreateSimpleAircraft);
panelTools.Controls.Add(TextBoxIdAircraft);
panelTools.Controls.Add(buttonCreateAircraft);
panelTools.Controls.Add(buttonRefresh);
panelTools.Controls.Add(buttonDeleteAircraft);
panelTools.Controls.Add(buttonTest);
panelTools.Location = new Point(905, 554);
panelTools.Name = "panelTools";
panelTools.Size = new Size(250, 226);
panelTools.TabIndex = 9;
//
// FormShipCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1085, 638);
ClientSize = new Size(1155, 780);
Controls.Add(panelTools);
Controls.Add(pictureBox);
Controls.Add(groupBox1);
Name = "FormShipCollection";
Text = "FormShipCollection";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
panel1.ResumeLayout(false);
panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelTools.ResumeLayout(false);
panelTools.PerformLayout();
ResumeLayout(false);
}
@ -163,5 +289,16 @@
private Button buttonDeleteAircraft;
private Button buttonCreateAircraft;
private Button buttonCreateSimpleAircraft;
private Panel panel1;
private Label label1;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private Button buttonCreateCompany;
private Button buttonDelCollection;
private TextBox textBoxNameCollection;
private Button buttonCreateCollection;
private ListBox listBoxCollection;
private Panel panel2;
private Panel panelTools;
}
}

View File

@ -4,26 +4,19 @@ namespace AircraftCarrier.CollectionGenericObjects;
public partial class FormShipCollection : Form
{
private StorageColletion<DrawingSimpleAircraftCarrier> _storageCollection;
private AbstractCompany? _company = null;
public FormShipCollection()
{
InitializeComponent();
_storageCollection = new();
panelTools.Enabled = false;
}
private void comboBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxStorage.Text)
{
case "Хранилище":
_company = new DocksService(
pictureBox.Width,
pictureBox.Height,
new MassiveGenericObjects<DrawingSimpleAircraftCarrier>()
);
break;
}
_company.Show();
panelTools.Enabled = false;
}
private static Color GetColor(Random random)
@ -122,4 +115,88 @@ public partial class FormShipCollection : Form
if (_company == null) return;
pictureBox.Image = _company.Show();
}
private void buttonCreateCollection_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxNameCollection.Text) | (!radioButtonList.Checked && !radioButtonMassive.Checked))
{
MessageBox.Show("Некорректный набор данных");
return;
}
CollectionType collectionType = CollectionType.Massive;
if (radioButtonMassive.Checked)
{
collectionType = CollectionType.Massive;
}
else if (radioButtonList.Checked)
{
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxNameCollection.Text, collectionType);
RefreshListBoxItem();
}
private void RefreshListBoxItem()
{
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 buttonDelCollection_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedItems.Count == 0)
{
MessageBox.Show("Нет выбранных элементов");
return;
}
if (MessageBox.Show(
"вы уверены, что хотите удалить " + listBoxCollection.SelectedItem?.ToString(),
"Предупреждение",
MessageBoxButtons.YesNo
) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem?.ToString());
RefreshListBoxItem();
}
private void buttonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedItems.Count == 0)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawingSimpleAircraftCarrier>? collections;
collections = _storageCollection[listBoxCollection.SelectedItem?.ToString() ?? string.Empty];
if (collections == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxStorage.Text)
{
case "Хранилище":
_company = new DocksService(
pictureBox.Width,
pictureBox.Height,
collections
);
break;
}
pictureBox.Image = _company?.Show();
panelTools.Enabled = true;
}
}