From 9387eb7e2f6cba29cd7b412cc6a87245f3d791c3 Mon Sep 17 00:00:00 2001
From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com>
Date: Sun, 25 Dec 2022 10:42:59 +0400
Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD?=
=?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Boats/Boats/DrawingObjectBoat.cs | 43 +++++++++++++++++++++++++++
Boats/Boats/IDrawingObject.cs | 2 +-
Boats/Boats/MapWithSetBoatsGeneric.cs | 2 +-
Boats/Boats/SetBoatsGeneric.cs | 7 ++++-
4 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/Boats/Boats/DrawingObjectBoat.cs b/Boats/Boats/DrawingObjectBoat.cs
index 2209608..add9704 100644
--- a/Boats/Boats/DrawingObjectBoat.cs
+++ b/Boats/Boats/DrawingObjectBoat.cs
@@ -32,5 +32,48 @@ namespace Boats
}
public string GetInfo() => _boat?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectBoat(data.CreateDrawingBoat());
+
+ public bool Equals(IDrawingObject? other)
+ {
+ if (other == null)
+ {
+ return false;
+ }
+ var otherBoat = other as DrawingObjectBoat;
+ if (otherBoat == null)
+ {
+ return false;
+ }
+ var boat = _boat.Boat;
+ var otherBoatBoat = otherBoat._boat.Boat;
+ if (boat.Speed != otherBoatBoat.Speed)
+ {
+ return false;
+ }
+ if (boat.Weight != otherBoatBoat.Weight)
+ {
+ return false;
+ }
+ if (boat.BodyColor != otherBoatBoat.BodyColor)
+ {
+ return false;
+ }
+ if (boat is EntityCatamaran catamaran && otherBoatBoat is EntityCatamaran otherCatamaran)
+ {
+ if (catamaran.Sail != otherCatamaran.Sail)
+ {
+ return false;
+ }
+ if (catamaran.Bobbers != otherCatamaran.Bobbers)
+ {
+ return false;
+ }
+ if (catamaran.DopColor != otherCatamaran.DopColor)
+ {
+ return false;
+ }
+ }
+ return true;
+ }
}
}
diff --git a/Boats/Boats/IDrawingObject.cs b/Boats/Boats/IDrawingObject.cs
index 302d18d..da58ed9 100644
--- a/Boats/Boats/IDrawingObject.cs
+++ b/Boats/Boats/IDrawingObject.cs
@@ -9,7 +9,7 @@ namespace Boats
///
/// Интерфейс для работы с объектом, прорисовываемым на форме
///
- internal interface IDrawingObject
+ internal interface IDrawingObject : IEquatable
{
///
/// Шаг перемещения объекта
diff --git a/Boats/Boats/MapWithSetBoatsGeneric.cs b/Boats/Boats/MapWithSetBoatsGeneric.cs
index 7e330a4..92f94b4 100644
--- a/Boats/Boats/MapWithSetBoatsGeneric.cs
+++ b/Boats/Boats/MapWithSetBoatsGeneric.cs
@@ -12,7 +12,7 @@ namespace Boats
///
///
internal class MapWithSetBoatsGeneric
- where T : class, IDrawingObject
+ where T : class, IDrawingObject, IEquatable
where U : AbstractMap
{
///
diff --git a/Boats/Boats/SetBoatsGeneric.cs b/Boats/Boats/SetBoatsGeneric.cs
index 5def12b..53f70a2 100644
--- a/Boats/Boats/SetBoatsGeneric.cs
+++ b/Boats/Boats/SetBoatsGeneric.cs
@@ -11,7 +11,7 @@ namespace Boats
///
///
internal class SetBoatsGeneric
- where T : class
+ where T : class, IEquatable
{
///
/// Массив объектов, которые храним
@@ -56,6 +56,11 @@ namespace Boats
///
public int Insert(T boat, int position)
{
+ // Проверка на уникальность
+ if (_places.Contains(boat))
+ {
+ throw new ArgumentException($"Объект {boat} уже есть");
+ }
// Проверка позиции
if (position < 0 || position >= _maxCount - 1)
return -1;