From f5fb0777e64def6773967262fcef1b5eb380850b Mon Sep 17 00:00:00 2001 From: bulatova_karina Date: Wed, 20 Dec 2023 10:41:38 +0300 Subject: [PATCH] laba8: maybe ready --- WarmlyShip/WarmlyShip/FormShipCollection.cs | 9 +++---- WarmlyShip/WarmlyShip/SetGeneric.cs | 24 ++++++++--------- .../WarmlyShip/ShipsGenericCollection.cs | 27 ++++++++++--------- WarmlyShip/WarmlyShip/ShipsGenericStorage.cs | 2 +- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormShipCollection.cs b/WarmlyShip/WarmlyShip/FormShipCollection.cs index 13a2600..d26a33a 100644 --- a/WarmlyShip/WarmlyShip/FormShipCollection.cs +++ b/WarmlyShip/WarmlyShip/FormShipCollection.cs @@ -162,9 +162,9 @@ namespace WarmlyShip return; } - var formShipConfig = new FormShipConfig(); - formShipConfig.AddEvent(AddShip); - formShipConfig.Show(); + var formBoatConfig = new FormShipConfig(); + formBoatConfig.AddEvent(AddShip); + formBoatConfig.Show(); } private void AddShip(DrawingWarmlyShip drawingWarmlyShip) { @@ -178,7 +178,7 @@ namespace WarmlyShip _logger.LogWarning("Добавление пустого объекта"); return; } - if (obj + drawingWarmlyShip) + if (obj + drawingWarmlyShip != -1) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = obj.ShowShips(); @@ -189,7 +189,6 @@ namespace WarmlyShip MessageBox.Show("Не удалось добавить объект"); _logger.LogInformation($"Не удалось добавить объект"); } - } /// /// Удаление объекта из набора diff --git a/WarmlyShip/WarmlyShip/SetGeneric.cs b/WarmlyShip/WarmlyShip/SetGeneric.cs index f103d03..beea5b5 100644 --- a/WarmlyShip/WarmlyShip/SetGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetGeneric.cs @@ -37,12 +37,9 @@ namespace WarmlyShip.Generics /// /// Добавляемый теплоход /// - public bool Insert(T warmlyship, IEqualityComparer? equal = null) + public int Insert(T warmlyship, IEqualityComparer? equal = null) { - if (_places.Count == _maxCount) - throw new StorageOverflowException(_maxCount); - Insert(warmlyship, 0, equal); - return true; + return Insert(warmlyship, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,19 +47,20 @@ namespace WarmlyShip.Generics /// Добавляемый теплоход /// Позиция /// - public bool Insert(T warmlyship, int position, IEqualityComparer? equal = null) + public int Insert(T warmlyship, int position, IEqualityComparer? equal = null) { - if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + if (position < 0 || position > Count) + throw new ShipNotFoundException(position); + if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - if (!(position >= 0 && position <= Count)) - return false; - if (equal != null) + if (equal != null && _places.Contains(warmlyship, equal)) { - if (_places.Contains(warmlyship, equal)) - throw new ArgumentException(nameof(warmlyship)); + //throw new StorageOverflowException("Данный объект уже есть в коллекции"); + //MessageBox.Show("Данный объект уже есть в коллекции"); + return -1; } _places.Insert(position, warmlyship); - return true; + return position; } /// /// Удаление объекта из набора с конкретной позиции diff --git a/WarmlyShip/WarmlyShip/ShipsGenericCollection.cs b/WarmlyShip/WarmlyShip/ShipsGenericCollection.cs index 179eebd..e541701 100644 --- a/WarmlyShip/WarmlyShip/ShipsGenericCollection.cs +++ b/WarmlyShip/WarmlyShip/ShipsGenericCollection.cs @@ -14,12 +14,6 @@ namespace WarmlyShip.Generics where T : DrawingWarmlyShip where U : IMoveableObject { - /// - /// Сортировка - /// - /// - public void Sort(IComparer comparer) => - _collection.SortSet(comparer); /// /// Получение объектов коллекции /// @@ -45,6 +39,16 @@ namespace WarmlyShip.Generics /// private readonly SetGeneric _collection; /// + /// Получение объектов коллекции + /// + public IEnumerable GetAirplanes => _collection.GetShips(); + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => + _collection.SortSet(comparer); + /// /// Конструктор /// /// @@ -63,14 +67,14 @@ namespace WarmlyShip.Generics /// /// /// - public static bool operator +(ShipsGenericCollection collect, T? + public static int operator +(ShipsGenericCollection collect, T? obj) { if (obj == null) { - return false; + return -1; } - return (bool)collect?._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawingShipEqutables()); } /// /// Перегрузка оператора вычитания @@ -82,10 +86,7 @@ namespace WarmlyShip.Generics pos) { T? obj = collect._collection[pos]; - if (obj != null) - { - collect._collection.Remove(pos); - } + collect._collection.Remove(pos); return obj; } /// diff --git a/WarmlyShip/WarmlyShip/ShipsGenericStorage.cs b/WarmlyShip/WarmlyShip/ShipsGenericStorage.cs index f35034e..d9ffb4a 100644 --- a/WarmlyShip/WarmlyShip/ShipsGenericStorage.cs +++ b/WarmlyShip/WarmlyShip/ShipsGenericStorage.cs @@ -171,7 +171,7 @@ namespace WarmlyShip.Generics elem?.CreateDrawingShip(_separatorForObject, _pictureWidth, _pictureHeight); if (ship != null) { - if (!(collection + ship)) + if (collection + ship != -1) { throw new Exception("Ошибка добавления в коллекцию"); }