From 01b12c835a41bb01c69ce8390616233671dc424b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=95=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2?= Date: Sun, 6 Oct 2024 18:22:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D1=8D?= =?UTF-8?q?=D1=82=D0=B0=D0=BF=20Lab=5F8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatCompareByColor.cs | 50 +++++++++++++++++++ Sailboat/Sailboat/BoatCompareByType.cs | 35 ++++++++++++++ Sailboat/Sailboat/BoatsCollectionInfo.cs | 30 ++++++++++++ Sailboat/Sailboat/DrawingBoatEqutables.cs | 59 +++++++++++++++++++++++ Sailboat/Sailboat/SetGeneric.cs | 41 ++++++++-------- 5 files changed, 193 insertions(+), 22 deletions(-) create mode 100644 Sailboat/Sailboat/BoatCompareByColor.cs create mode 100644 Sailboat/Sailboat/BoatCompareByType.cs create mode 100644 Sailboat/Sailboat/BoatsCollectionInfo.cs create mode 100644 Sailboat/Sailboat/DrawingBoatEqutables.cs diff --git a/Sailboat/Sailboat/BoatCompareByColor.cs b/Sailboat/Sailboat/BoatCompareByColor.cs new file mode 100644 index 0000000..b3aae56 --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByColor.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics +{ + internal class BoatCompareByColor : IComparer + { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityBoat is EntitySailboat xEntitySailboat && y.EntityBoat is EntitySailboat yEntitySailboat) + { + var dumpBoxColorCompare = xEntitySailboat.BodyColor.Name.CompareTo(yEntitySailboat.BodyColor.Name); + if (dumpBoxColorCompare != 0) + { + return dumpBoxColorCompare; + } + var tentColorCompare = xEntitySailboat.AdditionalColor.Name.CompareTo(yEntitySailboat.AdditionalColor.Name); + if (tentColorCompare != 0) + { + return tentColorCompare; + } + } + var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } + } +} diff --git a/Sailboat/Sailboat/BoatCompareByType.cs b/Sailboat/Sailboat/BoatCompareByType.cs new file mode 100644 index 0000000..32419f8 --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByType.cs @@ -0,0 +1,35 @@ +using Sailboat.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + internal class BoatCompareByType : IComparer + { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } + } +} diff --git a/Sailboat/Sailboat/BoatsCollectionInfo.cs b/Sailboat/Sailboat/BoatsCollectionInfo.cs new file mode 100644 index 0000000..9a20a0f --- /dev/null +++ b/Sailboat/Sailboat/BoatsCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + internal class BoatsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BoatsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BoatsCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/Sailboat/Sailboat/DrawingBoatEqutables.cs b/Sailboat/Sailboat/DrawingBoatEqutables.cs new file mode 100644 index 0000000..e66d5c0 --- /dev/null +++ b/Sailboat/Sailboat/DrawingBoatEqutables.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics +{ + internal class DrawingBoatEqutables : IEqualityComparer + { + public bool Equals(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBoat.Speed != y.EntityBoat.Speed) + { + return false; + } + if (x.EntityBoat.Weight != y.EntityBoat.Weight) + { + return false; + } + if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor) + { + return false; + } + if (x is DrawingSailboat && y is DrawingSailboat) + { + EntitySailboat EntityX = (EntitySailboat)x.EntityBoat; + EntitySailboat EntityY = (EntitySailboat)y.EntityBoat; + if (EntityX.Sail != EntityY.Sail) + return false; + if (EntityX.Hull != EntityY.Hull) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingBoat obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index b3d3f93..72cf481 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -22,6 +22,8 @@ namespace Sailboat.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); + /// /// Конструктор /// @@ -36,14 +38,9 @@ namespace Sailboat.Generics /// /// Добавляемая лодка /// - public bool Insert(T boat) + public int Insert(T boat, IEqualityComparer? equal = null) { - if (_places.Count == _maxCount) - { - throw new StorageOverflowException(_maxCount); - } - Insert(boat, 0); - return true; + return Insert(boat, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -51,16 +48,19 @@ namespace Sailboat.Generics /// Добавляемая лодка /// Позиция /// - public bool Insert(T boat, int position) + public int Insert(T boat, int position, IEqualityComparer? equal = null) { - if (_places.Count == _maxCount) + if (position < 0 || position > Count) + throw new BoatNotFoundException(position); + if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - if (!(position >= 0 && position <= Count)) + if (equal != null && _places.Contains(boat, equal)) { - return false; + + return -1; } _places.Insert(position, boat); - return true; + return position; } /// /// Удаление объекта из набора с конкретной позиции @@ -69,10 +69,9 @@ namespace Sailboat.Generics /// public bool Remove(int position) { - if (position < 0 || position >= Count) - { + if (position < 0 || position > _maxCount || position >= Count) throw new BoatNotFoundException(position); - } + _places.RemoveAt(position); return true; } @@ -85,17 +84,15 @@ namespace Sailboat.Generics { get { - if (position < 0 || position >= Count) - { - return null; + if (position < 0 || position >= Count) { + return null; } return _places[position]; } set { - if (!(position >= 0 && position < Count && _places.Count < _maxCount)) - { - return; + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) { + return; } _places.Insert(position, value); return; @@ -117,4 +114,4 @@ namespace Sailboat.Generics } } } -} \ No newline at end of file +}