Восьмая базовая лабораторная

This commit is contained in:
Pyatkin I.A 2023-12-18 17:10:46 +04:00
parent dd87d4fba8
commit 543ee84aa7
9 changed files with 390 additions and 159 deletions

View File

@ -0,0 +1,65 @@
using ProjectExcavator.DrawingObjects;
using ProjectExcavator.Entities;
using System.Diagnostics.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectExcavator.Generics
{
internal class DrawningExcavatorEqutables : IEqualityComparer<DrawingExcavator?>
{
public bool Equals(DrawingExcavator? x, DrawingExcavator? y)
{
if (x == null || x.EntityExcavator == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityExcavator == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityExcavator.Speed != y.EntityExcavator.Speed)
{
return false;
}
if (x.EntityExcavator.Weight != y.EntityExcavator.Weight)
{
return false;
}
if (x.EntityExcavator.BodyColor != y.EntityExcavator.BodyColor)
{
return false;
}
if (x is DrawingExcavatorKovsh && y is DrawingExcavatorKovsh)
{
// TODO доделать логику сравнения дополнительных параметров
EntityExcavatorKovsh _excavatorX = (EntityExcavatorKovsh)x.EntityExcavator;
EntityExcavatorKovsh _excavatorY = (EntityExcavatorKovsh)y.EntityExcavator;
if (_excavatorX.Kovsh != _excavatorY.Kovsh)
{
return false;
}
if (_excavatorX.Katki != _excavatorY.Katki)
{
return false;
}
if (_excavatorX.AdditionalColor != _excavatorY.AdditionalColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawingExcavator obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectExcavator.Generics
{
internal class ExcavatorCollectionInfo : IEquatable<ExcavatorCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public ExcavatorCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(ExcavatorCollectionInfo? other)
{
if (this.Name == other?.Name)
{
return true;
}
return false;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,51 @@
using ProjectExcavator.DrawingObjects;
using ProjectExcavator.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectExcavator.Generics
{
internal class ExcavatorCompareByColor : IComparer<DrawingExcavator?>
{
public int Compare(DrawingExcavator? x, DrawingExcavator? y)
{
if (x == null || x.EntityExcavator == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityExcavator == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityExcavator.BodyColor.Name != y.EntityExcavator.BodyColor.Name)
{
return x.EntityExcavator.BodyColor.Name.CompareTo(y.EntityExcavator.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
if (x.GetType().Name == y.GetType().Name && x is DrawingExcavatorKovsh)
{
EntityExcavatorKovsh _exX = (EntityExcavatorKovsh)x.EntityExcavator;
EntityExcavatorKovsh _exY = (EntityExcavatorKovsh)y.EntityExcavator;
if (_exX.AdditionalColor.Name != _exY.AdditionalColor.Name)
{
return _exX.AdditionalColor.Name.CompareTo(_exY.AdditionalColor.Name);
}
}
var speedCompare = x.EntityExcavator.Speed.CompareTo(y.EntityExcavator.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityExcavator.Weight.CompareTo(y.EntityExcavator.Weight);
}
}
}

View File

@ -0,0 +1,35 @@
using ProjectExcavator.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectExcavator.Generics
{
internal class ExcavatorCompareByType : IComparer<DrawingExcavator?>
{
public int Compare(DrawingExcavator? x, DrawingExcavator? y)
{
if (x == null || x.EntityExcavator == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityExcavator == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare =
x.EntityExcavator.Speed.CompareTo(y.EntityExcavator.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityExcavator.Weight.CompareTo(y.EntityExcavator.Weight);
}
}
}

View File

@ -17,6 +17,7 @@ namespace ProjectExcavator.Generics
where T : DrawingExcavator where T : DrawingExcavator
where U : IMoveableObject where U : IMoveableObject
{ {
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary> /// <summary>
/// Ширина окна прорисовки /// Ширина окна прорисовки
/// </summary> /// </summary>
@ -64,7 +65,7 @@ namespace ProjectExcavator.Generics
{ {
return false; return false;
} }
return collect?._collection.Insert(obj) ?? false; return (bool)collect?._collection.Insert(obj, new DrawningExcavatorEqutables());
} }
/// <summary> /// <summary>

View File

@ -18,12 +18,12 @@ namespace ProjectExcavator.Generics
/// <summary> /// <summary>
/// Словарь (хранилище) /// Словарь (хранилище)
/// </summary> /// </summary>
readonly Dictionary<string, ExcavatorGenericCollection<DrawingExcavator, readonly Dictionary<ExcavatorCollectionInfo, ExcavatorGenericCollection<DrawingExcavator,
DrawingObjectExcavator>> _excavatorStorages; DrawingObjectExcavator>> _excavatorStorages;
/// <summary> /// <summary>
/// Возвращение списка названий наборов /// Возвращение списка названий наборов
/// </summary> /// </summary>
public List<string> Keys => _excavatorStorages.Keys.ToList(); public List<ExcavatorCollectionInfo> Keys => _excavatorStorages.Keys.ToList();
/// <summary> /// <summary>
/// Ширина окна отрисовки /// Ширина окна отрисовки
/// </summary> /// </summary>
@ -51,7 +51,7 @@ namespace ProjectExcavator.Generics
private static readonly char _separatorForObject = ':'; private static readonly char _separatorForObject = ':';
public ExcavatorGenericStorage(int pictureWidth, int pictureHeight) public ExcavatorGenericStorage(int pictureWidth, int pictureHeight)
{ {
_excavatorStorages = new Dictionary<string, _excavatorStorages = new Dictionary<ExcavatorCollectionInfo,
ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>>(); ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
@ -62,10 +62,10 @@ namespace ProjectExcavator.Generics
/// <param name="name">Название набора</param> /// <param name="name">Название набора</param>
public void AddSet(string name) public void AddSet(string name)
{ {
if (!_excavatorStorages.ContainsKey(name)) if (!_excavatorStorages.ContainsKey(new ExcavatorCollectionInfo(name, string.Empty)))
{ {
ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator> newSet = new ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>(_pictureWidth, _pictureHeight); ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator> newSet = new ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>(_pictureWidth, _pictureHeight);
_excavatorStorages.Add(name, newSet); _excavatorStorages.Add(new ExcavatorCollectionInfo(name, string.Empty), newSet);
} }
} }
/// <summary> /// <summary>
@ -75,9 +75,9 @@ namespace ProjectExcavator.Generics
public void DelSet(string name) public void DelSet(string name)
{ {
{ {
if (_excavatorStorages.ContainsKey(name)) if (_excavatorStorages.ContainsKey(new ExcavatorCollectionInfo(name, string.Empty)))
{ {
_excavatorStorages.Remove(name); _excavatorStorages.Remove(new ExcavatorCollectionInfo(name, string.Empty));
} }
} }
} }
@ -90,9 +90,10 @@ namespace ProjectExcavator.Generics
{ {
get get
{ {
if (_excavatorStorages.ContainsKey(ind)) ExcavatorCollectionInfo indOb = new ExcavatorCollectionInfo(ind, string.Empty);
if (_excavatorStorages.ContainsKey(indOb))
{ {
return _excavatorStorages[ind]; return _excavatorStorages[indOb];
} }
return null; return null;
} }
@ -109,7 +110,7 @@ namespace ProjectExcavator.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, foreach (KeyValuePair<ExcavatorCollectionInfo,
ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>> record in _excavatorStorages) ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>> record in _excavatorStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
@ -184,7 +185,7 @@ namespace ProjectExcavator.Generics
} }
} }
_excavatorStorages.Add(namestorage, collection); _excavatorStorages.Add(new ExcavatorCollectionInfo(namestorage, string.Empty), collection);
} }
} }
} }

View File

@ -28,195 +28,220 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBoxCollection = new PictureBox(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
maskedTextBoxNumber = new MaskedTextBox(); this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
buttonAddEx = new Button(); this.buttonAddEx = new System.Windows.Forms.Button();
buttonRemoveEx = new Button(); this.buttonRemoveEx = new System.Windows.Forms.Button();
buttonRefreshCollection = new Button(); this.buttonRefreshCollection = new System.Windows.Forms.Button();
groupBox1 = new GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
groupBox2 = new GroupBox(); this.ButtonSortByColor = new System.Windows.Forms.Button();
textBoxStorageName = new TextBox(); this.ButtonSortByType = new System.Windows.Forms.Button();
buttonDelObject = new Button(); this.groupBox2 = new System.Windows.Forms.GroupBox();
listBoxStorages = new ListBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox();
buttonAddObject = new Button(); this.buttonDelObject = new System.Windows.Forms.Button();
FilemenuStrip = new MenuStrip(); this.listBoxStorages = new System.Windows.Forms.ListBox();
FileToolStripMenuItem = new ToolStripMenuItem(); this.buttonAddObject = new System.Windows.Forms.Button();
SaveToolStripMenuItem = new ToolStripMenuItem(); this.FilemenuStrip = new System.Windows.Forms.MenuStrip();
LoadToolStripMenuItem = new ToolStripMenuItem(); this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
openFileDialog = new OpenFileDialog(); this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
saveFileDialog = new SaveFileDialog(); this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
groupBox1.SuspendLayout(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
FilemenuStrip.SuspendLayout(); this.groupBox1.SuspendLayout();
SuspendLayout(); this.groupBox2.SuspendLayout();
this.FilemenuStrip.SuspendLayout();
this.SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Dock = DockStyle.Fill; this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 24); this.pictureBoxCollection.Location = new System.Drawing.Point(0, 24);
pictureBoxCollection.Name = "pictureBoxCollection"; this.pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(909, 437); this.pictureBoxCollection.Size = new System.Drawing.Size(909, 437);
pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false; this.pictureBoxCollection.TabStop = false;
// //
// maskedTextBoxNumber // maskedTextBoxNumber
// //
maskedTextBoxNumber.Location = new Point(35, 339); this.maskedTextBoxNumber.Location = new System.Drawing.Point(35, 339);
maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 23); this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 23);
maskedTextBoxNumber.TabIndex = 1; this.maskedTextBoxNumber.TabIndex = 1;
// //
// buttonAddEx // buttonAddEx
// //
buttonAddEx.Location = new Point(13, 310); this.buttonAddEx.Location = new System.Drawing.Point(13, 310);
buttonAddEx.Name = "buttonAddEx"; this.buttonAddEx.Name = "buttonAddEx";
buttonAddEx.Size = new Size(150, 23); this.buttonAddEx.Size = new System.Drawing.Size(150, 23);
buttonAddEx.TabIndex = 2; this.buttonAddEx.TabIndex = 2;
buttonAddEx.Text = "Добавить экскаватор"; this.buttonAddEx.Text = "Добавить экскаватор";
buttonAddEx.UseVisualStyleBackColor = true; this.buttonAddEx.UseVisualStyleBackColor = true;
buttonAddEx.Click += ButtonAddEx_Click; this.buttonAddEx.Click += new System.EventHandler(this.ButtonAddEx_Click);
// //
// buttonRemoveEx // buttonRemoveEx
// //
buttonRemoveEx.Location = new Point(13, 368); this.buttonRemoveEx.Location = new System.Drawing.Point(13, 368);
buttonRemoveEx.Name = "buttonRemoveEx"; this.buttonRemoveEx.Name = "buttonRemoveEx";
buttonRemoveEx.Size = new Size(150, 23); this.buttonRemoveEx.Size = new System.Drawing.Size(150, 23);
buttonRemoveEx.TabIndex = 3; this.buttonRemoveEx.TabIndex = 3;
buttonRemoveEx.Text = "Удалить экскаватор"; this.buttonRemoveEx.Text = "Удалить экскаватор";
buttonRemoveEx.UseVisualStyleBackColor = true; this.buttonRemoveEx.UseVisualStyleBackColor = true;
buttonRemoveEx.Click += ButtonRemoveEx_Click; this.buttonRemoveEx.Click += new System.EventHandler(this.ButtonRemoveEx_Click);
// //
// buttonRefreshCollection // buttonRefreshCollection
// //
buttonRefreshCollection.Location = new Point(13, 432); this.buttonRefreshCollection.Location = new System.Drawing.Point(13, 432);
buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(148, 23); this.buttonRefreshCollection.Size = new System.Drawing.Size(148, 23);
buttonRefreshCollection.TabIndex = 4; this.buttonRefreshCollection.TabIndex = 4;
buttonRefreshCollection.Text = "Обновить коллекцию"; this.buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true; this.buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click; this.buttonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
// //
// groupBox1 // groupBox1
// //
groupBox1.Controls.Add(groupBox2); this.groupBox1.Controls.Add(this.ButtonSortByColor);
groupBox1.Controls.Add(buttonAddEx); this.groupBox1.Controls.Add(this.ButtonSortByType);
groupBox1.Controls.Add(buttonRefreshCollection); this.groupBox1.Controls.Add(this.groupBox2);
groupBox1.Controls.Add(maskedTextBoxNumber); this.groupBox1.Controls.Add(this.buttonAddEx);
groupBox1.Controls.Add(buttonRemoveEx); this.groupBox1.Controls.Add(this.buttonRefreshCollection);
groupBox1.Location = new Point(741, 0); this.groupBox1.Controls.Add(this.maskedTextBoxNumber);
groupBox1.Name = "groupBox1"; this.groupBox1.Controls.Add(this.buttonRemoveEx);
groupBox1.Size = new Size(168, 461); this.groupBox1.Location = new System.Drawing.Point(741, 0);
groupBox1.TabIndex = 5; this.groupBox1.Name = "groupBox1";
groupBox1.TabStop = false; this.groupBox1.Size = new System.Drawing.Size(168, 461);
groupBox1.Text = "Инструменты"; this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(28, 277);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(122, 23);
this.ButtonSortByColor.TabIndex = 7;
this.ButtonSortByColor.Text = "Сортировка(цвет)";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(28, 248);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(122, 23);
this.ButtonSortByType.TabIndex = 6;
this.ButtonSortByType.Text = "Сортировка(тип)";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
// //
// groupBox2 // groupBox2
// //
groupBox2.Controls.Add(textBoxStorageName); this.groupBox2.Controls.Add(this.textBoxStorageName);
groupBox2.Controls.Add(buttonDelObject); this.groupBox2.Controls.Add(this.buttonDelObject);
groupBox2.Controls.Add(listBoxStorages); this.groupBox2.Controls.Add(this.listBoxStorages);
groupBox2.Controls.Add(buttonAddObject); this.groupBox2.Controls.Add(this.buttonAddObject);
groupBox2.Location = new Point(13, 28); this.groupBox2.Location = new System.Drawing.Point(13, 28);
groupBox2.Name = "groupBox2"; this.groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(143, 214); this.groupBox2.Size = new System.Drawing.Size(143, 214);
groupBox2.TabIndex = 5; this.groupBox2.TabIndex = 5;
groupBox2.TabStop = false; this.groupBox2.TabStop = false;
groupBox2.Text = "Наборы"; this.groupBox2.Text = "Наборы";
// //
// textBoxStorageName // textBoxStorageName
// //
textBoxStorageName.Location = new Point(15, 30); this.textBoxStorageName.Location = new System.Drawing.Point(15, 30);
textBoxStorageName.Name = "textBoxStorageName"; this.textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(122, 23); this.textBoxStorageName.Size = new System.Drawing.Size(122, 23);
textBoxStorageName.TabIndex = 6; this.textBoxStorageName.TabIndex = 6;
// //
// buttonDelObject // buttonDelObject
// //
buttonDelObject.Location = new Point(15, 185); this.buttonDelObject.Location = new System.Drawing.Point(15, 185);
buttonDelObject.Name = "buttonDelObject"; this.buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(122, 23); this.buttonDelObject.Size = new System.Drawing.Size(122, 23);
buttonDelObject.TabIndex = 8; this.buttonDelObject.TabIndex = 8;
buttonDelObject.Text = "Удалить набор"; this.buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true; this.buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += ButtonDelObject_Click; this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
// //
// listBoxStorages // listBoxStorages
// //
listBoxStorages.FormattingEnabled = true; this.listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15; this.listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(15, 88); this.listBoxStorages.Location = new System.Drawing.Point(15, 88);
listBoxStorages.Name = "listBoxStorages"; this.listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(122, 79); this.listBoxStorages.Size = new System.Drawing.Size(122, 79);
listBoxStorages.TabIndex = 6; this.listBoxStorages.TabIndex = 6;
listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged;
// //
// buttonAddObject // buttonAddObject
// //
buttonAddObject.Location = new Point(15, 59); this.buttonAddObject.Location = new System.Drawing.Point(15, 59);
buttonAddObject.Name = "buttonAddObject"; this.buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(122, 23); this.buttonAddObject.Size = new System.Drawing.Size(122, 23);
buttonAddObject.TabIndex = 7; this.buttonAddObject.TabIndex = 7;
buttonAddObject.Text = "Добавить набор"; this.buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true; this.buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += ButtonAddObject_Click; this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
// //
// FilemenuStrip // FilemenuStrip
// //
FilemenuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); this.FilemenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
FilemenuStrip.Location = new Point(0, 0); this.FileToolStripMenuItem});
FilemenuStrip.Name = "FilemenuStrip"; this.FilemenuStrip.Location = new System.Drawing.Point(0, 0);
FilemenuStrip.Size = new Size(909, 24); this.FilemenuStrip.Name = "FilemenuStrip";
FilemenuStrip.TabIndex = 6; this.FilemenuStrip.Size = new System.Drawing.Size(909, 24);
FilemenuStrip.Text = "menuStrip1"; this.FilemenuStrip.TabIndex = 6;
this.FilemenuStrip.Text = "menuStrip1";
// //
// FileToolStripMenuItem // FileToolStripMenuItem
// //
FileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
FileToolStripMenuItem.Name = "FileToolStripMenuItem"; this.SaveToolStripMenuItem,
FileToolStripMenuItem.Size = new Size(48, 20); this.LoadToolStripMenuItem});
FileToolStripMenuItem.Text = "Файл"; this.FileToolStripMenuItem.Name = "FileToolStripMenuItem";
this.FileToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
this.FileToolStripMenuItem.Text = "Файл";
// //
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(133, 22); this.SaveToolStripMenuItem.Size = new System.Drawing.Size(133, 22);
SaveToolStripMenuItem.Text = "Сохранить"; this.SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(133, 22); this.LoadToolStripMenuItem.Size = new System.Drawing.Size(133, 22);
LoadToolStripMenuItem.Text = "Загрузить"; this.LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
// //
// openFileDialog // openFileDialog
// //
openFileDialog.FileName = "openFileDialog"; this.openFileDialog.FileName = "openFileDialog";
openFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\"."; this.openFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\".";
// //
// saveFileDialog // saveFileDialog
// //
saveFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\"."; this.saveFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\".";
// //
// FormExcavatorCollection // FormExcavatorCollection
// //
ClientSize = new Size(909, 461); this.ClientSize = new System.Drawing.Size(909, 461);
Controls.Add(groupBox1); this.Controls.Add(this.groupBox1);
Controls.Add(pictureBoxCollection); this.Controls.Add(this.pictureBoxCollection);
Controls.Add(FilemenuStrip); this.Controls.Add(this.FilemenuStrip);
MainMenuStrip = FilemenuStrip; this.MainMenuStrip = this.FilemenuStrip;
Name = "FormExcavatorCollection"; this.Name = "FormExcavatorCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
groupBox1.PerformLayout(); this.groupBox1.PerformLayout();
groupBox2.ResumeLayout(false); this.groupBox2.ResumeLayout(false);
groupBox2.PerformLayout(); this.groupBox2.PerformLayout();
FilemenuStrip.ResumeLayout(false); this.FilemenuStrip.ResumeLayout(false);
FilemenuStrip.PerformLayout(); this.FilemenuStrip.PerformLayout();
ResumeLayout(false); this.ResumeLayout(false);
PerformLayout(); this.PerformLayout();
} }
#endregion #endregion
@ -238,5 +263,7 @@
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
} }
} }

