This commit is contained in:
rakhaliullov 2024-05-01 18:17:48 +03:00
parent a272e8dbd0
commit 4dfab08ee6
5 changed files with 58 additions and 33 deletions

View File

@ -1,4 +1,5 @@
using Stormtrooper.Drawnings; using Stormtrooper.Drawnings;
using Stormtrooper.Exceptions;
namespace Stormtrooper.CollectionGenericObjects; namespace Stormtrooper.CollectionGenericObjects;
@ -92,20 +93,25 @@ public abstract class AbstractCompany
{ {
Bitmap bitmap = new(_pictureWidth, _pictureHeight); Bitmap bitmap = new(_pictureWidth, _pictureHeight);
Graphics graphics = Graphics.FromImage(bitmap); Graphics graphics = Graphics.FromImage(bitmap);
DrawBackgound(graphics); DrawBackground(graphics);
SetObjectsPosition(); SetObjectsPosition();
for (int i = 0; i < (_collection?.Count ?? 0); ++i) for (int i = 0; i < (_collection?.Count ?? 0); ++i)
{
try
{ {
DrawningAircraft? obj = _collection?.Get(i); DrawningAircraft? obj = _collection?.Get(i);
obj?.DrawTransport(graphics); obj?.DrawTransport(graphics);
} }
catch (ObjectNotFoundException) { };
}
return bitmap; return bitmap;
} }
/// <summary> /// <summary>
/// Вывод заднего фона /// Вывод заднего фона
/// </summary> /// </summary>
/// <param name="g"></param> /// <param name="g"></param>
protected abstract void DrawBackgound(Graphics g); protected abstract void DrawBackground(Graphics g);
/// <summary> /// <summary>
/// Расстановка объектов /// Расстановка объектов

View File

@ -1,4 +1,5 @@
using Stormtrooper.Drawnings; using Stormtrooper.Drawnings;
using Stormtrooper.Exceptions;
namespace Stormtrooper.CollectionGenericObjects namespace Stormtrooper.CollectionGenericObjects
{ {
@ -17,7 +18,7 @@ namespace Stormtrooper.CollectionGenericObjects
ICollectionGenericObjects<DrawningAircraft> collection) : base(picWidth, picHeight, collection) ICollectionGenericObjects<DrawningAircraft> collection) : base(picWidth, picHeight, collection)
{ {
} }
protected override void DrawBackgound(Graphics g) protected override void DrawBackground(Graphics g)
{ {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
@ -41,12 +42,11 @@ namespace Stormtrooper.CollectionGenericObjects
for (int i = 0; i < (_collection?.Count ?? 0); i++) for (int i = 0; i < (_collection?.Count ?? 0); i++)
{ {
if (_collection.Get(i) != null) try {
{
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 10, curHeight * _placeSizeHeight + 10); _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 10, curHeight * _placeSizeHeight + 10);
} }
catch (ObjectNotFoundException) { }
if (curWidth > 0) if (curWidth > 0)
curWidth--; curWidth--;
else else
@ -63,3 +63,4 @@ namespace Stormtrooper.CollectionGenericObjects
} }
} }
} }

View File

@ -24,7 +24,21 @@ where T : class
private int _maxCount; private int _maxCount;
public int Count => _collection.Count; 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; public CollectionType GetCollectionType => CollectionType.List;
@ -41,6 +55,7 @@ where T : class
throw new PositionOutOfCollectionException(position); throw new PositionOutOfCollectionException(position);
return _collection[position]; return _collection[position];
} }
public int Insert(T obj) public int Insert(T obj)
{ {
if (_collection.Count + 1 <= _maxCount) if (_collection.Count + 1 <= _maxCount)
@ -50,6 +65,7 @@ where T : class
} }
throw new CollectionOverflowException(MaxCount); throw new CollectionOverflowException(MaxCount);
} }
public bool Insert(T obj, int position) public bool Insert(T obj, int position)
{ {
if (_collection.Count + 1 > MaxCount) if (_collection.Count + 1 > MaxCount)
@ -59,6 +75,7 @@ where T : class
_collection.Insert(position, obj); _collection.Insert(position, obj);
return true; return true;
} }
public T Remove(int position) public T Remove(int position)
{ {
if (position < 0 || position >= _collection.Count) if (position < 0 || position >= _collection.Count)

View File

@ -59,7 +59,11 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
// проверка позиции // проверка позиции
if (position >= _collection.Length || position < 0) if (position >= _collection.Length || position < 0)
{ {
return null; throw new PositionOutOfCollectionException(position);
}
if (_collection[position] == null)
{
throw new ObjectNotFoundException(position);
} }
return _collection[position]; return _collection[position];
} }
@ -76,18 +80,18 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
} }
index++; 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) if (position >= _collection.Length || position < 0)
{ return -1; } { throw new PositionOutOfCollectionException(position); }
if (_collection[position] == null) if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return position; return true;
} }
int index; int index;
@ -96,7 +100,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
if (_collection[index] == null) if (_collection[index] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return position; return true;
} }
} }
@ -105,18 +109,20 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
if (_collection[index] == null) if (_collection[index] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return position; return true;
} }
} }
return -1; throw new CollectionOverflowException(_collection.Length);
} }
public T Remove(int position) public T Remove(int position)
{ {
if (position >= _collection.Length || position < 0) if (position >= _collection.Length || position < 0)
{ return null; } { throw new PositionOutOfCollectionException(position); }
T DrawningAircraft = _collection[position]; if (_collection[position] == null)
throw new ObjectNotFoundException(position);
T temp = _collection[position];
_collection[position] = null; _collection[position] = null;
return DrawningAircraft; return temp;
} }
public IEnumerable<T?> GetItems() public IEnumerable<T?> GetItems()
{ {

View File

@ -154,32 +154,28 @@ where T : DrawningAircraft
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
throw new Exception("Файл не существует"); throw new FileNotFoundException("Файл не существует");
} }
using (StreamReader sr = new StreamReader(filename))// открываем файла на чтение
using (StreamReader sr = new StreamReader(filename))
{ {
string? str; string? str;
str = sr.ReadLine(); str = sr.ReadLine();
if (str != _collectionKey.ToString()) if (str != _collectionKey.ToString())
throw new Exception("Неверные данные"); throw new FormatException("В файле неверные данные");
_storages.Clear(); _storages.Clear();
while ((str = sr.ReadLine()) != null) while ((str = sr.ReadLine()) != null)
{ {
string[] record = str.Split(_separatorForKeyValue); string[] record = str.Split(_separatorForKeyValue);
if (record.Length != 4) if (record.Length != 4)
{ {
continue; continue;
} }
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType); ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null) if (collection == null)
{ {
throw new Exception("Не удалось определить тип коллекции"); throw new InvalidOperationException("Не удалось определить тип коллекции:" + record[1]);
} }
collection.MaxCount = Convert.ToInt32(record[2]); collection.MaxCount = Convert.ToInt32(record[2]);
@ -191,7 +187,7 @@ where T : DrawningAircraft
{ {
try try
{ {
if (collection.Insert(aircraft) == -1) if (collection.Insert(ship) == -1)
{ {
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]); throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
} }
@ -202,7 +198,6 @@ where T : DrawningAircraft
} }
} }
} }
_storages.Add(record[0], collection); _storages.Add(record[0], collection);
} }
} }