From 81924c5d001ec770c8e689a8d73dde22db2f1764 Mon Sep 17 00:00:00 2001 From: Victoria_Isaeva Date: Thu, 16 May 2024 00:17:06 +0400 Subject: [PATCH] =?UTF-8?q?8=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 15 +++--- .../ICollectionGenericObjects.cs | 10 ++-- .../ListGenericObjects.cs | 33 ++++++++++-- .../MassiveGenericObjects.cs | 42 +++++++++++---- .../Drawnings/DrawiningBusEqutables.cs | 51 +++++++++++++++++++ .../Drawnings/DrawningBusCompareByColor.cs | 37 ++++++++++++++ .../Drawnings/DrawningBusCompareByType.cs | 32 ++++++++++++ 7 files changed, 195 insertions(+), 25 deletions(-) create mode 100644 ProjectAirbus/ProjectAirbus/Drawnings/DrawiningBusEqutables.cs create mode 100644 ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByColor.cs create mode 100644 ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByType.cs diff --git a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/AbstractCompany.cs b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/AbstractCompany.cs index 2d2deee..4e454b8 100644 --- a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/AbstractCompany.cs @@ -58,9 +58,9 @@ public abstract class AbstractCompany /// Компания /// Добавляемый объект /// - public static int operator +(AbstractCompany company, DrawningBus bus) + public static bool operator +(AbstractCompany company, DrawningBus bus) { - return company._collection?.Insert(bus) ?? -1; + return company._collection?.Insert(bus, new DrawiningBusEqutables()) ?? false; } /// @@ -71,7 +71,7 @@ public abstract class AbstractCompany /// public static DrawningBus operator -(AbstractCompany company, int position) { - return company._collection?.Remove(position) ?? null; + return company._collection.Remove(position); } /// @@ -102,14 +102,15 @@ public abstract class AbstractCompany DrawningBus? obj = _collection?.Get(i); obj?.DrawTransport(graphics); } - catch (ObjectNotFoundException e) - { } - catch (PositionOutOfCollectionException e) - { } + catch (Exception e) { } + + } return bitmap; } + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); + /// /// Вывод заднего фона diff --git a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ICollectionGenericObjects.cs index 9df6a33..8f16f5d 100644 --- a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,4 +1,5 @@ -using System; +using ProjectAirbus.Drawnings; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,13 +13,14 @@ public interface ICollectionGenericObjects { int Count { get; } int MaxCount{get;set;} - int Insert(T obj); - int Insert(T obj, int position); - T Remove(int position); + int Insert(T obj, IEqualityComparer? comparer = null ); + int Insert(T obj, int position, IEqualityComparer? comparer = null); + T? Remove(int position); T? Get(int position); CollectionType GetCollectionType { get; } IEnumerable GetItems(); + void CollectionSort(IComparer comparer); } diff --git a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ListGenericObjects.cs b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ListGenericObjects.cs index 9bf3716..e979ba2 100644 --- a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/ListGenericObjects.cs @@ -1,5 +1,7 @@ using ProjectAirbus.CollectionGenericObjects; +using ProjectAirbus.Drawnings; using ProjectAirbus.Exceptions; +using System.Linq; namespace ProjectAirBus.CollectionGenericObjects; @@ -18,11 +20,11 @@ public class ListGenericObjects : ICollectionGenericObjects /// /// Максимально допустимое число объектов в списке /// - private int _maxCount = 10; + private int _maxCount; public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } public CollectionType GetCollectionType => CollectionType.List; @@ -43,25 +45,41 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO выброс ошибки если переполнение + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectIsEqualException(); + } + } + if (Count == _maxCount) throw new CollectionOverflowException(Count); _collection.Add(obj); return Count; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO выброс ошибки если переполнение // TODO выброс ошибки если за границу + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectIsEqualException(); + } + } + if (Count == _maxCount) throw new CollectionOverflowException(Count); if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); _collection.Insert(position, obj); return position; } - public T? Remove(int position) + public T Remove(int position) { // TODO если выброс за границу @@ -77,4 +95,9 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } + } diff --git a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/MassiveGenericObjects.cs index 58634ea..16d6860 100644 --- a/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectAirbus/ProjectAirbus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -using ProjectAirbus.Exceptions; +using ProjectAirbus.Drawnings; +using ProjectAirbus.Exceptions; namespace ProjectAirbus.CollectionGenericObjects; @@ -55,9 +56,18 @@ internal class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO выброс ошибки если переполнение + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawingBus, item as DrawingBus)) + throw new ObjectIsEqualException(); + } + } + int index = 0; while (index < _collection.Length) { @@ -66,17 +76,25 @@ internal class MassiveGenericObjects : ICollectionGenericObjects _collection[index] = obj; return index; } - ++index; + index++; } throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - // TODO выброс ошибки если переполнение - // TODO выброс ошибки если выход за границу + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawingBus, item as DrawingBus)) + throw new ObjectIsEqualException(); + } + } + if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); - if (_collection[position] == null) + + if (_collection[position] == null) { _collection[position] = obj; return position; @@ -89,7 +107,7 @@ internal class MassiveGenericObjects : ICollectionGenericObjects _collection[index] = obj; return index; } - ++index; + index++; } index = position - 1; while (index >= 0) @@ -99,9 +117,10 @@ internal class MassiveGenericObjects : ICollectionGenericObjects _collection[index] = obj; return index; } - --index; + index--; } throw new CollectionOverflowException(Count); + } public T? Remove(int position) @@ -121,6 +140,11 @@ internal class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } diff --git a/ProjectAirbus/ProjectAirbus/Drawnings/DrawiningBusEqutables.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DrawiningBusEqutables.cs new file mode 100644 index 0000000..7e60c22 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DrawiningBusEqutables.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirbus.Drawnings; +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawiningBusEqutables : IEqualityComparer +{ + public bool Equals(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + { + return false; + } + if (y == null || y.EntityBus == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBus.Speed != y.EntityBus.Speed) + { + return false; + } + if (x.EntityBus.Weight != y.EntityBus.Weight) + { + return false; + } + if (x.EntityBus.BodyColor != y.EntityBus.BodyColor) + { + return false; + } + if (x is DrawningAirbus && y is DrawningAirbus) + { + // TODO доделать логику сравнения дополнительных параметров + + } + return true; + } + public int GetHashCode([DisallowNull] DrawningBus obj) + { + return obj.GetHashCode(); + } +} \ No newline at end of file diff --git a/ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByColor.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByColor.cs new file mode 100644 index 0000000..d387353 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByColor.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirbus.Drawnings; + +public class DrawningBusCompareByColor : IComparer +{ + public int Compare(DrawningBus? x, DrawningBus? y) + { + // TODO прописать логику сравения по цветам, скорости, весу + if (x == null || x.EntityBus == null) + { + return 1; + } + + if (y == null || y.EntityBus == null) + { + return -1; + } + var bodycolorCompare = x.EntityBus.BodyColor.Name.CompareTo(y.EntityBus.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + var speedCompare = x.EntityBus.Speed.CompareTo(y.EntityBus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight); + + throw new NotImplementedException(); + } +} diff --git a/ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByType.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByType.cs new file mode 100644 index 0000000..2ce1eeb --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DrawningBusCompareByType.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirbus.Drawnings; + +internal class DrawningBusCompareByType : IComparer +{ + public int Compare(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + { + return -1; + } + if (y == null || y.EntityBus == null) + { + return 1; + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityBus.Speed.CompareTo(y.EntityBus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight); + } +}