View File

@ -41,7 +41,7 @@ namespace ProjectExcavator
listBoxStorages.Items.Clear(); listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++) for (int i = 0; i < _storage.Keys.Count; i++)
{ {
listBoxStorages.Items.Add(_storage.Keys[i]); listBoxStorages.Items.Add(_storage.Keys[i].Name);
} }
if (listBoxStorages.Items.Count > 0 && (index == -1 || index if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count)) >= listBoxStorages.Items.Count))
@ -52,13 +52,6 @@ namespace ProjectExcavator
index < listBoxStorages.Items.Count) index < listBoxStorages.Items.Count)
{ {
listBoxStorages.SelectedIndex = index; listBoxStorages.SelectedIndex = index;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowExcavator();
} }
} }
/// <summary> /// <summary>
@ -143,6 +136,12 @@ namespace ProjectExcavator
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
} }
catch (ArgumentException ex)
{
_logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
}); });
form.AddEvent(ExcavatorDelegate); form.AddEvent(ExcavatorDelegate);
form.Show(); form.Show();
@ -268,5 +267,21 @@ namespace ProjectExcavator
} }
} }
} }
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareExcavators(new ExcavatorCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareExcavators(new ExcavatorCompareByColor());
private void CompareExcavators(IComparer<DrawingExcavator?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowExcavator();
}
} }
} }

