половина 4 лабы

This commit is contained in:
Petek1234 2024-04-03 11:53:35 +04:00
parent e3253aeb74
commit c81a717269
8 changed files with 345 additions and 50 deletions

View File

@ -63,9 +63,9 @@ public abstract class AbstractCompany
/// <param name="company">Компания</param>
/// <param name="car">Добавляемый объект</param>
/// <returns></returns>
public static bool operator +(AbstractCompany company, DrawningB B)
public static int operator +(AbstractCompany company, DrawningB B)
{
return company._collection?.Insert(B) ?? false;
return company._collection?.Insert(B) ?? -1;
}
/// <summary>
@ -74,9 +74,9 @@ public abstract class AbstractCompany
/// <param name="company">Компания</param>
/// <param name="position">Номер удаляемого объекта</param>
/// <returns></returns>
public static bool operator -(AbstractCompany company, int position)
public static DrawningB? operator -(AbstractCompany company, int position)
{
return company._collection?.Remove(position) ?? false;
return company._collection?.Remove(position) ?? null;
}
/// <summary>

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MotorBoat.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)
{
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
// TODO Прописать логику для добавления
if (string.IsNullOrEmpty(name) || _storages.ContainsKey(name))
{
return;
}
switch (collectionType)
{
case CollectionType.Massive:
_storages[name] = new MassiveGenericObjects<T>();
break;
case CollectionType.List:
_storages[name] = new ListGenericObjects<T>();
break;
default:
return;
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
// TODO Прописать логику для удаления коллекции
if (_storages.ContainsKey(name))
{
_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

@ -37,23 +37,35 @@
buttonAddBoat = new Button();
comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox();
panelStorage = new Panel();
labelCollectionName = new Label();
textBoxCollectionName = new TextBox();
radioButtonMassive = new RadioButton();
radioButtonList = new RadioButton();
buttonCollectionAdd = new Button();
listBoxCollection = new ListBox();
buttonCollectionDel = new Button();
buttonCreateCompany = new Button();
groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
panelStorage.SuspendLayout();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonRemoveBoat);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddMotorBoat);
groupBoxTools.Controls.Add(buttonAddBoat);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Controls.Add(buttonAddBoat);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(489, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(194, 411);
groupBoxTools.Size = new Size(194, 497);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
@ -61,7 +73,7 @@
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(3, 320);
buttonRefresh.Location = new Point(6, 464);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(185, 27);
buttonRefresh.TabIndex = 6;
@ -72,7 +84,7 @@
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(3, 260);
buttonGoToCheck.Location = new Point(6, 431);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(185, 27);
buttonGoToCheck.TabIndex = 5;
@ -83,7 +95,7 @@
// buttonRemoveBoat
//
buttonRemoveBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveBoat.Location = new Point(3, 206);
buttonRemoveBoat.Location = new Point(6, 398);
buttonRemoveBoat.Name = "buttonRemoveBoat";
buttonRemoveBoat.Size = new Size(185, 27);
buttonRemoveBoat.TabIndex = 4;
@ -93,7 +105,7 @@
//
// maskedTextBox
//
maskedTextBox.Location = new Point(3, 177);
maskedTextBox.Location = new Point(6, 369);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(185, 23);
@ -103,7 +115,7 @@
// buttonAddMotorBoat
//
buttonAddMotorBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddMotorBoat.Location = new Point(3, 113);
buttonAddMotorBoat.Location = new Point(6, 325);
buttonAddMotorBoat.Name = "buttonAddMotorBoat";
buttonAddMotorBoat.Size = new Size(185, 38);
buttonAddMotorBoat.TabIndex = 2;
@ -114,7 +126,7 @@
// buttonAddBoat
//
buttonAddBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddBoat.Location = new Point(3, 69);
buttonAddBoat.Location = new Point(6, 281);
buttonAddBoat.Name = "buttonAddBoat";
buttonAddBoat.Size = new Size(185, 38);
buttonAddBoat.TabIndex = 1;
@ -128,7 +140,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(3, 22);
comboBoxSelectorCompany.Location = new Point(6, 228);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(185, 23);
comboBoxSelectorCompany.TabIndex = 0;
@ -139,15 +151,104 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(489, 411);
pictureBox.Size = new Size(489, 497);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// 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(188, 206);
panelStorage.TabIndex = 7;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(29, 9);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(135, 15);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 27);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(182, 23);
textBoxCollectionName.TabIndex = 1;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(17, 56);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(69, 19);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(97, 56);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(67, 19);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 81);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(182, 23);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(3, 110);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(182, 64);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 180);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(182, 23);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 252);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(185, 23);
buttonCreateCompany.TabIndex = 8;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
//
// FormBoatCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(683, 411);
ClientSize = new Size(683, 497);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormBoatCollection";
@ -155,6 +256,8 @@
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
ResumeLayout(false);
}
@ -169,5 +272,14 @@
private MaskedTextBox maskedTextBox;
private Button buttonRefresh;
private Button buttonGoToCheck;
private Panel panelStorage;
private Label labelCollectionName;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Button buttonCreateCompany;
private Button buttonCollectionDel;
}
}

View File

@ -71,7 +71,7 @@ public partial class FormBoatCollection : Form
return;
}
if (_company + drawningB)
if (_company + drawningB != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
@ -110,7 +110,7 @@ public partial class FormBoatCollection : Form
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
int pos = Convert.ToInt32(maskedTextBox.Text);
if (_company - pos)
if (_company - pos != null)
{
MessageBox.Show("Объект удалён");
pictureBox.Image = _company.Show();