Лабораторная работа 8

This commit is contained in:
BoiledMilk123 2024-06-04 20:20:10 +04:00
parent 8726d3bf2c
commit e845f0c4ba
5 changed files with 80 additions and 15 deletions

View File

@ -63,7 +63,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
public int Insert(T obj, IEqualityComparer<DrawningLocomotive?>? comparer = null) public int Insert(T obj, IEqualityComparer<DrawningLocomotive?>? comparer = null)
{ {
if (_collection.Contains(obj, comparer)) throw new AlreadyExistingObject(obj.GetDataForSave());
if (Count == _maxCount) throw new CollectionOwerflowException(Count); if (Count == _maxCount) throw new CollectionOwerflowException(Count);
_collection.Add(obj); _collection.Add(obj);
return Count; return Count;
@ -110,4 +110,10 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i]; yield return _collection[i];
} }
} }
public void CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
}
} }

View File

@ -70,6 +70,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
public int Insert(T obj, IEqualityComparer<DrawningLocomotive?>? comparer = null) public int Insert(T obj, IEqualityComparer<DrawningLocomotive?>? comparer = null)
{ {
// вставка в свободное место набора // вставка в свободное место набора
if (_collection.Contains(obj, comparer)) throw new AlreadyExistingObject(obj.GetDataForSave());
for (int i = 0; i < Count; ++i) for (int i = 0; i < Count; ++i)
{ {
if (_collection[i] == null) if (_collection[i] == null)

View File

@ -181,7 +181,7 @@ public class StorageCollection<T>
{ {
try try
{ {
collection.Insert(locomotive); collection.Insert(locomotive, new DrawningLocomotiveEqutables());
} }
catch(Exception ex) catch(Exception ex)
{ {

View File

@ -53,6 +53,8 @@
loadToolStripMenuItem = new ToolStripMenuItem(); loadToolStripMenuItem = new ToolStripMenuItem();
saveFileDialog = new SaveFileDialog(); saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog(); openFileDialog = new OpenFileDialog();
buttonSortByColor = new Button();
buttonSortByType = new Button();
groupBoxTools.SuspendLayout(); groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout(); panelCompanyTools.SuspendLayout();
panelStorage.SuspendLayout(); panelStorage.SuspendLayout();
@ -69,7 +71,7 @@
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(640, 24); groupBoxTools.Location = new Point(640, 24);
groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(200, 569); groupBoxTools.Size = new Size(200, 678);
groupBoxTools.TabIndex = 0; groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты"; groupBoxTools.Text = "Инструменты";
@ -98,6 +100,8 @@
// //
// panelCompanyTools // panelCompanyTools
// //
panelCompanyTools.Controls.Add(buttonSortByColor);
panelCompanyTools.Controls.Add(buttonSortByType);
panelCompanyTools.Controls.Add(buttonAddLocomotive); panelCompanyTools.Controls.Add(buttonAddLocomotive);
panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(maskedTextBoxPosition); panelCompanyTools.Controls.Add(maskedTextBoxPosition);
@ -105,9 +109,9 @@
panelCompanyTools.Controls.Add(buttonRemoveLocomotive); panelCompanyTools.Controls.Add(buttonRemoveLocomotive);
panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false; panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 340); panelCompanyTools.Location = new Point(3, 364);
panelCompanyTools.Name = "panelCompanyTools"; panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(194, 226); panelCompanyTools.Size = new Size(194, 311);
panelCompanyTools.TabIndex = 8; panelCompanyTools.TabIndex = 8;
// //
// buttonAddLocomotive // buttonAddLocomotive
@ -246,7 +250,7 @@
pictureBox.Dock = DockStyle.Fill; pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 24); pictureBox.Location = new Point(0, 24);
pictureBox.Name = "pictureBox"; pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(640, 569); pictureBox.Size = new Size(640, 678);
pictureBox.TabIndex = 1; pictureBox.TabIndex = 1;
pictureBox.TabStop = false; pictureBox.TabStop = false;
// //
@ -290,11 +294,31 @@
// //
openFileDialog.Filter = "txt file|*.txt"; openFileDialog.Filter = "txt file|*.txt";
// //
// buttonSortByColor
//
buttonSortByColor.Location = new Point(3, 260);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(188, 31);
buttonSortByColor.TabIndex = 8;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += ButtonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(3, 223);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(188, 31);
buttonSortByType.TabIndex = 7;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += ButtonSortByType_Click;
//
// FormLocomotiveCollection // FormLocomotiveCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(840, 593); ClientSize = new Size(840, 702);
Controls.Add(pictureBox); Controls.Add(pictureBox);
Controls.Add(groupBoxTools); Controls.Add(groupBoxTools);
Controls.Add(menuStrip); Controls.Add(menuStrip);
@ -340,5 +364,7 @@
private ToolStripMenuItem loadToolStripMenuItem; private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
} }
} }

View File

@ -228,11 +228,11 @@ public partial class FormLocomotiveCollection : Form
private void RefreshListBoxItems() private void RefreshListBoxItems()
{ {
listBoxCollection.Items.Clear(); listBoxCollection.Items.Clear();
foreach(string colName in _storageCollection.Keys) foreach (var colName in _storageCollection.Keys)
{ {
if (!string.IsNullOrEmpty(colName)) if (!string.IsNullOrEmpty(colName.Name))
{ {
listBoxCollection.Items.Add(colName); listBoxCollection.Items.Add(colName.Name);
} }
} }
} }
@ -315,4 +315,36 @@ public partial class FormLocomotiveCollection : Form
RefreshListBoxItems(); RefreshListBoxItems();
} }
} }
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e)
{
CompareCars(new DrawningLocomotiveCompareByType());
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
CompareCars(new DrawningLocomotiveCompareByColor());
}
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
private void CompareCars(IComparer<DrawningLocomotive?> comparer)
{
if (_company == null) return;
_company.Sort(comparer);
pictureBox.Image = _company.Show();
}
} }