PIbd-13 Valiullin R.A.LabWork04 Simple #4

Closed
rinat wants to merge 1 commits from lab4 into lab3
5 changed files with 401 additions and 49 deletions

View File

@ -0,0 +1,13 @@
namespace WarmlyShip.CollectionGenericObjects;
/// <summary>
/// Тип коллекции
/// </summary>
public enum CollectionType
{
None = 0,
Massive = 1,
List = 2
}

View File

@ -0,0 +1,73 @@
namespace WarmlyShip.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 Count;
}
public int Insert(T obj, int position)
{
if (Count == _maxCount)
{
return -1;
}
if (position >= Count || position < 0)
{
return -1;
}
_collection.Insert(position, obj);
return position;
}
public T Remove(int position)
{
if (position >= Count || position < 0)
{
return null;
}
T obj = _collection[position];
_collection.RemoveAt(position);
return obj;
}
}

View File

@ -0,0 +1,53 @@
namespace WarmlyShip.CollectionGenericObjects;
public class StorageCollection<T> where T : class
{
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
public List<string> Keys => _storages.Keys.ToList();
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
}
public void AddCollection(string name, CollectionType collectionType)
{
if (_storages.ContainsKey(name) || collectionType == CollectionType.None)
{
return;
}
switch (collectionType)
{
case CollectionType.Massive:
_storages[name] = new MassiveGenericObjects<T>();
break;
case CollectionType.List:
_storages[name] = new ListGenericObjects<T>();
break;
}
}
public void DelCollection(string name)
{
if (_storages.ContainsKey(name) && name != null)
{
_storages.Remove(name);
}
}
public ICollectionGenericObjects<T>? this[string name]
{
get
{
if (_storages.ContainsKey(name))
{
return _storages[name];
}
return null;
}
}
}

View File

