diff --git a/HoistingCrane/HoistingCrane/CraneCompareByColor.cs b/HoistingCrane/HoistingCrane/CraneCompareByColor.cs new file mode 100644 index 0000000..7fd8825 --- /dev/null +++ b/HoistingCrane/HoistingCrane/CraneCompareByColor.cs @@ -0,0 +1,49 @@ + +using HoistingCrane.DrawningObjects; +using HoistingCrane.Entities; + +namespace HoistingCrane.Generics +{ + internal class CraneCompareByColor : IComparer + { + public int Compare(DrawingCrane? x, DrawingCrane? y) + { + if (x == null || x.EntityCrane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCrane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityCrane.BodyColor.Name != y.EntityCrane.BodyColor.Name) + { + return x.EntityCrane.BodyColor.Name.CompareTo(y.EntityCrane.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is EntityCrane) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is AdditionalDrawingCrane) + { + AdditionEntityCrane _X = (AdditionEntityCrane)x.EntityCrane; + AdditionEntityCrane _Y = (AdditionEntityCrane)y.EntityCrane; + + if (_X.AdditionalColor.Name != _Y.AdditionalColor.Name) + { + return _X.AdditionalColor.Name.CompareTo(_Y.AdditionalColor.Name); + } + } + + var speedCompare = x.EntityCrane.Speed.CompareTo(y.EntityCrane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityCrane.Weight.CompareTo(y.EntityCrane.Weight); + } + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/CraneCompareByType.cs b/HoistingCrane/HoistingCrane/CraneCompareByType.cs new file mode 100644 index 0000000..fc51be9 --- /dev/null +++ b/HoistingCrane/HoistingCrane/CraneCompareByType.cs @@ -0,0 +1,30 @@ +using HoistingCrane.DrawningObjects; + +namespace HoistingCrane.Generics +{ + internal class CraneCompareByType : IComparer + { + public int Compare(DrawingCrane? x, DrawingCrane? y) + { + if (x == null || x.EntityCrane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCrane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityCrane.Speed.CompareTo(y.EntityCrane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityCrane.Weight.CompareTo(y.EntityCrane.Weight); + } + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/CranesCollectionInfo.cs b/HoistingCrane/HoistingCrane/CranesCollectionInfo.cs new file mode 100644 index 0000000..cd0e12b --- /dev/null +++ b/HoistingCrane/HoistingCrane/CranesCollectionInfo.cs @@ -0,0 +1,23 @@ +namespace HoistingCrane.Generics +{ + internal class CranesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public CranesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(CranesCollectionInfo? other) + { + + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/CranesGenericCollection.cs b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs index 6cbfcb0..ad59fc5 100644 --- a/HoistingCrane/HoistingCrane/CranesGenericCollection.cs +++ b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs @@ -18,6 +18,7 @@ namespace HoistingCrane.Generics private readonly int _placeSizeHeight = 150; /// Набор объектов private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public IEnumerable GetCranes => _collection.GetCranes(); /// Конструктор public CranesGenericCollection(int picWidth, int picHeight) @@ -35,7 +36,7 @@ namespace HoistingCrane.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawningCraneEqutables()); } /// Перегрузка оператора вычитания public static bool operator -(CranesGenericCollection collect, int pos) diff --git a/HoistingCrane/HoistingCrane/CranesGenericStorage.cs b/HoistingCrane/HoistingCrane/CranesGenericStorage.cs index 7f3692a..bb5a112 100644 --- a/HoistingCrane/HoistingCrane/CranesGenericStorage.cs +++ b/HoistingCrane/HoistingCrane/CranesGenericStorage.cs @@ -9,9 +9,9 @@ namespace HoistingCrane internal class CranesGenericStorage { /// Словарь (хранилище) - readonly Dictionary> _craneStorages; + Dictionary> _craneStorages; /// Возвращение списка названий наборов - public List Keys => _craneStorages.Keys.ToList(); + public List Keys => _craneStorages.Keys.ToList(); /// Ширина окна отрисовки private readonly int _pictureWidth; /// Высота окна отрисовки @@ -29,8 +29,7 @@ namespace HoistingCrane File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _craneStorages) + foreach (KeyValuePair> record in _craneStorages) { StringBuilder records = new(); foreach (DrawingCrane? elem in record.Value.GetCranes) @@ -95,7 +94,7 @@ namespace HoistingCrane } } } - _craneStorages.Add(name, collection); + _craneStorages.Add(new CranesCollectionInfo(name, string.Empty), collection); } return true; } @@ -103,25 +102,30 @@ namespace HoistingCrane /// Конструктор public CranesGenericStorage(int pictureWidth, int pictureHeight) { - _craneStorages = new Dictionary>(); + _craneStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } /// Добавление набора public void AddSet(string name) { - if (_craneStorages.ContainsKey(name)) + if (_craneStorages.ContainsKey(new CranesCollectionInfo(name, string.Empty))) { - return; + MessageBox.Show("Словарь уже содержит набор с таким названием", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + else + { + _craneStorages.Add(new CranesCollectionInfo(name, string.Empty), new CranesGenericCollection(_pictureWidth, _pictureHeight)); } - _craneStorages.Add(name, new CranesGenericCollection(_pictureWidth, _pictureHeight)); } /// Удаление набора public void DelSet(string name) { - if (_craneStorages.ContainsKey(name)) + CranesGenericCollection crane; + if (_craneStorages.TryGetValue(new CranesCollectionInfo(name, string.Empty), out crane)) { - _craneStorages.Remove(name); + _craneStorages.Remove(new CranesCollectionInfo(name, string.Empty)); } } /// Доступ к набору @@ -129,9 +133,10 @@ namespace HoistingCrane { get { - if (_craneStorages.ContainsKey(ind)) - return _craneStorages[ind]; + CranesCollectionInfo infCrane = new CranesCollectionInfo(ind, string.Empty); + if (_craneStorages.ContainsKey(infCrane)) return _craneStorages[infCrane]; return null; + } } } diff --git a/HoistingCrane/HoistingCrane/DrawningCraneEqutables.cs b/HoistingCrane/HoistingCrane/DrawningCraneEqutables.cs new file mode 100644 index 0000000..2fb86a0 --- /dev/null +++ b/HoistingCrane/HoistingCrane/DrawningCraneEqutables.cs @@ -0,0 +1,58 @@ +using HoistingCrane.DrawningObjects; +using HoistingCrane.Entities; +using System.Diagnostics.CodeAnalysis; +namespace HoistingCrane.Generics +{ + internal class DrawningCraneEqutables : IEqualityComparer + { + public bool Equals(DrawingCrane? x, DrawingCrane? y) + { + if (x == null || x.EntityCrane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCrane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityCrane.Speed != y.EntityCrane.Speed) + { + return false; + } + if (x.EntityCrane.Weight != y.EntityCrane.Weight) + { + return false; + } + if (x.EntityCrane.BodyColor != y.EntityCrane.BodyColor) + { + return false; + } + if (x is AdditionalDrawingCrane && y is AdditionalDrawingCrane) + { + AdditionEntityCrane _x = (AdditionEntityCrane)x.EntityCrane; + AdditionEntityCrane _y = (AdditionEntityCrane)y.EntityCrane; + if(_x.AdditionalColor != _y.AdditionalColor) + { + return false; + } + if (_x.Speed != _y.Speed) + { + return false; + } + if (_x.Weight != _y.Weight) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawingCrane obj) + { + return obj.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs index 2c0924d..09fc4cc 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs @@ -38,6 +38,8 @@ this.buttonReload = new System.Windows.Forms.Button(); this.buttonDelete = new System.Windows.Forms.Button(); this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -58,9 +60,11 @@ this.groupBoxInstruments.Controls.Add(this.buttonReload); this.groupBoxInstruments.Controls.Add(this.buttonDelete); this.groupBoxInstruments.Controls.Add(this.buttonAdd); - this.groupBoxInstruments.Location = new System.Drawing.Point(811, 0); + this.groupBoxInstruments.Controls.Add(this.buttonSortByColor); + this.groupBoxInstruments.Controls.Add(this.buttonSortByType); + this.groupBoxInstruments.Location = new System.Drawing.Point(806, 0); this.groupBoxInstruments.Name = "groupBoxInstruments"; - this.groupBoxInstruments.Size = new System.Drawing.Size(185, 464); + this.groupBoxInstruments.Size = new System.Drawing.Size(192, 511); this.groupBoxInstruments.TabIndex = 0; this.groupBoxInstruments.TabStop = false; this.groupBoxInstruments.Text = "Инструменты"; @@ -87,9 +91,9 @@ // // buttonAddSet // - this.buttonAddSet.Location = new System.Drawing.Point(6, 65); + this.buttonAddSet.Location = new System.Drawing.Point(-6, 65); this.buttonAddSet.Name = "buttonAddSet"; - this.buttonAddSet.Size = new System.Drawing.Size(167, 35); + this.buttonAddSet.Size = new System.Drawing.Size(191, 35); this.buttonAddSet.TabIndex = 12; this.buttonAddSet.Text = "Добавить набор"; this.buttonAddSet.UseVisualStyleBackColor = true; @@ -107,9 +111,9 @@ // // buttonRemoveSet // - this.buttonRemoveSet.Location = new System.Drawing.Point(6, 216); + this.buttonRemoveSet.Location = new System.Drawing.Point(-1, 216); this.buttonRemoveSet.Name = "buttonRemoveSet"; - this.buttonRemoveSet.Size = new System.Drawing.Size(167, 35); + this.buttonRemoveSet.Size = new System.Drawing.Size(181, 35); this.buttonRemoveSet.TabIndex = 11; this.buttonRemoveSet.Text = "Удалить набор"; this.buttonRemoveSet.UseVisualStyleBackColor = true; @@ -117,16 +121,16 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 319); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(29, 398); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31); this.maskedTextBoxNumber.TabIndex = 9; // // buttonReload // - this.buttonReload.Location = new System.Drawing.Point(12, 397); + this.buttonReload.Location = new System.Drawing.Point(0, 476); this.buttonReload.Name = "buttonReload"; - this.buttonReload.Size = new System.Drawing.Size(167, 35); + this.buttonReload.Size = new System.Drawing.Size(191, 35); this.buttonReload.TabIndex = 8; this.buttonReload.Text = "Обновить экран"; this.buttonReload.UseVisualStyleBackColor = true; @@ -134,9 +138,9 @@ // // buttonDelete // - this.buttonDelete.Location = new System.Drawing.Point(12, 356); + this.buttonDelete.Location = new System.Drawing.Point(0, 435); this.buttonDelete.Name = "buttonDelete"; - this.buttonDelete.Size = new System.Drawing.Size(167, 35); + this.buttonDelete.Size = new System.Drawing.Size(191, 35); this.buttonDelete.TabIndex = 7; this.buttonDelete.Text = "Удалить кран"; this.buttonDelete.UseVisualStyleBackColor = true; @@ -144,20 +148,40 @@ // // buttonAdd // - this.buttonAdd.Location = new System.Drawing.Point(12, 278); + this.buttonAdd.Location = new System.Drawing.Point(1, 357); this.buttonAdd.Name = "buttonAdd"; - this.buttonAdd.Size = new System.Drawing.Size(167, 35); + this.buttonAdd.Size = new System.Drawing.Size(191, 35); this.buttonAdd.TabIndex = 6; this.buttonAdd.Text = "Добавить кран"; this.buttonAdd.UseVisualStyleBackColor = true; this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click); // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(-1, 319); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(199, 36); + this.buttonSortByColor.TabIndex = 15; + this.buttonSortByColor.Text = "Сортировка по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(0, 278); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(198, 35); + this.buttonSortByType.TabIndex = 14; + this.buttonSortByType.Text = "Сортировка по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // pictureBoxCollection // this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(996, 401); + this.pictureBoxCollection.Size = new System.Drawing.Size(998, 478); this.pictureBoxCollection.TabIndex = 5; this.pictureBoxCollection.TabStop = false; // @@ -168,10 +192,10 @@ this.menuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.файлToolStripMenuItem}); - this.menuStrip1.Location = new System.Drawing.Point(0, 401); + this.menuStrip1.Location = new System.Drawing.Point(0, 478); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional; - this.menuStrip1.Size = new System.Drawing.Size(996, 33); + this.menuStrip1.Size = new System.Drawing.Size(998, 33); this.menuStrip1.TabIndex = 12; this.menuStrip1.Text = "menuStrip1"; // @@ -211,7 +235,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(996, 434); + this.ClientSize = new System.Drawing.Size(998, 511); this.Controls.Add(this.groupBoxInstruments); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.menuStrip1); @@ -248,5 +272,7 @@ private ToolStripMenuItem ToolStripMenuItemSave; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.cs index f21b480..06d2d41 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.cs +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.cs @@ -10,6 +10,25 @@ namespace HoistingCrane { public partial class FormCraneCollection : Form { + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCranes(new CraneCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareCranes(new CraneCompareByColor()); + } + private void CompareCranes(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.ShowCars(); + } private readonly CranesGenericStorage _storage; private readonly ILogger _logger; public FormCraneCollection(ILogger logger) @@ -24,7 +43,7 @@ namespace HoistingCrane 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)) { @@ -108,40 +127,48 @@ namespace HoistingCrane } private void ButtonAddCrane_Click(object sender, EventArgs e) { - if (listBoxStorages.SelectedIndex == -1) { - return; - } - var formCraneConfig = new FormCraneConfig(); - formCraneConfig.AddEvent(crane => - { - try + if (listBoxStorages.SelectedIndex == -1) { - if (listBoxStorages.SelectedIndex != -1) + return; + } + var formCraneConfig = new FormCraneConfig(); + formCraneConfig.AddEvent(crane => + { + try { - var obj = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]; - if (obj != null) + if (listBoxStorages.SelectedIndex != -1) { - if (obj + crane != 1) + var obj = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]; + if (obj != null) { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = obj.ShowCars(); - _logger.LogInformation("Объект добавлен"); - } - else - { - MessageBox.Show("Не удалось добавить объект"); + if (obj + crane != 1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowCars(); + _logger.LogInformation("Объект добавлен"); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } } } - } - catch (StorageOverflowException ex) - { - MessageBox.Show(ex.Message); - _logger.LogWarning(ex.Message); - } - }); - formCraneConfig.Show(); + catch (StorageOverflowException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning(ex.Message); + } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning(ex.Message); + } + }); + + formCraneConfig.Show(); + } } diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.resx b/HoistingCrane/HoistingCrane/FormCraneCollection.resx index a15ebdd..6128f5c 100644 --- a/HoistingCrane/HoistingCrane/FormCraneCollection.resx +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.resx @@ -66,4 +66,7 @@ 367, 17 + + 27 + \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/SetGeneric.cs b/HoistingCrane/HoistingCrane/SetGeneric.cs index c525799..029d356 100644 --- a/HoistingCrane/HoistingCrane/SetGeneric.cs +++ b/HoistingCrane/HoistingCrane/SetGeneric.cs @@ -19,21 +19,26 @@ namespace HoistingCrane.Generics _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// Добавление объекта в набор - public int Insert(T crane) + public int Insert(T crane, IEqualityComparer? equal = null) { if (Count >= _maxCount) throw new StorageOverflowException(Count); - _places.Insert(0, crane); + Insert(0, crane, equal); if (_places.Contains(null)) _places.Remove(null); return 0; } - public int Insert(T crane, int position) + public int Insert(int position, T crane, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount || Count >= _maxCount) throw new CraneNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(Count); + if (equal != null && _places.Contains(crane, equal)) + { + throw new ArgumentException("Добавляемый объект уже существует в коллекции"); + } _places.Insert(position, crane); if(_places.Contains(null)) _places.Remove(null); return position;