well done

This commit is contained in:
Baryshev Dmitry 2024-04-05 01:27:26 +04:00
parent e337dd9b06
commit 60e7881cb0
6 changed files with 522 additions and 96 deletions

View File

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

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectDumpTruck.CollectionGenericObject;
public class ListGenericObjects<T> : ICollectionGenericObject<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 _collection.Count;
}
public int Insert(T obj, int position)
{
if (Count == _maxCount || 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

@ -18,7 +18,23 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
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];
}
}
}
}
/// <summary>
/// Конструктор
@ -31,7 +47,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
public T? Get(int position)
{
// TODO проверка позиции
if (position < 0 || position > Count)
if (position < 0 || position >= Count)
{
return null;
}
@ -54,12 +70,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
public int Insert(T obj, int position)
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// ищется свободное место после этой позиции и идет вставка туда
// если нет после, ищем до
// TODO вставка
if (position < 0 || position > Count)
if (position < 0 || position >= Count)
{
return -1;
}
@ -75,7 +86,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
if (_collection[i] == null)
{
_collection[i] = obj;
return position;
return i;
}
}
@ -84,7 +95,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
if (_collection[i] == null)
{
_collection[i] = obj;
return position;
return i;
}
}

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectDumpTruck.CollectionGenericObject;
public class StorageCollection<T>
where T : class
{
/// <summary>
/// Словарь (хранилище) с коллекциями
/// </summary>
readonly Dictionary<string, ICollectionGenericObject<T>> _storages;
/// <summary>
/// Возвращение списка названий коллекций
/// </summary>
public List<string> Keys => _storages.Keys.ToList();
/// <summary>
/// Конструктор
/// </summary>
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObject<T>>();
}
/// <summary>
/// Добавление коллекции в хранилище
/// </summary>
/// <param name="name">Название коллекции</param>
/// <param name="collectionType">Тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType)
{
if (name == null || _storages.ContainsKey(name))
{
return;
}
if (collectionType == CollectionType.Massive)
{
_storages.Add(name, new MassiveGenericObjects<T>());
}
if (collectionType == CollectionType.List)
{
_storages.Add(name, new ListGenericObjects<T>());
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
if (name == null || !_storages.ContainsKey(name))
{
return;
}
_storages.Remove(name);
}
/// <summary>
/// Доступ к коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
/// <returns></returns>
public ICollectionGenericObject<T>? this[string name]
{
get
{
if (_storages.ContainsKey(name))
{
return _storages[name];
}
return null;
}
}
}

View File

