diff --git a/ProjectMonorail/ProjectMonorail/DrawingMonorailEqutables.cs b/ProjectMonorail/ProjectMonorail/DrawingMonorailEqutables.cs new file mode 100644 index 0000000..ee9cca5 --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/DrawingMonorailEqutables.cs @@ -0,0 +1,57 @@ +using ProjectMonorail.DrawingObjects; +using ProjectMonorail.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectMonorail.Generics +{ + internal class DrawingMonorailEqutables : IEqualityComparer + { + public bool Equals(DrawingMonorail? x, DrawingMonorail? y) + { + if (x == null || x.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityMonorail.Speed != y.EntityMonorail.Speed) + { + return false; + } + if (x.EntityMonorail.Weight != y.EntityMonorail.Weight) + { + return false; + } + if (x.EntityMonorail.MainColor != y.EntityMonorail.MainColor) + { + return false; + } + if (x is DrawingExtendedMonorail && y is DrawingExtendedMonorail) + { + EntityExtendedMonorail xExtendedMonorail = (EntityExtendedMonorail)x.EntityMonorail; + EntityExtendedMonorail yExtendedMonorail = (EntityExtendedMonorail)y.EntityMonorail; + + if (xExtendedMonorail.AdditionalColor != yExtendedMonorail.AdditionalColor) + return false; + + if (xExtendedMonorail.MagneticRail != yExtendedMonorail.MagneticRail) + return false; + + if (xExtendedMonorail.ExtraCabin != yExtendedMonorail.ExtraCabin) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawingMonorail obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs index 2016826..2c29fde 100644 --- a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs +++ b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { panelTools = new Panel(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); panelSets = new Panel(); textBoxNameSet = new TextBox(); buttonAddSet = new Button(); @@ -57,6 +59,8 @@ // panelTools.Anchor = AnchorStyles.Top | AnchorStyles.Right; panelTools.BorderStyle = BorderStyle.FixedSingle; + panelTools.Controls.Add(buttonSortByColor); + panelTools.Controls.Add(buttonSortByType); panelTools.Controls.Add(panelSets); panelTools.Controls.Add(buttonRefreshCollection); panelTools.Controls.Add(labelToolsName); @@ -65,9 +69,37 @@ panelTools.Controls.Add(buttonAddMonorail); panelTools.Location = new Point(784, 38); panelTools.Name = "panelTools"; - panelTools.Size = new Size(187, 580); + panelTools.Size = new Size(187, 661); panelTools.TabIndex = 0; // + // buttonSortByColor + // + buttonSortByColor.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonSortByColor.BackColor = SystemColors.Window; + buttonSortByColor.FlatAppearance.BorderColor = Color.Black; + buttonSortByColor.FlatStyle = FlatStyle.Flat; + buttonSortByColor.Location = new Point(5, 392); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(175, 28); + buttonSortByColor.TabIndex = 6; + buttonSortByColor.Text = "Sort by color"; + buttonSortByColor.UseVisualStyleBackColor = false; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonSortByType.BackColor = SystemColors.Window; + buttonSortByType.FlatAppearance.BorderColor = Color.Black; + buttonSortByType.FlatStyle = FlatStyle.Flat; + buttonSortByType.Location = new Point(5, 354); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(175, 28); + buttonSortByType.TabIndex = 5; + buttonSortByType.Text = "Sort by type"; + buttonSortByType.UseVisualStyleBackColor = false; + buttonSortByType.Click += buttonSortByType_Click; + // // panelSets // panelSets.Anchor = AnchorStyles.Top | AnchorStyles.Right; @@ -145,7 +177,7 @@ buttonRefreshCollection.BackColor = SystemColors.Window; buttonRefreshCollection.FlatAppearance.BorderColor = Color.Black; buttonRefreshCollection.FlatStyle = FlatStyle.Flat; - buttonRefreshCollection.Location = new Point(5, 534); + buttonRefreshCollection.Location = new Point(5, 615); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(175, 34); buttonRefreshCollection.TabIndex = 3; @@ -168,7 +200,7 @@ // maskedTextBoxNumber.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; maskedTextBoxNumber.BorderStyle = BorderStyle.FixedSingle; - maskedTextBoxNumber.Location = new Point(4, 411); + maskedTextBoxNumber.Location = new Point(4, 492); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(175, 23); maskedTextBoxNumber.TabIndex = 2; @@ -179,7 +211,7 @@ buttonRemoveMonorail.BackColor = SystemColors.Window; buttonRemoveMonorail.FlatAppearance.BorderColor = Color.Black; buttonRemoveMonorail.FlatStyle = FlatStyle.Flat; - buttonRemoveMonorail.Location = new Point(4, 451); + buttonRemoveMonorail.Location = new Point(4, 532); buttonRemoveMonorail.Name = "buttonRemoveMonorail"; buttonRemoveMonorail.Size = new Size(175, 34); buttonRemoveMonorail.TabIndex = 1; @@ -193,7 +225,7 @@ buttonAddMonorail.BackColor = SystemColors.Window; buttonAddMonorail.FlatAppearance.BorderColor = Color.Black; buttonAddMonorail.FlatStyle = FlatStyle.Flat; - buttonAddMonorail.Location = new Point(4, 357); + buttonAddMonorail.Location = new Point(4, 438); buttonAddMonorail.Name = "buttonAddMonorail"; buttonAddMonorail.Size = new Size(175, 34); buttonAddMonorail.TabIndex = 0; @@ -205,7 +237,7 @@ // pictureBoxCollection.Location = new Point(8, 38); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(774, 580); + pictureBoxCollection.Size = new Size(774, 661); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -254,7 +286,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(974, 626); + ClientSize = new Size(974, 701); Controls.Add(pictureBoxCollection); Controls.Add(panelTools); Controls.Add(menuStripFile); @@ -294,5 +326,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/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs index 50985a7..9fb5e79 100644 --- a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs +++ b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs @@ -39,7 +39,7 @@ namespace ProjectMonorail listBoxSets.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxSets.Items.Add(_storage.Keys[i]); + listBoxSets.Items.Add(_storage.Keys[i].Name); } if (listBoxSets.Items.Count > 0 && (index == -1 || index >= listBoxSets.Items.Count)) { @@ -136,6 +136,11 @@ namespace ProjectMonorail MessageBox.Show(ex.Message); _logger.LogWarning($"Failed attempt to add a object: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Failed attempt to add a object: {ex.Message}"); + } } /// @@ -198,7 +203,7 @@ namespace ProjectMonorail { MessageBox.Show(ex.Message); _logger.LogWarning($"Failed attempt to delete a object: {ex.Message}"); - } + } } /// @@ -272,5 +277,38 @@ namespace ProjectMonorail } } } + + /// + /// Сортировка по типу + /// + /// + /// + private void buttonSortByType_Click(object sender, EventArgs e) => CompareMonorails(new MonorailCompareByType()); + + /// + /// Сортировка по цвету + /// + /// + /// + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareMonorails(new MonorailCompareByColor()); + + /// + /// Сортировка по сравнителю + /// + /// + private void CompareMonorails(IComparer comparer) + { + if (listBoxSets.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowMonorails(); + } } } diff --git a/ProjectMonorail/ProjectMonorail/MonorailCompareByColor.cs b/ProjectMonorail/ProjectMonorail/MonorailCompareByColor.cs new file mode 100644 index 0000000..3b639ac --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/MonorailCompareByColor.cs @@ -0,0 +1,41 @@ +using ProjectMonorail.DrawingObjects; +using ProjectMonorail.Entities; + +namespace ProjectMonorail.Generics +{ + internal class MonorailCompareByColor :IComparer + { + public int Compare(DrawingMonorail? x, DrawingMonorail? y) + { + if (x == null || x.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(y)); + } + var mainColorCompare = x.EntityMonorail.MainColor.Name.CompareTo(y.EntityMonorail.MainColor.Name); + if (mainColorCompare != 0) + { + return mainColorCompare; + } + if (x is DrawingExtendedMonorail && y is DrawingExtendedMonorail) + { + EntityExtendedMonorail xExtendedMonorail = (EntityExtendedMonorail)x.EntityMonorail; + EntityExtendedMonorail yExtendedMonorail = (EntityExtendedMonorail)y.EntityMonorail; + var additionalColorCompare = xExtendedMonorail.AdditionalColor.Name.CompareTo(yExtendedMonorail.AdditionalColor.Name); + if (additionalColorCompare != 0) + { + return additionalColorCompare; + } + } + var speedCompare = x.EntityMonorail.Speed.CompareTo(y.EntityMonorail.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityMonorail.Weight.CompareTo(y.EntityMonorail.Weight); + } + } +} diff --git a/ProjectMonorail/ProjectMonorail/MonorailCompareByType.cs b/ProjectMonorail/ProjectMonorail/MonorailCompareByType.cs new file mode 100644 index 0000000..9178b9d --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/MonorailCompareByType.cs @@ -0,0 +1,29 @@ +using ProjectMonorail.DrawingObjects; + +namespace ProjectMonorail.Generics +{ + internal class MonorailCompareByType : IComparer + { + public int Compare(DrawingMonorail? x, DrawingMonorail? y) + { + if (x == null || x.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityMonorail.Speed.CompareTo(y.EntityMonorail.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityMonorail.Weight.CompareTo(y.EntityMonorail.Weight); + } + } +} diff --git a/ProjectMonorail/ProjectMonorail/MonorailsCollectionInfo.cs b/ProjectMonorail/ProjectMonorail/MonorailsCollectionInfo.cs new file mode 100644 index 0000000..90caffc --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/MonorailsCollectionInfo.cs @@ -0,0 +1,29 @@ +namespace ProjectMonorail.Generics +{ + internal class MonorailsCollectionInfo : IEquatable + { + public string Name { get; private set; } + + public string Description { get; private set; } + + public MonorailsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + + public bool Equals(MonorailsCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} + diff --git a/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs b/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs index 40e87f2..1953b2b 100644 --- a/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs +++ b/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs @@ -42,6 +42,12 @@ namespace ProjectMonorail.Generics /// public IEnumerable GetMonorails => _collection.GetMonorails(); + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Конструктор /// @@ -68,7 +74,7 @@ namespace ProjectMonorail.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawingMonorailEqutables()); } /// diff --git a/ProjectMonorail/ProjectMonorail/MonorailsGenericStorage.cs b/ProjectMonorail/ProjectMonorail/MonorailsGenericStorage.cs index 70e883f..77c8a9a 100644 --- a/ProjectMonorail/ProjectMonorail/MonorailsGenericStorage.cs +++ b/ProjectMonorail/ProjectMonorail/MonorailsGenericStorage.cs @@ -27,12 +27,12 @@ namespace ProjectMonorail.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _monorailsStorages; + readonly Dictionary> _monorailsStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _monorailsStorages.Keys.ToList(); + public List Keys => _monorailsStorages.Keys.ToList(); /// /// Ширина окна отрисовки @@ -51,7 +51,7 @@ namespace ProjectMonorail.Generics /// public MonorailsGenericStorage(int pictureWidth, int pictureHeight) { - _monorailsStorages = new Dictionary>(); + _monorailsStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -62,8 +62,8 @@ namespace ProjectMonorail.Generics /// Название набора public void AddSet(string name) { - if (!_monorailsStorages.ContainsKey(name)) - _monorailsStorages.Add(name, new MonorailsGenericCollection(_pictureWidth, _pictureHeight)); + if (!_monorailsStorages.ContainsKey(new MonorailsCollectionInfo(name, ""))) + _monorailsStorages.Add(new MonorailsCollectionInfo(name, ""), new MonorailsGenericCollection(_pictureWidth, _pictureHeight)); } /// @@ -72,8 +72,8 @@ namespace ProjectMonorail.Generics /// Название набора public void DelSet(string name) { - if (_monorailsStorages.ContainsKey(name)) - _monorailsStorages.Remove(name); + if (_monorailsStorages.ContainsKey(new MonorailsCollectionInfo(name, ""))) + _monorailsStorages.Remove(new MonorailsCollectionInfo(name, "")); } /// @@ -85,8 +85,8 @@ namespace ProjectMonorail.Generics { get { - if (_monorailsStorages.ContainsKey(ind)) - return _monorailsStorages[ind]; + if (_monorailsStorages.ContainsKey(new MonorailsCollectionInfo(ind, ""))) + return _monorailsStorages[new MonorailsCollectionInfo(ind, "")]; return null; } } @@ -102,14 +102,14 @@ namespace ProjectMonorail.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _monorailsStorages) + foreach (KeyValuePair> record in _monorailsStorages) { StringBuilder records = new(); foreach (DrawingMonorail? elem in record.Value.GetMonorails) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -162,7 +162,7 @@ namespace ProjectMonorail.Generics } } } - _monorailsStorages.Add(record[0], collection); + _monorailsStorages.Add(new MonorailsCollectionInfo(record[0], ""), collection); } } } diff --git a/ProjectMonorail/ProjectMonorail/SetGeneric.cs b/ProjectMonorail/ProjectMonorail/SetGeneric.cs index 90c37bb..6ec0659 100644 --- a/ProjectMonorail/ProjectMonorail/SetGeneric.cs +++ b/ProjectMonorail/ProjectMonorail/SetGeneric.cs @@ -24,6 +24,12 @@ namespace ProjectMonorail.Generics /// private readonly int _maxCount; + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); + /// /// Конструктор /// @@ -39,9 +45,9 @@ namespace ProjectMonorail.Generics /// /// Добавляемый монорельс /// - public int Insert(T monorail) + public int Insert(T monorail, IEqualityComparer? equal = null) { - return Insert(monorail, 0); + return Insert(monorail, 0, equal); } /// @@ -50,13 +56,14 @@ namespace ProjectMonorail.Generics /// Добавляемый монорельс /// Позиция /// - public int Insert(T monorail, int position) + public int Insert(T monorail, int position, IEqualityComparer? equal = null) { if (position < 0 || position > Count) throw new MonorailNotFoundException("Impossible to insert"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - + if (equal != null && _places.Contains(monorail, equal)) + throw new ArgumentException("This object is already in the collection"); _places.Insert(position, monorail); return position; }