From 075103c1162dbdfc3f5a6026a010e6a960da8f10 Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 25 Dec 2023 18:37:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20Lab8.=20?= =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D0=B0=20Equatables,=20=D0=B4=D0=BE=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=B4?= =?UTF-8?q?=D1=80=D1=83=D0=B3=D0=B8=D1=85=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawningAirBomber.cs | 3 +- AirBomber/AirBomber/DrawningAirPlane.cs | 4 +- .../AirBomber/DrawningPlanesEquatables.cs | 62 +++++++++++++++++++ AirBomber/AirBomber/EntityAirBomber.cs | 2 +- AirBomber/AirBomber/EntityAirPlane.cs | 2 +- AirBomber/AirBomber/SetGeneric.cs | 13 ++-- 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 AirBomber/AirBomber/DrawningPlanesEquatables.cs diff --git a/AirBomber/AirBomber/DrawningAirBomber.cs b/AirBomber/AirBomber/DrawningAirBomber.cs index 003679d..4d09f40 100644 --- a/AirBomber/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/AirBomber/DrawningAirBomber.cs @@ -5,8 +5,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using AirBomber; +using AirBomber.Entities; -namespace AirBomber +namespace AirBomber.DrawningObjects { public class DrawningAirBomber : DrawningAirPlane { diff --git a/AirBomber/AirBomber/DrawningAirPlane.cs b/AirBomber/AirBomber/DrawningAirPlane.cs index 56a60f7..869bff7 100644 --- a/AirBomber/AirBomber/DrawningAirPlane.cs +++ b/AirBomber/AirBomber/DrawningAirPlane.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirBomber; +using AirBomber.Entities; -namespace AirBomber +namespace AirBomber.DrawningObjects { public class DrawningAirPlane { diff --git a/AirBomber/AirBomber/DrawningPlanesEquatables.cs b/AirBomber/AirBomber/DrawningPlanesEquatables.cs new file mode 100644 index 0000000..7042fd2 --- /dev/null +++ b/AirBomber/AirBomber/DrawningPlanesEquatables.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawningObjects; +using AirBomber.Entities; + +namespace AirBomber +{ + internal class DrawningPlanesEquatables: IEqualityComparer + { + public bool Equals(DrawningAirPlane? x, DrawningAirPlane? y) + { + if (x == null || x.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityAirPlane.Speed != y.EntityAirPlane.Speed) + { + return false; + } + if (x.EntityAirPlane.Weight != y.EntityAirPlane.Weight) + { + return false; + } + if (x.EntityAirPlane.BodyColor != y.EntityAirPlane.BodyColor) + { + return false; + } + if (x is DrawningAirBomber && y is DrawningAirBomber) + { + EntityAirBomber xAirBomber = (EntityAirBomber)x.EntityAirPlane; + EntityAirBomber yAirBomber = (EntityAirBomber)y.EntityAirPlane; + + if (xAirBomber.Bombs != yAirBomber.Bombs) + return false; + + if (xAirBomber.FuelTanks != yAirBomber.FuelTanks) + return false; + + if (xAirBomber.AdditionalColor != yAirBomber.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningAirPlane obj) + { + return obj.GetHashCode(); + } + } +} + diff --git a/AirBomber/AirBomber/EntityAirBomber.cs b/AirBomber/AirBomber/EntityAirBomber.cs index f8b76af..4efd868 100644 --- a/AirBomber/AirBomber/EntityAirBomber.cs +++ b/AirBomber/AirBomber/EntityAirBomber.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber; -namespace AirBomber +namespace AirBomber.Entities { public class EntityAirBomber : EntityAirPlane { diff --git a/AirBomber/AirBomber/EntityAirPlane.cs b/AirBomber/AirBomber/EntityAirPlane.cs index 418142f..d3650df 100644 --- a/AirBomber/AirBomber/EntityAirPlane.cs +++ b/AirBomber/AirBomber/EntityAirPlane.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber; -namespace AirBomber +namespace AirBomber.Entities { public class EntityAirPlane { diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 0b1c8bd..168cf51 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber.Exceptions; -namespace AirBomber +namespace AirBomber.Generics { internal class SetGeneric where T : class @@ -31,14 +31,15 @@ namespace AirBomber _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый самолет /// - public bool Insert(T plane) + public bool Insert(T plane, IEqualityComparer? equal = null) { - return Insert(plane, 0); + return Insert(plane, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -46,13 +47,17 @@ namespace AirBomber /// Добавляемый самолет /// Позиция /// - public bool Insert(T plane, int position) + public bool Insert(T plane, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new StorageOverflowException("Невозможно добавить"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + + if (equal != null && _places.Contains(plane, equal)) + throw new ArgumentException("Такой объект уже есть в коллекции"); + _places.Insert(0, plane); return true; }