@ -29,27 +29,36 @@
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
buttonRefresh = new Button();
buttonGoToCheck = new Button();
buttonRemoveTruck = new Button();
maskedTextBoxPosition = new MaskedTextBox();
buttonAddDumpTruck = new Button();
buttonAddTruck = new Button();
buttonCreteCompany = new Button();
comboBoxSelectorCompany = new ComboBox();
panelStorage = new Panel();
buttonCollectionDel = new Button();
listBoxCollection = new ListBox();
buttonCollectionAdd = new Button();
radioButtonList = new RadioButton();
radioButtonMassive = new RadioButton();
textBoxCollectionName = new TextBox();
labelCollectionalName = new Label();
panelCompanyTools = new Panel();
maskedTextBoxPosition = new MaskedTextBox();
buttonRefresh = new Button();
buttonAddTruck = new Button();
buttonGoToCheck = new Button();
buttonAddDumpTruck = new Button();
buttonRemoveTruck = new Button();
pictureBox = new PictureBox();
groupBoxTools.SuspendLayout();
panelStorage.SuspendLayout();
panelCompanyTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonRemoveTruck);
groupBoxTools.Controls.Add(maskedTextBoxPosition);
groupBoxTools.Controls.Add(buttonAddDumpTruck);
groupBoxTools.Controls.Add(buttonAddTruck);
groupBoxTools.Controls.Add(buttonCreteCompany);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(888, 0);
groupBoxTools.Name = "groupBoxTools";
@ -58,64 +67,15 @@
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// buttonRefresh
// buttonCreteCompany
//
buttonRefresh.Location = new Point(6, 517);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(226, 48);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
//
// buttonGoToCheck
//
buttonGoToCheck.Location = new Point(6, 360);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(226, 48);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тест";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += ButtonGoToCheck_Click;
//
// buttonRemoveTruck
//
buttonRemoveTruck.Location = new Point(6, 258);
buttonRemoveTruck.Name = "buttonRemoveTruck";
buttonRemoveTruck.Size = new Size(226, 48);
buttonRemoveTruck.TabIndex = 4;
buttonRemoveTruck.Text = "Удаление\r\n";
buttonRemoveTruck.UseVisualStyleBackColor = true;
buttonRemoveTruck.Click += ButtonRemoveTruck_Click;
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(6, 225);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(226, 27);
maskedTextBoxPosition.TabIndex = 3;
maskedTextBoxPosition.ValidatingType = typeof(int);
//
// buttonAddDumpTruck
//
buttonAddDumpTruck.Location = new Point(6, 150);
buttonAddDumpTruck.Name = "buttonAddDumpTruck";
buttonAddDumpTruck.Size = new Size(226, 48);
buttonAddDumpTruck.TabIndex = 2;
buttonAddDumpTruck.Text = "Добавление самосвала";
buttonAddDumpTruck.UseVisualStyleBackColor = true;
buttonAddDumpTruck.Click += ButtonAddDumpTruck_Click;
//
// buttonAddTruck
//
buttonAddTruck.Location = new Point(6, 96);
buttonAddTruck.Name = "buttonAddTruck";
buttonAddTruck.Size = new Size(226, 48);
buttonAddTruck.TabIndex = 1;
buttonAddTruck.Text = "Добавление грузовика";
buttonAddTruck.UseVisualStyleBackColor = true;
buttonAddTruck.Click += ButtonAddTruck_Click;
buttonCreteCompany.Location = new Point(7, 313);
buttonCreteCompany.Name = "buttonCreteCompany";
buttonCreteCompany.Size = new Size(225, 29);
buttonCreteCompany.TabIndex = 10;
buttonCreteCompany.Text = "Создать компанию";
buttonCreteCompany.UseVisualStyleBackColor = true;
buttonCreteCompany.Click += ButtonCreteCompany_Click;
//
// comboBoxSelectorCompany
//
@ -123,11 +83,166 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(6, 35);
comboBoxSelectorCompany.Location = new Point(6, 348);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(226, 28);
comboBoxSelectorCompany.TabIndex = 0;
comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged;
comboBoxSelectorCompany.TabIndex = 9;
//
// 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(labelCollectionalName);
panelStorage.Dock = DockStyle.Top;
panelStorage.Location = new Point(3, 23);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(232, 280);
panelStorage.TabIndex = 8;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 241);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(219, 29);
buttonCollectionDel.TabIndex = 7;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 20;
listBoxCollection.Location = new Point(4, 131);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(219, 104);
listBoxCollection.TabIndex = 6;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(4, 96);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(219, 29);
buttonCollectionAdd.TabIndex = 5;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(122, 66);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(80, 24);
radioButtonList.TabIndex = 4;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(25, 66);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(82, 24);
radioButtonMassive.TabIndex = 3;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 33);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(226, 27);
textBoxCollectionName.TabIndex = 2;
//
// labelCollectionalName
//
labelCollectionalName.AutoSize = true;
labelCollectionalName.Location = new Point(38, 10);
labelCollectionalName.Name = "labelCollectionalName";
labelCollectionalName.Size = new Size(155, 20);
labelCollectionalName.TabIndex = 1;
labelCollectionalName.Text = "Название коллекции";
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonAddTruck);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonAddDumpTruck);
panelCompanyTools.Controls.Add(buttonRemoveTruck);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 382);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(232, 303);
panelCompanyTools.TabIndex = 7;
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(3, 115);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(226, 27);
maskedTextBoxPosition.TabIndex = 3;
maskedTextBoxPosition.ValidatingType = typeof(int);
//
// buttonRefresh
//
buttonRefresh.Location = new Point(3, 256);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(226, 48);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
//
// buttonAddTruck
//
buttonAddTruck.Location = new Point(3, 3);
buttonAddTruck.Name = "buttonAddTruck";
buttonAddTruck.Size = new Size(226, 52);
buttonAddTruck.TabIndex = 1;
buttonAddTruck.Text = "Добавление грузовика";
buttonAddTruck.UseVisualStyleBackColor = true;
buttonAddTruck.Click += ButtonAddTruck_Click;
//
// buttonGoToCheck
//
buttonGoToCheck.Location = new Point(3, 202);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(226, 48);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тест";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += ButtonGoToCheck_Click;
//
// buttonAddDumpTruck
//
buttonAddDumpTruck.Location = new Point(3, 61);
buttonAddDumpTruck.Name = "buttonAddDumpTruck";
buttonAddDumpTruck.Size = new Size(226, 48);
buttonAddDumpTruck.TabIndex = 2;
buttonAddDumpTruck.Text = "Добавление самосвала";
buttonAddDumpTruck.UseVisualStyleBackColor = true;
buttonAddDumpTruck.Click += ButtonAddDumpTruck_Click;
//
// buttonRemoveTruck
//
buttonRemoveTruck.Location = new Point(3, 148);
buttonRemoveTruck.Name = "buttonRemoveTruck";
buttonRemoveTruck.Size = new Size(226, 48);
buttonRemoveTruck.TabIndex = 4;
buttonRemoveTruck.Text = "Удаление\r\n";
buttonRemoveTruck.UseVisualStyleBackColor = true;
buttonRemoveTruck.Click += ButtonRemoveTruck_Click;
//
// pictureBox
//
@ -148,7 +263,10 @@
Name = "FormTruckCollection";
Text = "Коллекция грузовиков";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
ResumeLayout(false);
}
@ -156,7 +274,6 @@
#endregion
private GroupBox groupBoxTools;
private ComboBox comboBoxSelectorCompany;
private Button buttonAddTruck;
private Button buttonAddDumpTruck;
private Button buttonGoToCheck;
@ -164,5 +281,16 @@
private MaskedTextBox maskedTextBoxPosition;
private PictureBox pictureBox;
private Button buttonRefresh;
private Panel panelCompanyTools;
private Panel panelStorage;
private Button buttonCollectionAdd;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Label labelCollectionalName;
private Button buttonCreteCompany;
private ComboBox comboBoxSelectorCompany;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
}
}