View File

@ -35,14 +35,15 @@ namespace ProjectExcavator.Generics
_maxCount = count; _maxCount = count;
_places = new List<T?>(count); _places = new List<T?>(count);
} }
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="excavator">Добавляемый экскаватор</param> /// <param name="excavator">Добавляемый экскаватор</param>
/// <returns></returns> /// <returns></returns>
public bool Insert(T excavator) public bool Insert(T excavator, IEqualityComparer<T?>? equal = null)
{ {
return Insert(excavator, 0); return Insert(excavator, 0, equal);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -50,7 +51,7 @@ namespace ProjectExcavator.Generics
/// <param name="excavator">Добавляемый экскаватор</param> /// <param name="excavator">Добавляемый экскаватор</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public bool Insert(T excavator, int position) public bool Insert(T excavator, int position, IEqualityComparer<T?>? equal = null)
{ {
// TODO проверка позиции // TODO проверка позиции
if (position < 0 || position >= _maxCount) if (position < 0 || position >= _maxCount)
@ -62,8 +63,12 @@ namespace ProjectExcavator.Generics
{ {
throw new StorageOverflowException(_places.Count); throw new StorageOverflowException(_places.Count);
} }
if (equal != null && _places.Contains(excavator, equal))
{
throw new ArgumentException("Добавляемый объект присутствует в коллекции");
}
// TODO вставка по позиции // TODO вставка по позиции
_places.Insert(position, excavator); _places.Insert(0, excavator);
return true; return true;
} }
/// <summary> /// <summary>