diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavatorEqutables.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavatorEqutables.cs new file mode 100644 index 0000000..7901a1f --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavatorEqutables.cs @@ -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 + { + 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(); + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorCollectionInfo.cs b/ProjectExcavator/ProjectExcavator/ExcavatorCollectionInfo.cs new file mode 100644 index 0000000..e42c453 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorCollectionInfo.cs @@ -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 + { + 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(); + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorCompareByColor.cs b/ProjectExcavator/ProjectExcavator/ExcavatorCompareByColor.cs new file mode 100644 index 0000000..1d7dcf8 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorCompareByColor.cs @@ -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 + { + 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); + + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorCompareByType.cs b/ProjectExcavator/ProjectExcavator/ExcavatorCompareByType.cs new file mode 100644 index 0000000..e12f0e5 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorCompareByType.cs @@ -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 + { + 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); + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs index 7eddf6b..bf76ba6 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs @@ -17,6 +17,7 @@ namespace ProjectExcavator.Generics where T : DrawingExcavator where U : IMoveableObject { + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Ширина окна прорисовки /// @@ -64,7 +65,7 @@ namespace ProjectExcavator.Generics { return false; } - return collect?._collection.Insert(obj) ?? false; + return (bool)collect?._collection.Insert(obj, new DrawningExcavatorEqutables()); } /// diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs index 5fca65b..e82e8f7 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericStorage.cs @@ -18,12 +18,12 @@ namespace ProjectExcavator.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _excavatorStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _excavatorStorages.Keys.ToList(); + public List Keys => _excavatorStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -51,7 +51,7 @@ namespace ProjectExcavator.Generics private static readonly char _separatorForObject = ':'; public ExcavatorGenericStorage(int pictureWidth, int pictureHeight) { - _excavatorStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -62,10 +62,10 @@ namespace ProjectExcavator.Generics /// Название набора public void AddSet(string name) { - if (!_excavatorStorages.ContainsKey(name)) + if (!_excavatorStorages.ContainsKey(new ExcavatorCollectionInfo(name, string.Empty))) { ExcavatorGenericCollection newSet = new ExcavatorGenericCollection(_pictureWidth, _pictureHeight); - _excavatorStorages.Add(name, newSet); + _excavatorStorages.Add(new ExcavatorCollectionInfo(name, string.Empty), newSet); } } /// @@ -75,9 +75,9 @@ namespace ProjectExcavator.Generics 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 { - if (_excavatorStorages.ContainsKey(ind)) + ExcavatorCollectionInfo indOb = new ExcavatorCollectionInfo(ind, string.Empty); + if (_excavatorStorages.ContainsKey(indOb)) { - return _excavatorStorages[ind]; + return _excavatorStorages[indOb]; } return null; } @@ -109,7 +110,7 @@ namespace ProjectExcavator.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _excavatorStorages) { StringBuilder records = new(); @@ -184,7 +185,7 @@ namespace ProjectExcavator.Generics } } - _excavatorStorages.Add(namestorage, collection); + _excavatorStorages.Add(new ExcavatorCollectionInfo(namestorage, string.Empty), collection); } } } diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs index 2e53e0e..3358407 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs @@ -28,195 +28,220 @@ /// private void InitializeComponent() { - pictureBoxCollection = new PictureBox(); - maskedTextBoxNumber = new MaskedTextBox(); - buttonAddEx = new Button(); - buttonRemoveEx = new Button(); - buttonRefreshCollection = new Button(); - groupBox1 = new GroupBox(); - groupBox2 = new GroupBox(); - textBoxStorageName = new TextBox(); - buttonDelObject = new Button(); - listBoxStorages = new ListBox(); - buttonAddObject = new Button(); - FilemenuStrip = new MenuStrip(); - FileToolStripMenuItem = new ToolStripMenuItem(); - SaveToolStripMenuItem = new ToolStripMenuItem(); - LoadToolStripMenuItem = new ToolStripMenuItem(); - openFileDialog = new OpenFileDialog(); - saveFileDialog = new SaveFileDialog(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); - groupBox1.SuspendLayout(); - groupBox2.SuspendLayout(); - FilemenuStrip.SuspendLayout(); - SuspendLayout(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddEx = new System.Windows.Forms.Button(); + this.buttonRemoveEx = new System.Windows.Forms.Button(); + this.buttonRefreshCollection = new System.Windows.Forms.Button(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.buttonDelObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonAddObject = new System.Windows.Forms.Button(); + this.FilemenuStrip = new System.Windows.Forms.MenuStrip(); + this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); + this.FilemenuStrip.SuspendLayout(); + this.SuspendLayout(); // // pictureBoxCollection // - pictureBoxCollection.Dock = DockStyle.Fill; - pictureBoxCollection.Location = new Point(0, 24); - pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(909, 437); - pictureBoxCollection.TabIndex = 0; - pictureBoxCollection.TabStop = false; + this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxCollection.Location = new System.Drawing.Point(0, 24); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(909, 437); + this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.TabStop = false; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(35, 339); - maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(100, 23); - maskedTextBoxNumber.TabIndex = 1; + this.maskedTextBoxNumber.Location = new System.Drawing.Point(35, 339); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 23); + this.maskedTextBoxNumber.TabIndex = 1; // // buttonAddEx // - buttonAddEx.Location = new Point(13, 310); - buttonAddEx.Name = "buttonAddEx"; - buttonAddEx.Size = new Size(150, 23); - buttonAddEx.TabIndex = 2; - buttonAddEx.Text = "Добавить экскаватор"; - buttonAddEx.UseVisualStyleBackColor = true; - buttonAddEx.Click += ButtonAddEx_Click; + this.buttonAddEx.Location = new System.Drawing.Point(13, 310); + this.buttonAddEx.Name = "buttonAddEx"; + this.buttonAddEx.Size = new System.Drawing.Size(150, 23); + this.buttonAddEx.TabIndex = 2; + this.buttonAddEx.Text = "Добавить экскаватор"; + this.buttonAddEx.UseVisualStyleBackColor = true; + this.buttonAddEx.Click += new System.EventHandler(this.ButtonAddEx_Click); // // buttonRemoveEx // - buttonRemoveEx.Location = new Point(13, 368); - buttonRemoveEx.Name = "buttonRemoveEx"; - buttonRemoveEx.Size = new Size(150, 23); - buttonRemoveEx.TabIndex = 3; - buttonRemoveEx.Text = "Удалить экскаватор"; - buttonRemoveEx.UseVisualStyleBackColor = true; - buttonRemoveEx.Click += ButtonRemoveEx_Click; + this.buttonRemoveEx.Location = new System.Drawing.Point(13, 368); + this.buttonRemoveEx.Name = "buttonRemoveEx"; + this.buttonRemoveEx.Size = new System.Drawing.Size(150, 23); + this.buttonRemoveEx.TabIndex = 3; + this.buttonRemoveEx.Text = "Удалить экскаватор"; + this.buttonRemoveEx.UseVisualStyleBackColor = true; + this.buttonRemoveEx.Click += new System.EventHandler(this.ButtonRemoveEx_Click); // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(13, 432); - buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(148, 23); - buttonRefreshCollection.TabIndex = 4; - buttonRefreshCollection.Text = "Обновить коллекцию"; - buttonRefreshCollection.UseVisualStyleBackColor = true; - buttonRefreshCollection.Click += ButtonRefreshCollection_Click; + this.buttonRefreshCollection.Location = new System.Drawing.Point(13, 432); + this.buttonRefreshCollection.Name = "buttonRefreshCollection"; + this.buttonRefreshCollection.Size = new System.Drawing.Size(148, 23); + this.buttonRefreshCollection.TabIndex = 4; + this.buttonRefreshCollection.Text = "Обновить коллекцию"; + this.buttonRefreshCollection.UseVisualStyleBackColor = true; + this.buttonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // // groupBox1 // - groupBox1.Controls.Add(groupBox2); - groupBox1.Controls.Add(buttonAddEx); - groupBox1.Controls.Add(buttonRefreshCollection); - groupBox1.Controls.Add(maskedTextBoxNumber); - groupBox1.Controls.Add(buttonRemoveEx); - groupBox1.Location = new Point(741, 0); - groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(168, 461); - groupBox1.TabIndex = 5; - groupBox1.TabStop = false; - groupBox1.Text = "Инструменты"; + this.groupBox1.Controls.Add(this.ButtonSortByColor); + this.groupBox1.Controls.Add(this.ButtonSortByType); + this.groupBox1.Controls.Add(this.groupBox2); + this.groupBox1.Controls.Add(this.buttonAddEx); + this.groupBox1.Controls.Add(this.buttonRefreshCollection); + this.groupBox1.Controls.Add(this.maskedTextBoxNumber); + this.groupBox1.Controls.Add(this.buttonRemoveEx); + this.groupBox1.Location = new System.Drawing.Point(741, 0); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(168, 461); + 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.Controls.Add(textBoxStorageName); - groupBox2.Controls.Add(buttonDelObject); - groupBox2.Controls.Add(listBoxStorages); - groupBox2.Controls.Add(buttonAddObject); - groupBox2.Location = new Point(13, 28); - groupBox2.Name = "groupBox2"; - groupBox2.Size = new Size(143, 214); - groupBox2.TabIndex = 5; - groupBox2.TabStop = false; - groupBox2.Text = "Наборы"; + this.groupBox2.Controls.Add(this.textBoxStorageName); + this.groupBox2.Controls.Add(this.buttonDelObject); + this.groupBox2.Controls.Add(this.listBoxStorages); + this.groupBox2.Controls.Add(this.buttonAddObject); + this.groupBox2.Location = new System.Drawing.Point(13, 28); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(143, 214); + this.groupBox2.TabIndex = 5; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Наборы"; // // textBoxStorageName // - textBoxStorageName.Location = new Point(15, 30); - textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(122, 23); - textBoxStorageName.TabIndex = 6; + this.textBoxStorageName.Location = new System.Drawing.Point(15, 30); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(122, 23); + this.textBoxStorageName.TabIndex = 6; // // buttonDelObject // - buttonDelObject.Location = new Point(15, 185); - buttonDelObject.Name = "buttonDelObject"; - buttonDelObject.Size = new Size(122, 23); - buttonDelObject.TabIndex = 8; - buttonDelObject.Text = "Удалить набор"; - buttonDelObject.UseVisualStyleBackColor = true; - buttonDelObject.Click += ButtonDelObject_Click; + this.buttonDelObject.Location = new System.Drawing.Point(15, 185); + this.buttonDelObject.Name = "buttonDelObject"; + this.buttonDelObject.Size = new System.Drawing.Size(122, 23); + this.buttonDelObject.TabIndex = 8; + this.buttonDelObject.Text = "Удалить набор"; + this.buttonDelObject.UseVisualStyleBackColor = true; + this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); // // listBoxStorages // - listBoxStorages.FormattingEnabled = true; - listBoxStorages.ItemHeight = 15; - listBoxStorages.Location = new Point(15, 88); - listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(122, 79); - listBoxStorages.TabIndex = 6; - listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged; + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 15; + this.listBoxStorages.Location = new System.Drawing.Point(15, 88); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(122, 79); + this.listBoxStorages.TabIndex = 6; // // buttonAddObject // - buttonAddObject.Location = new Point(15, 59); - buttonAddObject.Name = "buttonAddObject"; - buttonAddObject.Size = new Size(122, 23); - buttonAddObject.TabIndex = 7; - buttonAddObject.Text = "Добавить набор"; - buttonAddObject.UseVisualStyleBackColor = true; - buttonAddObject.Click += ButtonAddObject_Click; + this.buttonAddObject.Location = new System.Drawing.Point(15, 59); + this.buttonAddObject.Name = "buttonAddObject"; + this.buttonAddObject.Size = new System.Drawing.Size(122, 23); + this.buttonAddObject.TabIndex = 7; + this.buttonAddObject.Text = "Добавить набор"; + this.buttonAddObject.UseVisualStyleBackColor = true; + this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); // // FilemenuStrip // - FilemenuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); - FilemenuStrip.Location = new Point(0, 0); - FilemenuStrip.Name = "FilemenuStrip"; - FilemenuStrip.Size = new Size(909, 24); - FilemenuStrip.TabIndex = 6; - FilemenuStrip.Text = "menuStrip1"; + this.FilemenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.FileToolStripMenuItem}); + this.FilemenuStrip.Location = new System.Drawing.Point(0, 0); + this.FilemenuStrip.Name = "FilemenuStrip"; + this.FilemenuStrip.Size = new System.Drawing.Size(909, 24); + this.FilemenuStrip.TabIndex = 6; + this.FilemenuStrip.Text = "menuStrip1"; // // FileToolStripMenuItem // - FileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); - FileToolStripMenuItem.Name = "FileToolStripMenuItem"; - FileToolStripMenuItem.Size = new Size(48, 20); - FileToolStripMenuItem.Text = "Файл"; + this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.FileToolStripMenuItem.Name = "FileToolStripMenuItem"; + this.FileToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.FileToolStripMenuItem.Text = "Файл"; // // SaveToolStripMenuItem // - SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(133, 22); - SaveToolStripMenuItem.Text = "Сохранить"; - SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(133, 22); + this.SaveToolStripMenuItem.Text = "Сохранить"; // // LoadToolStripMenuItem // - LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(133, 22); - LoadToolStripMenuItem.Text = "Загрузить"; - LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(133, 22); + this.LoadToolStripMenuItem.Text = "Загрузить"; // // openFileDialog // - openFileDialog.FileName = "openFileDialog"; - openFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\"."; + this.openFileDialog.FileName = "openFileDialog"; + this.openFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\"."; // // saveFileDialog // - saveFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\"."; + this.saveFileDialog.Filter = "\"txt file|*.txt|Все файлы|*.*\"."; // // FormExcavatorCollection // - ClientSize = new Size(909, 461); - Controls.Add(groupBox1); - Controls.Add(pictureBoxCollection); - Controls.Add(FilemenuStrip); - MainMenuStrip = FilemenuStrip; - Name = "FormExcavatorCollection"; - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); - groupBox1.ResumeLayout(false); - groupBox1.PerformLayout(); - groupBox2.ResumeLayout(false); - groupBox2.PerformLayout(); - FilemenuStrip.ResumeLayout(false); - FilemenuStrip.PerformLayout(); - ResumeLayout(false); - PerformLayout(); + this.ClientSize = new System.Drawing.Size(909, 461); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.FilemenuStrip); + this.MainMenuStrip = this.FilemenuStrip; + this.Name = "FormExcavatorCollection"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.groupBox2.PerformLayout(); + this.FilemenuStrip.ResumeLayout(false); + this.FilemenuStrip.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + } #endregion @@ -238,5 +263,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs index a519856..d2cd819 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs @@ -41,7 +41,7 @@ namespace ProjectExcavator listBoxStorages.Items.Clear(); 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 >= listBoxStorages.Items.Count)) @@ -52,13 +52,6 @@ namespace ProjectExcavator index < listBoxStorages.Items.Count) { listBoxStorages.SelectedIndex = index; - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? - string.Empty]; - if (obj == null) - { - return; - } - pictureBoxCollection.Image = obj.ShowExcavator(); } } /// @@ -143,6 +136,12 @@ namespace ProjectExcavator MessageBox.Show("Не удалось добавить объект"); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + + } }); form.AddEvent(ExcavatorDelegate); 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 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(); + } } } diff --git a/ProjectExcavator/ProjectExcavator/SetGeneric.cs b/ProjectExcavator/ProjectExcavator/SetGeneric.cs index de9a58d..6ab2149 100644 --- a/ProjectExcavator/ProjectExcavator/SetGeneric.cs +++ b/ProjectExcavator/ProjectExcavator/SetGeneric.cs @@ -35,14 +35,15 @@ namespace ProjectExcavator.Generics _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый экскаватор /// - public bool Insert(T excavator) + public bool Insert(T excavator, IEqualityComparer? equal = null) { - return Insert(excavator, 0); + return Insert(excavator, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,7 +51,7 @@ namespace ProjectExcavator.Generics /// Добавляемый экскаватор /// Позиция /// - public bool Insert(T excavator, int position) + public bool Insert(T excavator, int position, IEqualityComparer? equal = null) { // TODO проверка позиции if (position < 0 || position >= _maxCount) @@ -62,8 +63,12 @@ namespace ProjectExcavator.Generics { throw new StorageOverflowException(_places.Count); } + if (equal != null && _places.Contains(excavator, equal)) + { + throw new ArgumentException("Добавляемый объект присутствует в коллекции"); + } // TODO вставка по позиции - _places.Insert(position, excavator); + _places.Insert(0, excavator); return true; } ///