From a0a018477f27f43702a025a1532ca86838e74d65 Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Wed, 27 Dec 2023 08:38:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawiningTractorEqutables.cs | 52 +++++++++++++++++++ .../RPP_FirstLaba_Tractor/SetGeneric.cs | 17 ++++-- .../TractorCompareByColor.cs | 32 ++++++++++++ .../TractorCompareByType.cs | 36 +++++++++++++ .../TractorsGenericCollection.cs | 6 +++ 5 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/DrawiningTractorEqutables.cs create mode 100644 RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByColor.cs create mode 100644 RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByType.cs diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/DrawiningTractorEqutables.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/DrawiningTractorEqutables.cs new file mode 100644 index 0000000..2e0b03f --- /dev/null +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/DrawiningTractorEqutables.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTractor.DrawningObjects; +using ProjectTractor.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectTractor.Generics +{ + internal class DrawiningTractorEqutables : IEqualityComparer + { + public bool Equals(DrawningTractor? x, DrawningTractor? y) + { + if (x == null || x.EntityTractor == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTractor == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityTractor.Speed != y.EntityTractor.Speed) + { + return false; + } + if (x.EntityTractor.Weight != y.EntityTractor.Weight) + { + return false; + } + if (x.EntityTractor.BodyColor != y.EntityTractor.BodyColor) + { + return false; + } + if (x is DrawningBulldoser && y is DrawningBulldoser) + { + // TODO доделать логику сравнения дополнительных параметров + } + return true; + } + public int GetHashCode([DisallowNull] DrawningTractor obj) + { + return obj.GetHashCode(); + } + } + +} diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs index d3f8a13..22d9918 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/SetGeneric.cs @@ -32,15 +32,21 @@ namespace ProjectTractor.Generics _places = new List(count); } /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => + _places.Sort(comparer); + /// /// Добавление объекта в набор /// /// Добавляемый трактор /// - public bool Insert(T tractor) + public bool Insert(T tractor, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(tractor, 0); + Insert(tractor, 0, equal); return true; } /// @@ -49,12 +55,17 @@ namespace ProjectTractor.Generics /// Добавляемый автомобиль /// Позиция /// - public void Insert(T tractor, int position) + public void Insert(T tractor, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!(position >= 0 && position <= Count)) throw new Exception("Неверная позиция для вставки"); + if (equal != null) + { + if (_places.Contains(tractor, equal)) + throw new ArgumentException(nameof(tractor)); + } _places.Insert(position, tractor); } /// diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByColor.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByColor.cs new file mode 100644 index 0000000..fec4f0f --- /dev/null +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByColor.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTractor.DrawningObjects; + +namespace ProjectTractor.Generics +{ + internal class TractorCompareByColor : IComparer + { + public int Compare(DrawningTractor? x, DrawningTractor? y) + { + if (x == null || x.EntityTractor == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityTractor == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityTractor.BodyColor.Name != y.EntityTractor.BodyColor.Name) + { + return x.EntityTractor.BodyColor.Name.CompareTo(y.EntityTractor.BodyColor.Name); + } + + var speedCompare = x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed); + if (speedCompare != 0) + return speedCompare; + + return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight); + } + } +} diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByType.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByType.cs new file mode 100644 index 0000000..a9ccdd8 --- /dev/null +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorCompareByType.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTractor.DrawningObjects; + +namespace ProjectTractor.Generics +{ + internal class TractorCompareByType : IComparer + { + public int Compare(DrawningTractor? x, DrawningTractor? y) + { + if (x == null || x.EntityTractor == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTractor == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight); + } + } + +} diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs index 8cc895e..61e3386 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericCollection.cs @@ -55,6 +55,12 @@ namespace ProjectTractor.Generics _collection = new SetGeneric(width * height); } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => + _collection.SortSet(comparer); + /// /// Перегрузка оператора сложения /// ///