From 071713ba600d825486483613dee321ecf0b9a659 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Tue, 29 Nov 2022 17:38:21 +0400 Subject: [PATCH] =?UTF-8?q?1=20=D1=88=D0=B0=D0=B3(IEuatable)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Warship/Warship/DrawingObjectWarship.cs | 43 ++++++++++++++++++++ Warship/Warship/IDrawingObject.cs | 2 +- Warship/Warship/MapWithSetWarshipsGeneric.cs | 2 +- Warship/Warship/SetWarshipsGeneric.cs | 9 +++- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Warship/Warship/DrawingObjectWarship.cs b/Warship/Warship/DrawingObjectWarship.cs index f841af0..6c77dee 100644 --- a/Warship/Warship/DrawingObjectWarship.cs +++ b/Warship/Warship/DrawingObjectWarship.cs @@ -39,5 +39,48 @@ namespace Warship public string GetInfo() => _warship?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship()); + + public bool Equals(IDrawingObject? other) + { + if (other == null) + return false; + + var otherWarship = other as DrawingObjectWarship; + + if (otherWarship == null) + return false; + + var warship = _warship.Warship; + var otherWarshipWarship = otherWarship._warship.Warship; + + if (warship.GetType() != otherWarshipWarship.GetType()) + return false; + + if (warship.Speed != otherWarshipWarship.Speed) + return false; + + if (warship.Weight != otherWarshipWarship.Weight) + return false; + + if (warship.BodyColor != otherWarshipWarship.BodyColor) + return false; + + if(warship is EntityAdvancedWarship adv && otherWarshipWarship is EntityAdvancedWarship otherAdv) + { + if (adv.DopColor != otherAdv.DopColor) + return false; + + if (adv.Antenna != otherAdv.Antenna) + return false; + + if (adv.Helipad != otherAdv.Helipad) + return false; + + if (adv.Missile != otherAdv.Missile) + return false; + } + + return true; + } } } diff --git a/Warship/Warship/IDrawingObject.cs b/Warship/Warship/IDrawingObject.cs index 00feb14..df26652 100644 --- a/Warship/Warship/IDrawingObject.cs +++ b/Warship/Warship/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Warship { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs index df158ca..f15a69c 100644 --- a/Warship/Warship/MapWithSetWarshipsGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Warship { internal class MapWithSetWarshipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs index 977fb0a..5362761 100644 --- a/Warship/Warship/SetWarshipsGeneric.cs +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Warship { internal class SetWarshipsGeneric - where T : class + where T : class, IEquatable { private readonly List _places; @@ -25,15 +25,22 @@ namespace Warship { if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + _places.Insert(0, warship); + return 0; } public int Insert(T warship, int position) { + if (_places.Contains(warship)) + return -1; + if (position >= _maxCount || position < 0) throw new StorageOverflowException(_maxCount); + _places.Insert(position, warship); + return position; }