diff --git a/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs b/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs index d34be07..bd16cf8 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs @@ -39,7 +39,18 @@ namespace ProjectElectricLocomotive.Generics } if (x is DrawingElectricLocomotive && y is DrawingElectricLocomotive) { - // TODO доделать логику сравнения дополнительных параметров + if ((x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor != (y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor) + { + return false; + } + if ((x.EntityLocomotive as EntityElectricLocomotive).Pantograph != (y.EntityLocomotive as EntityElectricLocomotive).Pantograph) + { + return false; + } + if ((x.EntityLocomotive as EntityElectricLocomotive).Compartment != (y.EntityLocomotive as EntityElectricLocomotive).Compartment) + { + return false; + } } return true; } diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs index 42af062..5136c5f 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs @@ -94,6 +94,10 @@ namespace ProjectElectricLocomotive MessageBox.Show(ex.Message); _logger.LogWarning("Не удалось добавить объект"); } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } } private void ButtonRemoveObject_Click(object sender, EventArgs e) { @@ -158,19 +162,9 @@ namespace ProjectElectricLocomotive pictureBoxCollection.Image = obj.ShowLocomotives(); } private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType()); - /// - /// Сортировка по цвету - /// - /// - /// - private void ButtonSortByColor_Click(object sender, EventArgs e) - { - // TODO продумать логику - } - /// - /// Сортировка по сравнителю - /// - /// + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByColor()); + private void CompareLocomotives(IComparer comparer) { if (listBoxStorages.SelectedIndex == -1) diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs index 026ea0e..632c195 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs @@ -8,28 +8,45 @@ using ProjectElectricLocomotive.Entities; namespace ProjectElectricLocomotive.Generics { - internal class LocoCompareByColor : IComparer + internal class LocomotiveCompareByColor : IComparer { public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) { - if (x == null || x.EntityLocomotive == null) - throw new NotImplementedException(nameof(x)); - - if (y == null || y.EntityLocomotive == null) - throw new NotImplementedException(nameof(y)); - - var bodyColor = x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name); - - if (bodyColor != 0) return bodyColor; - - if (x.EntityLocomotive is EntityElectricLocomotive && - y.EntityLocomotive is EntityElectricLocomotive) { - var addcolor = (x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name.CompareTo((y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name); - if (addcolor != 0) return addcolor; + throw new ArgumentNullException(nameof(x)); } - return 1; + if (y == null || y.EntityLocomotive == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityLocomotive.BodyColor.Name != y.EntityLocomotive.BodyColor.Name) + { + return x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawingLocomotive) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawingElectricLocomotive) + { + EntityElectricLocomotive entityX = (EntityElectricLocomotive)x.EntityLocomotive; + EntityElectricLocomotive entityY = (EntityElectricLocomotive)y.EntityLocomotive; + if (entityX.AdditionalColor.Name != entityY.AdditionalColor.Name) + { + return entityX.AdditionalColor.Name.CompareTo(entityY.AdditionalColor.Name); + } + } + var speedCompare = + x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight); } } } diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs index 5f100eb..011d197 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs @@ -34,7 +34,7 @@ namespace ProjectElectricLocomotive.Generics { return -1; } - return collect._collection.Insert(locomotive); + return collect._collection.Insert(locomotive, new DrawingLocomotiveEqutables()); } public static T? operator -(LocomotivesGenericCollection collect, int pos) {