Первый шаг: проверка уникальности объекта

This commit is contained in:
prodigygirl 2022-11-30 17:36:00 +04:00
parent af1efb4a3d
commit e27ebecf0e
4 changed files with 60 additions and 6 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.DirectoryServices.ActiveDirectory;
using System.Drawing;
using System.Linq;
using System.Text;
@ -34,7 +35,54 @@ namespace ArmoredCar
_armCar.DrawTransport(g);
}
public string GetInfo() => _armCar?.GetDataForSave();
public static IDrawningObject Create(string data) => new
DrawningObjectArmCar(data.CreateDrawningArmoredCar());
public static IDrawningObject Create(string data) => new DrawningObjectArmCar(data.CreateDrawningArmoredCar());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherCar = other as DrawningObjectArmCar;
if (otherCar == null)
{
return false;
}
var car = _armCar.ArmoredCar;
var otherCarCar = otherCar._armCar.ArmoredCar;
if (car.GetType() != otherCarCar.GetType())
return false;
if (car.Speed != otherCarCar.Speed)
{
return false;
}
if (car.Weight != otherCarCar.Weight)
{
return false;
}
if (car.BodyColor != otherCarCar.BodyColor)
{
return false;
}
if (car is EntityTank tank && otherCarCar is EntityTank otherTank)
{
if (tank.DopColor != otherTank.DopColor)
{
return false;
}
if (tank.TowerWeapon != otherTank.TowerWeapon)
{
return false;
}
if (tank.AMachineGun != otherTank.AMachineGun)
{
return false;
}
}
return true;
}
}
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace ArmoredCar
{
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace ArmoredCar
{
internal class MapWithSetArmoredCarsGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace ArmoredCar
{
internal class SetArmoredCarsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -43,13 +43,19 @@ namespace ArmoredCar
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T armoredCar, int position)
{
{
if (Count >= _maxCount)
throw new StorageOverflowException(Count);
if (position < 0 || position >= _maxCount)
return -1;
// проверка на уникальность
if (_places.Contains(armoredCar))
{
return -1;
}
_places.Insert(position, armoredCar);
return position;
}