@ -29,26 +29,35 @@
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
panelCompanyTools = new Panel();
buttonAddShip = new Button();
buttonAddWarmlyShip = new Button();
buttonRefresh = new Button();
maskedTextBoxPosition = new MaskedTextBox();
buttonGoToCheck = new Button();
buttonRemoveShip = new Button();
maskedTextBoxPosition = new MaskedTextBox();
buttonAddWarmlyShip = new Button();
buttonAddShip = new Button();
buttonCreateCompany = new Button();
panelStoreage = new Panel();
buttonCollectionDel = new Button();
listBoxCollection = new ListBox();
buttonCollectionAdd = new Button();
radioButtonList = new RadioButton();
radioButtonMssive = new RadioButton();
textBoxCollectionName = new TextBox();
labelCollectionName = new Label();
comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox();
groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout();
panelStoreage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonRemoveShip);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddWarmlyShip);
groupBoxTools.Controls.Add(buttonAddShip);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStoreage);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(961, 0);
@ -58,23 +67,68 @@
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddShip);
panelCompanyTools.Controls.Add(buttonAddWarmlyShip);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonRemoveShip);
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(6, 292);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(178, 234);
panelCompanyTools.TabIndex = 4;
//
// buttonAddShip
//
buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddShip.Location = new Point(3, 3);
buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Size = new Size(166, 36);
buttonAddShip.TabIndex = 1;
buttonAddShip.Text = "Добавление корабля";
buttonAddShip.UseVisualStyleBackColor = true;
buttonAddShip.Click += ButtonAddShip_Click;
//
// buttonAddWarmlyShip
//
buttonAddWarmlyShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddWarmlyShip.Location = new Point(3, 45);
buttonAddWarmlyShip.Name = "buttonAddWarmlyShip";
buttonAddWarmlyShip.Size = new Size(166, 36);
buttonAddWarmlyShip.TabIndex = 2;
buttonAddWarmlyShip.Text = "Добавление парохода";
buttonAddWarmlyShip.UseVisualStyleBackColor = true;
buttonAddWarmlyShip.Click += ButtonAddWarmlyShip_Click;
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(6, 473);
buttonRefresh.Anchor = AnchorStyles.None;
buttonRefresh.Location = new Point(0, 198);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(175, 36);
buttonRefresh.Size = new Size(169, 28);
buttonRefresh.TabIndex = 7;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(3, 87);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(169, 23);
maskedTextBoxPosition.TabIndex = 4;
maskedTextBoxPosition.ValidatingType = typeof(int);
//
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(6, 290);
buttonGoToCheck.Location = new Point(3, 158);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(175, 36);
buttonGoToCheck.Size = new Size(166, 36);
buttonGoToCheck.TabIndex = 6;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
@ -83,49 +137,112 @@
// buttonRemoveShip
//
buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveShip.Location = new Point(6, 191);
buttonRemoveShip.Location = new Point(3, 116);
buttonRemoveShip.Name = "buttonRemoveShip";
buttonRemoveShip.Size = new Size(175, 36);
buttonRemoveShip.Size = new Size(166, 36);
buttonRemoveShip.TabIndex = 5;
buttonRemoveShip.Text = "Удаление судна";
buttonRemoveShip.UseVisualStyleBackColor = true;
buttonRemoveShip.Click += ButtonRemoveShip_Click;
//
// maskedTextBoxPosition
// buttonCreateCompany
//
maskedTextBoxPosition.Location = new Point(6, 152);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(181, 23);
maskedTextBoxPosition.TabIndex = 4;
maskedTextBoxPosition.ValidatingType = typeof(int);
buttonCreateCompany.Location = new Point(6, 263);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(175, 23);
buttonCreateCompany.TabIndex = 7;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = false;
buttonCreateCompany.Click += ButtonCreateCompany_Click;
//
// buttonAddWarmlyShip
// panelStoreage
//
buttonAddWarmlyShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddWarmlyShip.Location = new Point(6, 90);
buttonAddWarmlyShip.Name = "buttonAddWarmlyShip";
buttonAddWarmlyShip.Size = new Size(175, 36);
buttonAddWarmlyShip.TabIndex = 2;
buttonAddWarmlyShip.Text = "Добавление парохода";
buttonAddWarmlyShip.UseVisualStyleBackColor = true;
buttonAddWarmlyShip.Click += ButtonAddWarmlyShip_Click;
panelStoreage.Controls.Add(buttonCollectionDel);
panelStoreage.Controls.Add(listBoxCollection);
panelStoreage.Controls.Add(buttonCollectionAdd);
panelStoreage.Controls.Add(radioButtonList);
panelStoreage.Controls.Add(radioButtonMssive);
panelStoreage.Controls.Add(textBoxCollectionName);
panelStoreage.Controls.Add(labelCollectionName);
panelStoreage.Dock = DockStyle.Top;
panelStoreage.Location = new Point(3, 19);
panelStoreage.Name = "panelStoreage";
panelStoreage.Size = new Size(181, 213);
panelStoreage.TabIndex = 9;
//
// buttonAddShip
// buttonCollectionDel
//
buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddShip.Location = new Point(6, 48);
buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Size = new Size(175, 36);
buttonAddShip.TabIndex = 1;
buttonAddShip.Text = "Добавление корабля";
buttonAddShip.UseVisualStyleBackColor = true;
buttonAddShip.Click += ButtonAddShip_Click;
buttonCollectionDel.Location = new Point(0, 186);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(175, 23);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = false;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(3, 101);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(175, 79);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 72);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(175, 23);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = false;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(106, 47);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(66, 19);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMssive
//
radioButtonMssive.AutoSize = true;
radioButtonMssive.Location = new Point(3, 47);
radioButtonMssive.Name = "radioButtonMssive";
radioButtonMssive.Size = new Size(67, 19);
radioButtonMssive.TabIndex = 2;
radioButtonMssive.TabStop = true;
radioButtonMssive.Text = "Массив";
radioButtonMssive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 18);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(175, 23);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(28, 0);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(122, 15);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции";
//
// comboBoxSelectorCompany
//
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(6, 22);
comboBoxSelectorCompany.Location = new Point(6, 234);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(175, 23);
comboBoxSelectorCompany.TabIndex = 8;
@ -150,7 +267,10 @@
Name = "FormShipCollection";
Text = "Коллекция кораблей";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
panelStoreage.ResumeLayout(false);
panelStoreage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
ResumeLayout(false);
}
@ -166,5 +286,15 @@
private MaskedTextBox maskedTextBoxPosition;
private Button buttonRefresh;
private Button buttonGoToCheck;
private Panel panelStoreage;
private Label labelCollectionName;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMssive;
private TextBox textBoxCollectionName;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Panel panelCompanyTools;
}
}

View File

@ -17,17 +17,14 @@ namespace WarmlyShip
public FormShipCollection()
{
InitializeComponent();
_storageCollection = new();
}
private AbstractCompany? _company = null;
private readonly StorageCollection<DrawningShip> _storageCollection;
private void ComboBoxSelectorCompany_SelectedIndexChanged(Object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new PortForShips(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningShip>());
break;
}
panelCompanyTools.Enabled = false;
}
private void CreateObject(string type)
@ -138,7 +135,93 @@ namespace WarmlyShip
private void ButtonRefresh_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
pictureBox.Image = _company.Show();
}
private void ButtonCollectionAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMssive.Checked))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CollectionType collectionType = CollectionType.None;
if (radioButtonMssive.Checked)
{
collectionType = CollectionType.Massive;
}
else if (radioButtonList.Checked)
{
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
RefreshListBoxItems();
}
private void ButtonCollectionDel_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RefreshListBoxItems();
}
private void ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItems == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawningShip>? collection = _storageCollection[listBoxCollection.SelectedItem?.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new PortForShips(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RefreshListBoxItems();
}
private void RefreshListBoxItems()
{
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);
}
}
}
}
}