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; }