From 4dfab08ee68331f1e22de08789ac07b13fc82e67 Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Wed, 1 May 2024 18:17:48 +0300 Subject: [PATCH] 4 --- .../AbstractCompany.cs | 14 +++++++--- .../AircraftHangarService.cs | 13 +++++---- .../ListGenericObjects.cs | 19 ++++++++++++- .../MassiveGenericObjects.cs | 28 +++++++++++-------- .../StorageCollection.cs | 17 ++++------- 5 files changed, 58 insertions(+), 33 deletions(-) diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/AbstractCompany.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/AbstractCompany.cs index 7fa344c..5fddf33 100644 --- a/Stormtrooper/Stormtrooper/CollectionGenericObjects/AbstractCompany.cs +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/AbstractCompany.cs @@ -1,4 +1,5 @@ using Stormtrooper.Drawnings; +using Stormtrooper.Exceptions; namespace Stormtrooper.CollectionGenericObjects; @@ -92,12 +93,17 @@ public abstract class AbstractCompany { Bitmap bitmap = new(_pictureWidth, _pictureHeight); Graphics graphics = Graphics.FromImage(bitmap); - DrawBackgound(graphics); + DrawBackground(graphics); + SetObjectsPosition(); for (int i = 0; i < (_collection?.Count ?? 0); ++i) { - DrawningAircraft? obj = _collection?.Get(i); - obj?.DrawTransport(graphics); + try + { + DrawningAircraft? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + catch (ObjectNotFoundException) { }; } return bitmap; } @@ -105,7 +111,7 @@ public abstract class AbstractCompany /// Вывод заднего фона /// /// - protected abstract void DrawBackgound(Graphics g); + protected abstract void DrawBackground(Graphics g); /// /// Расстановка объектов diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/AircraftHangarService.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/AircraftHangarService.cs index 63282a7..c67dbdc 100644 --- a/Stormtrooper/Stormtrooper/CollectionGenericObjects/AircraftHangarService.cs +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/AircraftHangarService.cs @@ -1,4 +1,5 @@ using Stormtrooper.Drawnings; +using Stormtrooper.Exceptions; namespace Stormtrooper.CollectionGenericObjects { @@ -17,7 +18,7 @@ namespace Stormtrooper.CollectionGenericObjects ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) { } - protected override void DrawBackgound(Graphics g) + protected override void DrawBackground(Graphics g) { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; @@ -41,18 +42,17 @@ namespace Stormtrooper.CollectionGenericObjects for (int i = 0; i < (_collection?.Count ?? 0); i++) { - if (_collection.Get(i) != null) - { + try { _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 10, curHeight * _placeSizeHeight + 10); } - + catch (ObjectNotFoundException) { } if (curWidth > 0) curWidth--; else { - curWidth = width - 1; - curHeight ++; + curWidth = width - 1; + curHeight++; } if (curHeight >= height) @@ -63,3 +63,4 @@ namespace Stormtrooper.CollectionGenericObjects } } } + diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs index 6d8108a..a920646 100644 --- a/Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs @@ -24,7 +24,21 @@ where T : class private int _maxCount; public int Count => _collection.Count; - public int MaxCount { set { if (value > 0) { _maxCount = value; } } get { return Count; } } + public int MaxCount + { + set + { + if (value > 0) + { + _maxCount = value; + } + } + get + { + + return Count; + } + } public CollectionType GetCollectionType => CollectionType.List; @@ -41,6 +55,7 @@ where T : class throw new PositionOutOfCollectionException(position); return _collection[position]; } + public int Insert(T obj) { if (_collection.Count + 1 <= _maxCount) @@ -50,6 +65,7 @@ where T : class } throw new CollectionOverflowException(MaxCount); } + public bool Insert(T obj, int position) { if (_collection.Count + 1 > MaxCount) @@ -59,6 +75,7 @@ where T : class _collection.Insert(position, obj); return true; } + public T Remove(int position) { if (position < 0 || position >= _collection.Count) diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs index bfd8836..d545787 100644 --- a/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs @@ -59,7 +59,11 @@ public class MassiveGenericObjects : ICollectionGenericObjects // проверка позиции if (position >= _collection.Length || position < 0) { - return null; + throw new PositionOutOfCollectionException(position); + } + if (_collection[position] == null) + { + throw new ObjectNotFoundException(position); } return _collection[position]; } @@ -76,18 +80,18 @@ public class MassiveGenericObjects : ICollectionGenericObjects } index++; } - return -1; + throw new CollectionOverflowException(_collection.Length); } - public int Insert(T obj, int position) + public bool Insert(T obj, int position) { if (position >= _collection.Length || position < 0) - { return -1; } + { throw new PositionOutOfCollectionException(position); } if (_collection[position] == null) { _collection[position] = obj; - return position; + return true; } int index; @@ -96,7 +100,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[position] = obj; - return position; + return true; } } @@ -105,18 +109,20 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[position] = obj; - return position; + return true; } } - return -1; + throw new CollectionOverflowException(_collection.Length); } public T Remove(int position) { if (position >= _collection.Length || position < 0) - { return null; } - T DrawningAircraft = _collection[position]; + { throw new PositionOutOfCollectionException(position); } + if (_collection[position] == null) + throw new ObjectNotFoundException(position); + T temp = _collection[position]; _collection[position] = null; - return DrawningAircraft; + return temp; } public IEnumerable GetItems() { diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs index 797dbf2..d9af62f 100644 --- a/Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs @@ -154,32 +154,28 @@ where T : DrawningAircraft { if (!File.Exists(filename)) { - throw new Exception("Файл не существует"); + throw new FileNotFoundException("Файл не существует"); } - using (StreamReader sr = new StreamReader(filename))// открываем файла на чтение + + using (StreamReader sr = new StreamReader(filename)) { string? str; str = sr.ReadLine(); if (str != _collectionKey.ToString()) - throw new Exception("Неверные данные"); - + throw new FormatException("В файле неверные данные"); _storages.Clear(); - while ((str = sr.ReadLine()) != null) { string[] record = str.Split(_separatorForKeyValue); - - if (record.Length != 4) { continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); if (collection == null) { - throw new Exception("Не удалось определить тип коллекции"); + throw new InvalidOperationException("Не удалось определить тип коллекции:" + record[1]); } collection.MaxCount = Convert.ToInt32(record[2]); @@ -191,7 +187,7 @@ where T : DrawningAircraft { try { - if (collection.Insert(aircraft) == -1) + if (collection.Insert(ship) == -1) { throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]); } @@ -202,7 +198,6 @@ where T : DrawningAircraft } } } - _storages.Add(record[0], collection); } }