diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavatorEqutables.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavatorEqutables.cs new file mode 100644 index 0000000..86577be --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavatorEqutables.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics.CodeAnalysis; +using ProjectExcavator.Entities; + +namespace ProjectExcavator.Generic +{ + internal class DrawningExcavatorEqutables : IEqualityComparer + { + public bool Equals(DrawningExcavator? x, DrawningExcavator? 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 DrawningExcavatorBodyKits && y is DrawningExcavatorBodyKits) + { + EntityExcavatorBodyKits xExcavator = (EntityExcavatorBodyKits)x.EntityExcavator; + EntityExcavatorBodyKits yExcavator = (EntityExcavatorBodyKits)y.EntityExcavator; + if(xExcavator.Bucket != yExcavator.Bucket) + { + return false; + } + if(xExcavator.BodyKit != yExcavator.BodyKit) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningExcavator obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorCompareByColor.cs b/ProjectExcavator/ProjectExcavator/ExcavatorCompareByColor.cs new file mode 100644 index 0000000..8798085 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorCompareByColor.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator +{ + internal class ExcavatorCompareByColor : IComparer + { + public int Compare(DrawningExcavator? x, DrawningExcavator? 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 != y.EntityExcavator.BodyColor) + { + return x.EntityExcavator.BodyColor.Name.CompareTo(y.EntityExcavator.BodyColor.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..6e0ccfd --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorCompareByType.cs @@ -0,0 +1,34 @@ +using System; +using System.CodeDom; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator +{ + internal class ExcavatorCompareByType : IComparer + { + public int Compare(DrawningExcavator? x, DrawningExcavator? 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 7baaeae..ec6d552 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs @@ -36,7 +36,7 @@ namespace ProjectExcavator.Generic { return -1; } - return collect?._collection.Insert(obj) ?? -1; + return collect?._collection.Insert(obj, new DrawningExcavatorEqutables()) ?? -1; } public static T? operator -(ExcavatorGenericCollection collect, int pos) { @@ -44,7 +44,7 @@ namespace ProjectExcavator.Generic collect._collection.Remove(pos); return obj; } - + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public U? GetU(int pos) => (U?)_collection[pos]?.GetMoveableObject; /// /// Вывод всего набора объектов diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorsCollectionInfo.cs b/ProjectExcavator/ProjectExcavator/ExcavatorsCollectionInfo.cs new file mode 100644 index 0000000..84553fb --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorsCollectionInfo.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator +{ + internal class ExcavatorsCollectionInfo : IEquatable + { + public string Name { get;private set; } + public string Description { get;private set; } + public ExcavatorsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(ExcavatorsCollectionInfo other) + { + return Name.Equals(other.Name); + } + public override int GetHashCode() + { + return Name?.GetHashCode() ?? 0; + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorsGenericStorage.cs b/ProjectExcavator/ProjectExcavator/ExcavatorsGenericStorage.cs index 66baf3c..b63da37 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorsGenericStorage.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorsGenericStorage.cs @@ -15,11 +15,11 @@ namespace ProjectExcavator.Generic /// /// Словарь (хранилище) /// - readonly Dictionary> _excavatorStorages; + readonly Dictionary> _excavatorStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _excavatorStorages.Keys.ToList(); + public List Keys => _excavatorStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -48,7 +48,7 @@ namespace ProjectExcavator.Generic /// public ExcavatorsGenericStorage(int pictureWidth, int pictureHeight) { - _excavatorStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -127,7 +127,7 @@ namespace ProjectExcavator.Generic } } } - _excavatorStorages.Add(record[0], collection); + _excavatorStorages.Add(new ExcavatorsCollectionInfo(record [0], string.Empty), collection); currentLine = sr.ReadLine(); } } @@ -138,11 +138,11 @@ namespace ProjectExcavator.Generic /// Название набора public void AddSet(string name) { - if (_excavatorStorages.ContainsKey(name)) + if (_excavatorStorages.ContainsKey(new ExcavatorsCollectionInfo(name, string.Empty))) { return; } - _excavatorStorages[name] = new ExcavatorGenericCollection(_pictureWidth, _pictureHeight); + _excavatorStorages[new ExcavatorsCollectionInfo(name, string.Empty)] = new ExcavatorGenericCollection(_pictureWidth, _pictureHeight); } /// /// Удаление набора @@ -150,8 +150,9 @@ namespace ProjectExcavator.Generic /// Название набора public void DelSet(string name) { - if (!_excavatorStorages.ContainsKey(name)) return; - _excavatorStorages.Remove(name); + if (!_excavatorStorages.ContainsKey(new ExcavatorsCollectionInfo(name, string.Empty))) + return; + _excavatorStorages.Remove(new ExcavatorsCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -163,9 +164,9 @@ namespace ProjectExcavator.Generic { get { - if (_excavatorStorages.ContainsKey(ind)) + if (_excavatorStorages.ContainsKey(new ExcavatorsCollectionInfo(ind, string.Empty))) { - return _excavatorStorages[ind]; + return _excavatorStorages[new ExcavatorsCollectionInfo(ind, string.Empty)]; } return null; } diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs index 22a4935..8b5c847 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs @@ -44,6 +44,8 @@ загрузитьToolStripMenuItem = new ToolStripMenuItem(); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); menuStripItem.SuspendLayout(); SuspendLayout(); @@ -51,16 +53,18 @@ // pictureBoxCollection // pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(617, 450); + pictureBoxCollection.Size = new Size(705, 600); pictureBoxCollection.TabIndex = 0; pictureBoxCollection.TabStop = false; // // buttonAddExcavator // - buttonAddExcavator.Location = new Point(623, 275); + buttonAddExcavator.Location = new Point(712, 367); + buttonAddExcavator.Margin = new Padding(3, 4, 3, 4); buttonAddExcavator.Name = "buttonAddExcavator"; - buttonAddExcavator.Size = new Size(174, 51); + buttonAddExcavator.Size = new Size(199, 68); buttonAddExcavator.TabIndex = 1; buttonAddExcavator.Text = "Добавить экскавотор"; buttonAddExcavator.UseVisualStyleBackColor = true; @@ -68,16 +72,18 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(623, 332); + maskedTextBoxNumber.Location = new Point(712, 443); + maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(174, 23); + maskedTextBoxNumber.Size = new Size(198, 27); maskedTextBoxNumber.TabIndex = 2; // // buttonRemoveExcavator // - buttonRemoveExcavator.Location = new Point(623, 361); + buttonRemoveExcavator.Location = new Point(712, 481); + buttonRemoveExcavator.Margin = new Padding(3, 4, 3, 4); buttonRemoveExcavator.Name = "buttonRemoveExcavator"; - buttonRemoveExcavator.Size = new Size(174, 38); + buttonRemoveExcavator.Size = new Size(199, 51); buttonRemoveExcavator.TabIndex = 3; buttonRemoveExcavator.Text = "Удалить объект"; buttonRemoveExcavator.UseVisualStyleBackColor = true; @@ -85,9 +91,10 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(623, 405); + buttonRefreshCollection.Location = new Point(712, 540); + buttonRefreshCollection.Margin = new Padding(3, 4, 3, 4); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(174, 33); + buttonRefreshCollection.Size = new Size(199, 44); buttonRefreshCollection.TabIndex = 4; buttonRefreshCollection.Text = "Обновить коллекцию"; buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -96,18 +103,20 @@ // listBoxStorages // listBoxStorages.FormattingEnabled = true; - listBoxStorages.ItemHeight = 15; - listBoxStorages.Location = new Point(623, 156); + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(712, 208); + listBoxStorages.Margin = new Padding(3, 4, 3, 4); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(174, 79); + listBoxStorages.Size = new Size(198, 104); listBoxStorages.TabIndex = 5; listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; // // AddCollectButton // - AddCollectButton.Location = new Point(623, 119); + AddCollectButton.Location = new Point(712, 159); + AddCollectButton.Margin = new Padding(3, 4, 3, 4); AddCollectButton.Name = "AddCollectButton"; - AddCollectButton.Size = new Size(174, 31); + AddCollectButton.Size = new Size(199, 41); AddCollectButton.TabIndex = 6; AddCollectButton.Text = "Добавить набор"; AddCollectButton.UseVisualStyleBackColor = true; @@ -115,9 +124,10 @@ // // DeleteCollectButton // - DeleteCollectButton.Location = new Point(623, 241); + DeleteCollectButton.Location = new Point(712, 321); + DeleteCollectButton.Margin = new Padding(3, 4, 3, 4); DeleteCollectButton.Name = "DeleteCollectButton"; - DeleteCollectButton.Size = new Size(174, 28); + DeleteCollectButton.Size = new Size(199, 37); DeleteCollectButton.TabIndex = 7; DeleteCollectButton.Text = "Удалить набор"; DeleteCollectButton.UseVisualStyleBackColor = true; @@ -125,19 +135,22 @@ // // textBoxStorageName // - textBoxStorageName.Location = new Point(623, 90); + textBoxStorageName.Location = new Point(712, 120); + textBoxStorageName.Margin = new Padding(3, 4, 3, 4); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(174, 23); + textBoxStorageName.Size = new Size(198, 27); textBoxStorageName.TabIndex = 8; // // menuStripItem // menuStripItem.Anchor = AnchorStyles.Top | AnchorStyles.Right; menuStripItem.Dock = DockStyle.None; + menuStripItem.ImageScalingSize = new Size(20, 20); menuStripItem.Items.AddRange(new ToolStripItem[] { StripMenuItem }); - menuStripItem.Location = new Point(623, 9); + menuStripItem.Location = new Point(845, 12); menuStripItem.Name = "menuStripItem"; - menuStripItem.Size = new Size(170, 24); + menuStripItem.Padding = new Padding(7, 3, 0, 3); + menuStripItem.Size = new Size(61, 30); menuStripItem.TabIndex = 9; menuStripItem.Text = "File"; // @@ -145,25 +158,25 @@ // StripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem1, сохранитьToolStripMenuItem, загрузитьToolStripMenuItem }); StripMenuItem.Name = "StripMenuItem"; - StripMenuItem.Size = new Size(42, 20); + StripMenuItem.Size = new Size(52, 24); StripMenuItem.Text = "Files"; // // toolStripMenuItem1 // toolStripMenuItem1.Name = "toolStripMenuItem1"; - toolStripMenuItem1.Size = new Size(133, 22); + toolStripMenuItem1.Size = new Size(166, 26); // // сохранитьToolStripMenuItem // сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem"; - сохранитьToolStripMenuItem.Size = new Size(133, 22); + сохранитьToolStripMenuItem.Size = new Size(166, 26); сохранитьToolStripMenuItem.Text = "Сохранить"; сохранитьToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // загрузитьToolStripMenuItem // загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem"; - загрузитьToolStripMenuItem.Size = new Size(133, 22); + загрузитьToolStripMenuItem.Size = new Size(166, 26); загрузитьToolStripMenuItem.Text = "Загрузить"; загрузитьToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -171,11 +184,33 @@ // openFileDialog.FileName = "openFileDialog1"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(711, 84); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(195, 29); + buttonSortByColor.TabIndex = 10; + buttonSortByColor.Text = "Сортировать по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(712, 49); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(190, 29); + buttonSortByType.TabIndex = 11; + buttonSortByType.Text = "Сортировать по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // FormExcavatorCollection // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(914, 600); + Controls.Add(buttonSortByType); + Controls.Add(buttonSortByColor); Controls.Add(textBoxStorageName); Controls.Add(DeleteCollectButton); Controls.Add(AddCollectButton); @@ -187,6 +222,7 @@ Controls.Add(pictureBoxCollection); Controls.Add(menuStripItem); MainMenuStrip = menuStripItem; + Margin = new Padding(3, 4, 3, 4); Name = "FormExcavatorCollection"; Text = "Набор экскаваторов"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); @@ -214,5 +250,7 @@ private ToolStripMenuItem toolStripMenuItem1; private ToolStripMenuItem сохранитьToolStripMenuItem; private ToolStripMenuItem загрузитьToolStripMenuItem; + 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 a88cede..a79e46f 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs @@ -25,7 +25,7 @@ namespace ProjectExcavator public FormExcavatorCollection(ILogger logger) { InitializeComponent(); - _storage = new ExcavatorsGenericStorage(pictureBoxCollection.Width,pictureBoxCollection.Height); + _storage = new ExcavatorsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); _logger = logger; } /// @@ -37,7 +37,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)) { @@ -48,6 +48,21 @@ namespace ProjectExcavator listBoxStorages.SelectedIndex = index; } } + 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(); + } + private void ButtonAddObject_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxStorageName.Text)) @@ -113,11 +128,12 @@ namespace ProjectExcavator _logger.LogWarning($"Добавление не удалось (индекс вне границ)"); MessageBox.Show("Объект добавить не удалось"); } - }catch(ApplicationException ex) + } + catch (ApplicationException ex) { MessageBox.Show(ex.Message); } - + } private void buttonAddExcavator_Click(object sender, EventArgs e) @@ -173,7 +189,7 @@ namespace ProjectExcavator MessageBox.Show("Не удалось удалить объект"); } } - catch(ExcavatorNotFoundException ex) + catch (ExcavatorNotFoundException ex) { _logger.LogWarning($"Удаление не удалось {ex.Message}"); MessageBox.Show(ex.Message); @@ -213,7 +229,7 @@ namespace ProjectExcavator _logger.LogInformation($"Cохранение успешно"); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning($"Сохранение не удалось {ex.Message}"); @@ -245,5 +261,15 @@ namespace ProjectExcavator } ReloadObjects(); } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + CompareExcavators(new ExcavatorCompareByType()); + } + + private void buttonSortByColor_Click(object sender, EventArgs e) + { + CompareExcavators(new ExcavatorCompareByColor()); + } } } diff --git a/ProjectExcavator/ProjectExcavator/SetGeneric.cs b/ProjectExcavator/ProjectExcavator/SetGeneric.cs index 78a9741..e6b0e80 100644 --- a/ProjectExcavator/ProjectExcavator/SetGeneric.cs +++ b/ProjectExcavator/ProjectExcavator/SetGeneric.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectExcavator.Exceptions; + namespace ProjectExcavator.Generic { @@ -12,27 +14,35 @@ namespace ProjectExcavator.Generic private readonly List _places; public int Count => _places.Count; private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); public SetGeneric(int count) { _maxCount = count; _places = new List(_maxCount); } - public int Insert(T excavator) + public int Insert(T excavator, IEqualityComparer equal = null) { if(_places.Count >= _maxCount) { - return -1; + throw new StorageOverflowException(_places.Count); + } + if(equal != null && _places.Contains(excavator,equal)) + { + throw new ApplicationException("already exist"); } _places.Insert(0, excavator); return 0; } - public bool Insert(T excavator, int position) + public bool Insert(T excavator, int position, IEqualityComparer equal = null) { if (position < 0 || position >= _maxCount) - return false; + throw new ExcavatorNotFoundException(_places.Count); if (Count >= _maxCount) - return false; - _places.Insert(0,excavator); + throw new StorageOverflowException(_places.Count); + if(position == _places.Count) + _places.Add(excavator); + else + _places.Insert(position,excavator); return true; } public bool Remove(int position) {