From bd3d96264e7366525b8ef9538d2bf1f30284a880 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Wed, 30 Nov 2022 18:03:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D1=88?= =?UTF-8?q?=D0=B0=D0=B3:=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArmoredCar/ArmoredCarCompareByColor.cs | 64 +++++++++++++++++++ .../ArmoredCar/ArmoredCarCompareByType.cs | 55 ++++++++++++++++ ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs | 2 + .../MapWithSetArmoredCarsGeneric.cs | 9 +++ .../ArmoredCar/SetArmoredCarsGeneric.cs | 12 ++++ 5 files changed, 142 insertions(+) create mode 100644 ArmoredCar/ArmoredCar/ArmoredCarCompareByColor.cs create mode 100644 ArmoredCar/ArmoredCar/ArmoredCarCompareByType.cs diff --git a/ArmoredCar/ArmoredCar/ArmoredCarCompareByColor.cs b/ArmoredCar/ArmoredCar/ArmoredCarCompareByColor.cs new file mode 100644 index 0000000..d85bec5 --- /dev/null +++ b/ArmoredCar/ArmoredCar/ArmoredCarCompareByColor.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class ArmoredCarCompareByColor : IComparer + { + public int Compare(IDrawningObject x, IDrawningObject y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xCar = x as DrawningObjectArmCar; + var yCar = y as DrawningObjectArmCar; + if (xCar == null && yCar == null) + { + return 0; + } + if (xCar == null && yCar != null) + { + return 1; + } + if (xCar != null && yCar == null) + { + return -1; + } + string xCarColor = xCar.GetArmoredCar.ArmoredCar.BodyColor.Name; + string yCarColor = yCar.GetArmoredCar.ArmoredCar.BodyColor.Name; + if (xCarColor != yCarColor) + { + return xCarColor.CompareTo(yCarColor); + } + + if (xCar.GetArmoredCar.ArmoredCar is EntityTank xTank && yCar.GetArmoredCar.ArmoredCar is EntityTank yTank) + { + string xCarDopColor = xTank.DopColor.Name; + string yCarDopColor = yTank.DopColor.Name; + if (xCarDopColor != yCarDopColor) + { + return xCarColor.CompareTo(yCarColor); + } + } + + var speedCompare = xCar.GetArmoredCar.ArmoredCar.Speed.CompareTo(yCar.GetArmoredCar.ArmoredCar.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xCar.GetArmoredCar.ArmoredCar.Weight.CompareTo(yCar.GetArmoredCar.ArmoredCar.Weight); + } + } +} diff --git a/ArmoredCar/ArmoredCar/ArmoredCarCompareByType.cs b/ArmoredCar/ArmoredCar/ArmoredCarCompareByType.cs new file mode 100644 index 0000000..a02d251 --- /dev/null +++ b/ArmoredCar/ArmoredCar/ArmoredCarCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class ArmoredCarCompareByType : IComparer + { + public int Compare(IDrawningObject x, IDrawningObject y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xCar = x as DrawningObjectArmCar; + var yCar = y as DrawningObjectArmCar; + if (xCar == null && yCar == null) + { + return 0; + } + if (xCar == null && yCar != null) + { + return 1; + } + if (xCar != null && yCar == null) + { + return -1; + } + if (xCar.GetArmoredCar.GetType().Name != yCar.GetArmoredCar.GetType().Name) + { + if (xCar.GetArmoredCar.GetType().Name == "DrawningArmoredCar") + { + return -1; + } + return 1; + } + var speedCompare = xCar.GetArmoredCar.ArmoredCar.Speed.CompareTo(yCar.GetArmoredCar.ArmoredCar.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xCar.GetArmoredCar.ArmoredCar.Weight.CompareTo(yCar.GetArmoredCar.ArmoredCar.Weight); + } + } +} diff --git a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs index 87f94bf..8b14e12 100644 --- a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs +++ b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs @@ -11,6 +11,8 @@ namespace ArmoredCar internal class DrawningObjectArmCar : IDrawningObject { private DrawningArmoredCar _armCar = null; + + public DrawningArmoredCar GetArmoredCar => _armCar; public DrawningObjectArmCar(DrawningArmoredCar car) { _armCar = car; diff --git a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs index db64685..184d333 100644 --- a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs @@ -206,5 +206,14 @@ namespace ArmoredCar _setCars.Insert(DrawningObjectArmCar.Create(rec) as T); } } + + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setCars.SortSet(comparer); + } } } diff --git a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs index 1aef614..3fa6226 100644 --- a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs @@ -114,6 +114,18 @@ namespace ArmoredCar } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }