diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.cs index 2ae42d4..5107a74 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -127,10 +127,17 @@ namespace Bulldozer MessageBox.Show("Объект добавлен"); _logger.LogInformation("Объект добавлен"); pictureBoxCollection.Image = obj.ShowBulldozers(); - } catch(Exception ex) { + } + catch (StorageOverflowException ex) + { MessageBox.Show(ex.Message); _logger.LogWarning($"Объект не добавлен в набор {listBoxBulldozerStorages.SelectedItem.ToString()}"); } + catch (ArgumentException ex) + { + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxBulldozerStorages.SelectedItem.ToString()}"); + } } diff --git a/Bulldozer/Bulldozer/Generics/BulldozerCollectionInfo.cs b/Bulldozer/Bulldozer/Generics/BulldozerCollectionInfo.cs new file mode 100644 index 0000000..c26bddd --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/BulldozerCollectionInfo.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class BulldozerCollectionInfo: IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BulldozerCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BulldozerCollectionInfo other) + { + if (Name != other?.Name) + return false; + return true; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + public override string ToString() + { + return Name; + } + } +} diff --git a/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs index ceeb7bb..0d2e80d 100644 --- a/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs +++ b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs @@ -60,7 +60,7 @@ namespace Bulldozer.Generics { if (obj == null) return false; - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj, new DrawingBulldozerEqutables()) ?? false; } /// diff --git a/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs b/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs index 36f2155..d056c26 100644 --- a/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs +++ b/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs @@ -3,6 +3,7 @@ using Bulldozer.MovementStrategy; using System.Text; using Bulldozer.Exceptions; using System.Numerics; +using Microsoft.AspNetCore.Http; namespace Bulldozer.Generics { @@ -14,11 +15,11 @@ namespace Bulldozer.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _bulldozerStorages; + readonly Dictionary> _bulldozerStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _bulldozerStorages.Keys.ToList(); + public List Keys => _bulldozerStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -34,7 +35,7 @@ namespace Bulldozer.Generics /// public BulldozersGenericStorage(int pictureWidth, int pictureHeight) { - _bulldozerStorages = new Dictionary>(); + _bulldozerStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -44,9 +45,9 @@ namespace Bulldozer.Generics /// Название набора public void AddSet(string name) { - if (!_bulldozerStorages.ContainsKey(name)) + if (!_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(name, string.Empty))) { - _bulldozerStorages.Add(name, new BulldozersGenericCollection(_pictureWidth, _pictureHeight)); + _bulldozerStorages.Add(new BulldozerCollectionInfo(name, string.Empty), new BulldozersGenericCollection(_pictureWidth, _pictureHeight)); } } @@ -58,9 +59,9 @@ namespace Bulldozer.Generics public void DelSet(string name) { // TODO: Прописать логику для удаления набора - if (_bulldozerStorages.ContainsKey(name)) + if (_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(name, string.Empty))) { - _bulldozerStorages.Remove(name); + _bulldozerStorages.Remove(new BulldozerCollectionInfo(name, string.Empty)); } } @@ -73,14 +74,11 @@ namespace Bulldozer.Generics { get { - if (_bulldozerStorages.ContainsKey(ind)) + if (_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(ind, string.Empty))) { - return _bulldozerStorages[ind]; - } - else - { - return null; + return _bulldozerStorages[new BulldozerCollectionInfo(ind, string.Empty)]; } + return null; } } /// Разделитель для записи ключа и значения элемента словаря @@ -107,7 +105,7 @@ namespace Bulldozer.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _bulldozerStorages) + foreach (KeyValuePair> record in _bulldozerStorages) { StringBuilder records = new(); foreach (DrawingBulldozer? elem in record.Value.GetBulldozers) @@ -189,7 +187,7 @@ namespace Bulldozer.Generics } } } - _bulldozerStorages.Add(record[0], collection); + _bulldozerStorages.Add(new BulldozerCollectionInfo(record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); diff --git a/Bulldozer/Bulldozer/Generics/SetGeneric.cs b/Bulldozer/Bulldozer/Generics/SetGeneric.cs index b6fc48a..c0ea1ba 100644 --- a/Bulldozer/Bulldozer/Generics/SetGeneric.cs +++ b/Bulldozer/Bulldozer/Generics/SetGeneric.cs @@ -45,7 +45,10 @@ namespace Bulldozer.Generics /// public bool Insert(T bulldozer, IEqualityComparer? equal = null) { - return Insert(bulldozer, 0, equal); + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + Insert(bulldozer, 0, equal); + return true; } /// @@ -55,10 +58,14 @@ namespace Bulldozer.Generics /// public bool Insert(T bulldozer , int position, IEqualityComparer? equal = null) { - if (position < 0 || position >= _maxCount) { - throw new StorageOverflowException("Вставка невозможна."); + if (_places.Count >= _maxCount) + throw new StorageOverflowException(_maxCount); + + if (position < 0 || position >= _maxCount) + { + return false; } - if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + if (equal != null) { if (_places.Contains(bulldozer, equal))