View File

@ -6,9 +6,11 @@ using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
namespace ProjectDumpTruck;
@ -17,6 +19,9 @@ namespace ProjectDumpTruck;
/// </summary>
public partial class FormTruckCollection : Form
{
private readonly StorageCollection<DrawningTruck> _storageCollection;
/// <summary>
/// Компания
/// </summary>
@ -28,6 +33,7 @@ public partial class FormTruckCollection : Form
public FormTruckCollection()
{
InitializeComponent();
_storageCollection = new();
}
/// <summary>
@ -37,12 +43,7 @@ public partial class FormTruckCollection : Form
/// <param name="e"></param>
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new Autopark(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningTruck>());
break;
}
panelCompanyTools.Enabled = false;
}
/// <summary>
@ -64,14 +65,14 @@ public partial class FormTruckCollection : Form
case nameof(DrawningDumpTruck):
drawningTruck = new DrawningDumpTruck(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 + drawningTruck!=-1)
if (_company + drawningTruck != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
@ -107,7 +108,7 @@ public partial class FormTruckCollection : Form
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_company - pos!=null)
if (_company - pos != null)
{
MessageBox.Show("Объект удален");
@ -140,14 +141,109 @@ public partial class FormTruckCollection : Form
counter--;
if (counter <= 0) break;
}
if (truck == null) return;
FormDumpTruck form = new()
FormDumpTruck form = new()
{
SetTruck = truck
};
form.ShowDialog();
}
/// <summary>
/// Добавление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
RefreshListBoxItems();
}
/// <summary>
/// Обновление списка в listBoxCollection
/// </summary>
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);
}
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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.Yes)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RefreshListBoxItems();
}
/// <summary>
/// Создание компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreteCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObject<DrawningTruck>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new Autopark(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RefreshListBoxItems();
}
}