Начало Lab8. Создание класса Equatables, дополнения в других классах

This commit is contained in:
malimova 2023-12-25 18:37:32 +04:00
parent 76e036db5b
commit 075103c116
6 changed files with 77 additions and 9 deletions

View File

@ -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
{

View File

@ -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
{

View File

@ -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<DrawningAirPlane?>
{
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();
}
}
}

View File

@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using AirBomber;
namespace AirBomber
namespace AirBomber.Entities
{
public class EntityAirBomber : EntityAirPlane
{

View File

@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using AirBomber;
namespace AirBomber
namespace AirBomber.Entities
{
public class EntityAirPlane
{

View File

@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using AirBomber.Exceptions;
namespace AirBomber
namespace AirBomber.Generics
{
internal class SetGeneric<T>
where T : class
@ -31,14 +31,15 @@ namespace AirBomber
_maxCount = count;
_places = new List<T?>(count);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="plane">Добавляемый самолет</param>
/// <returns></returns>
public bool Insert(T plane)
public bool Insert(T plane, IEqualityComparer<T?>? equal = null)
{
return Insert(plane, 0);
return Insert(plane, 0, equal);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -46,13 +47,17 @@ namespace AirBomber
/// <param name="plane">Добавляемый самолет</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T plane, int position)
public bool Insert(T plane, int position, IEqualityComparer<T?>? 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;
}