Добавлена проверка на уникальность в наборе

This commit is contained in:
Данияр Аглиуллов 2022-10-31 06:30:18 +04:00
parent d81aa61eb0
commit c03e76fb60
5 changed files with 41 additions and 4 deletions

View File

@ -21,11 +21,13 @@
var xEntity = xAirplane.Airplane.Airplane;
var yEntity = yAirplane.Airplane.Airplane;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 || xEntity is not EntityAirBomber || yEntity is not EntityAirBomber)
if (colorWeight != 0 ||
xEntity is not EntityAirBomber xEntityAirBomber ||
yEntity is not EntityAirBomber yEntityAirBomber)
{
return colorWeight;
}
return ((EntityAirBomber)xEntity).DopColor.ToArgb().CompareTo(((EntityAirBomber)yEntity).DopColor.ToArgb());
return xEntityAirBomber.DopColor.ToArgb().CompareTo(yEntityAirBomber.DopColor.ToArgb());
}
}
}

View File

@ -40,5 +40,31 @@ namespace AirBomber
public string GetInfo() => Airplane?.GetDataForSave() ?? string.Empty;
public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane());
public bool Equals(IDrawningObject? other)
{
if (other is not DrawningObject otherAirplane)
{
return false;
}
var entity = Airplane.Airplane;
var otherEntity = otherAirplane.Airplane.Airplane;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntityAirBomber entityAirBomber &&
otherEntity is EntityAirBomber otherEntityAirBomber && (
entityAirBomber.HasBombs != otherEntityAirBomber.HasBombs ||
entityAirBomber.DopColor != otherEntityAirBomber.DopColor ||
entityAirBomber.HasFuelTanks != otherEntityAirBomber.HasFuelTanks))
{
return false;
}
return true;
}
}
}

View File

@ -149,6 +149,11 @@ namespace AirBomber
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, airplane);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>

View File

@ -9,7 +9,7 @@ namespace AirBomber
/// <summary>
/// Интерфейс для работы с объектом, прорисовываемым на форме
/// </summary>
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -11,7 +11,7 @@ namespace AirBomber
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetAirplanesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -51,9 +51,13 @@ namespace AirBomber
/// </summary>
/// <param name="airplane">Добавляемый самолет</param>
/// <param name="position">Позиция</param>
/// <exception cref="ArgumentException"> Исключения генерируется при попытке добавить дубликат в набор</exception>
/// <exception cref="StorageOverflowException"> </exception>
/// <returns>Возвращает позицию вставленного объекта, либо -1 если его не удалось вставить</returns>
public int Insert(T airplane, int position)
{
if (_places.Contains(airplane))
throw new ArgumentException($"Объект {airplane} уже есть в наборе");
if (Count == _maxcount)
throw new StorageOverflowException(_maxcount);
if (!isCorrectPosition(position))