Первый этап Lab_8

This commit is contained in:
Максим Егоров 2024-10-06 18:22:56 +04:00
parent a5cfd9fbe4
commit 01b12c835a
5 changed files with 193 additions and 22 deletions

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
using Sailboat.Entities;
namespace Sailboat.Generics
{
internal class BoatCompareByColor : IComparer<DrawingBoat?>
{
public int Compare(DrawingBoat? x, DrawingBoat? y)
{
if (x == null || x.EntityBoat == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityBoat == null)
{
throw new ArgumentNullException(nameof(y));
}
var bodyColorCompare = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name);
if (bodyColorCompare != 0)
{
return bodyColorCompare;
}
if (x.EntityBoat is EntitySailboat xEntitySailboat && y.EntityBoat is EntitySailboat yEntitySailboat)
{
var dumpBoxColorCompare = xEntitySailboat.BodyColor.Name.CompareTo(yEntitySailboat.BodyColor.Name);
if (dumpBoxColorCompare != 0)
{
return dumpBoxColorCompare;
}
var tentColorCompare = xEntitySailboat.AdditionalColor.Name.CompareTo(yEntitySailboat.AdditionalColor.Name);
if (tentColorCompare != 0)
{
return tentColorCompare;
}
}
var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight);
}
}
}

View File

@ -0,0 +1,35 @@
using Sailboat.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.Generics
{
internal class BoatCompareByType : IComparer<DrawingBoat?>
{
public int Compare(DrawingBoat? x, DrawingBoat? y)
{
if (x == null || x.EntityBoat == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityBoat == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare =
x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight);
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.Generics
{
internal class BoatsCollectionInfo : IEquatable<BoatsCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public BoatsCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(BoatsCollectionInfo? other)
{
if (Name == other?.Name)
return true;
return false;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
using Sailboat.Entities;
namespace Sailboat.Generics
{
internal class DrawingBoatEqutables : IEqualityComparer<DrawingBoat?>
{
public bool Equals(DrawingBoat? x, DrawingBoat? y)
{
if (x == null || x.EntityBoat == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityBoat == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityBoat.Speed != y.EntityBoat.Speed)
{
return false;
}
if (x.EntityBoat.Weight != y.EntityBoat.Weight)
{
return false;
}
if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor)
{
return false;
}
if (x is DrawingSailboat && y is DrawingSailboat)
{
EntitySailboat EntityX = (EntitySailboat)x.EntityBoat;
EntitySailboat EntityY = (EntitySailboat)y.EntityBoat;
if (EntityX.Sail != EntityY.Sail)
return false;
if (EntityX.Hull != EntityY.Hull)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawingBoat obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -22,6 +22,8 @@ namespace Sailboat.Generics
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Конструктор
/// </summary>
@ -36,14 +38,9 @@ namespace Sailboat.Generics
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <returns></returns>
public bool Insert(T boat)
public int Insert(T boat, IEqualityComparer<T?>? equal = null)
{
if (_places.Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
Insert(boat, 0);
return true;
return Insert(boat, 0, equal);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -51,16 +48,19 @@ namespace Sailboat.Generics
/// <param name="boat">Добавляемая лодка</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T boat, int position)
public int Insert(T boat, int position, IEqualityComparer<T?>? equal = null)
{
if (_places.Count == _maxCount)
if (position < 0 || position > Count)
throw new BoatNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
if (equal != null && _places.Contains(boat, equal))
{
return false;
return -1;
}
_places.Insert(position, boat);
return true;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -69,10 +69,9 @@ namespace Sailboat.Generics
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= Count)
{
if (position < 0 || position > _maxCount || position >= Count)
throw new BoatNotFoundException(position);
}
_places.RemoveAt(position);
return true;
}
@ -85,17 +84,15 @@ namespace Sailboat.Generics
{
get
{
if (position < 0 || position >= Count)
{
return null;
if (position < 0 || position >= Count) {
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
if (!(position >= 0 && position < Count && _places.Count < _maxCount)) {
return;
}
_places.Insert(position, value);
return;
@ -117,4 +114,4 @@ namespace Sailboat.Generics
}
}
}
}
}