diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs index 4c5e96d..c6aeece 100644 --- a/ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs @@ -1,6 +1,5 @@ -using ProjectAircraftCarrier_.Exceptions; +using ProjectGasMachine.Exceptions; using ProjectGasMachine.Drawnings; -using ProjectGasMachine.Exceptions; using System; using System.Collections.Generic; using System.Linq; diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/ICollectionGenericObjects.cs index 4d8a8b7..72b4572 100644 --- a/ProjectCar/ProjectCar/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,12 +1,11 @@ -using ProjectGasMachine.CollectionGenericObjects; -using ProjectGasMachine.Drawnings; +using ProjectGasMachine.Drawnings; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectGasMachine; +namespace ProjectGasMachine.CollectionGenericObjects; /// /// Интерфейс описания действий для набора хранимых объектов @@ -31,7 +30,7 @@ public interface ICollectionGenericObjects /// Добавляемый объект /// Сравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, IEqualityComparer? comparer = null); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию @@ -40,7 +39,7 @@ public interface ICollectionGenericObjects /// Позиция /// Сравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, int position, IEqualityComparer? comparer = null); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/ListGenericObjects.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/ListGenericObjects.cs index 75c20dc..7a5bcc0 100644 --- a/ProjectCar/ProjectCar/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/ListGenericObjects.cs @@ -1,7 +1,7 @@  -using ProjectAircraftCarrier_.Exceptions; using ProjectGasMachine.Drawnings; using ProjectGasMachine.Exceptions; +using System.Linq; namespace ProjectGasMachine.CollectionGenericObjects; @@ -48,14 +48,14 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj, IEqualityComparer? comparer = null) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если переполнение - for (int i = 0; i < Count; i++) + if (comparer != null) { - if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine))) + if (_collection.Contains(obj, comparer)) { - throw new ObjectAlreadyInCollectionException(i); + throw new ObjectAlreadyInCollectionException(); } } if (Count == _maxCount) @@ -67,15 +67,15 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection.Count; } - public int Insert(T obj, int position, IEqualityComparer? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если выход за границы списка // TODO выброс ошибки, если переполнение - for (int i = 0; i < Count; i++) + if (comparer != null) { - if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine))) + if (_collection.Contains(obj, comparer)) { - throw new ObjectAlreadyInCollectionException(i); + throw new ObjectAlreadyInCollectionException(); } } if (position < 0 || position > Count) diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs index 156d640..1d7281a 100644 --- a/ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,6 +1,4 @@ - -using ProjectAircraftCarrier_.Exceptions; -using ProjectGasMachine.Drawnings; +using ProjectGasMachine.Drawnings; using ProjectGasMachine.Exceptions; using System.Linq; @@ -71,18 +69,21 @@ public class MassiveGenericObjects : ICollectionGenericObjects } - public int Insert(T obj, IEqualityComparer? comparer = null) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если переполнение - - for (int i = 0; i < Count; i++) + if (comparer != null) { - if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine))) + foreach (T? item in _collection) { - throw new ObjectAlreadyInCollectionException(i); + if ((comparer as IEqualityComparer).Equals(obj as DrawningMachine, item as DrawningMachine)) + { + throw new ObjectAlreadyInCollectionException(); + } } } + for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -95,15 +96,18 @@ public class MassiveGenericObjects : ICollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position, IEqualityComparer? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если выход за границы массива // TODO выброс ошибки, если переполнение - for (int i = 0; i < Count; i++) + if (comparer != null) { - if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine))) + foreach (T? item in _collection) { - throw new ObjectAlreadyInCollectionException(i); + if ((comparer as IEqualityComparer).Equals(obj as DrawningMachine, item as DrawningMachine)) + { + throw new ObjectAlreadyInCollectionException(); + } } } diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/StorageCollection.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/StorageCollection.cs index 5c89875..9f9c30c 100644 --- a/ProjectCar/ProjectCar/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/StorageCollection.cs @@ -1,5 +1,4 @@ -using ProjectAircraftCarrier_.Exceptions; -using ProjectGasMachine.Drawnings; +using ProjectGasMachine.Drawnings; using ProjectGasMachine.Exceptions; namespace ProjectGasMachine.CollectionGenericObjects; diff --git a/ProjectCar/ProjectCar/Drawnings/DrawningMachineCompareByColor.cs b/ProjectCar/ProjectCar/Drawnings/DrawningMachineCompareByColor.cs index 0144d79..9e00061 100644 --- a/ProjectCar/ProjectCar/Drawnings/DrawningMachineCompareByColor.cs +++ b/ProjectCar/ProjectCar/Drawnings/DrawningMachineCompareByColor.cs @@ -23,10 +23,6 @@ public class DrawningMachineCompareByColor : IComparer { return -1; } - if (x.GetType().Name != y.GetType().Name) - { - return y.GetType().Name.CompareTo(x.GetType().Name); - } var bodyColorCompare = y.EntityGas.BodyColor.Name.CompareTo(x.EntityGas.BodyColor.Name); if (bodyColorCompare != 0) { diff --git a/ProjectCar/ProjectCar/Exceptions/ObjectAlreadyInCollectionException.cs b/ProjectCar/ProjectCar/Exceptions/ObjectAlreadyInCollectionException.cs index 3bd0d10..5c73f8d 100644 --- a/ProjectCar/ProjectCar/Exceptions/ObjectAlreadyInCollectionException.cs +++ b/ProjectCar/ProjectCar/Exceptions/ObjectAlreadyInCollectionException.cs @@ -1,14 +1,14 @@ using System.Runtime.Serialization; -namespace ProjectAircraftCarrier_.Exceptions; +namespace ProjectGasMachine.Exceptions; /// /// Класс, описывающий ошибку переполнения коллекции /// [Serializable] -internal class ObjectAlreadyInCollectionException : ApplicationException +public class ObjectAlreadyInCollectionException : ApplicationException { - public ObjectAlreadyInCollectionException(int index) : base("Такой объект уже присутствует в коллекции. Позиция " + index) { } + public ObjectAlreadyInCollectionException(int count) : base("Такой объект уже присутствует в коллекции. Позиция " + count) { } public ObjectAlreadyInCollectionException() : base() { } diff --git a/ProjectCar/ProjectCar/FormGasMachineCollection.cs b/ProjectCar/ProjectCar/FormGasMachineCollection.cs index 1abb173..59d7916 100644 --- a/ProjectCar/ProjectCar/FormGasMachineCollection.cs +++ b/ProjectCar/ProjectCar/FormGasMachineCollection.cs @@ -51,18 +51,22 @@ namespace ProjectGasMachine /// private void SetMachine(DrawningMachine? machine) { + if (_company == null || machine == null) + { + return; + } try { - if (_company == null || machine == null) - { - return; - } if (_company + machine != -1) { MessageBox.Show("Объект добавлен"); _logger.LogInformation($"Добавлен объект {machine.GetDataForSave()}"); pictureBox.Image = _company.Show(); } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } catch (CollectionOverflowException ex) @@ -70,6 +74,11 @@ namespace ProjectGasMachine MessageBox.Show(ex.Message); _logger.LogWarning($"Ошибка: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Ошибка: {ex.Message}"); + } }