Лабораторная работа №5(new)

This commit is contained in:
MatveyPetrov 2024-02-16 18:41:40 +04:00
parent 40c8209de7
commit 477bdcf3d4
7 changed files with 176 additions and 155 deletions

View File

@ -12,30 +12,83 @@ public class AircraftSharingService : AbstractCompany
public AircraftSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningAircraft> collection) : base(picWidth, picHeight, collection) public AircraftSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningAircraft> collection) : base(picWidth, picHeight, collection)
{ {
} }
private int? _startPosX;
private int? _startPosY;
private int? ObjPositionX;
private int? ObjPositionY;
private void DrawPlace(Graphics g)
{
Pen pen = new(Color.Black);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, _placeSizeWidth, _placeSizeHeight);
}
private void DrawPosition(Graphics g)
{
Pen pen = new(Color.Black);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 2, 2);
}
protected override void DrawBackGround(Graphics g) protected override void DrawBackGround(Graphics g)
{ {
Pen pen = new(Color.Black, 3); _startPosX = 0;
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) _startPosY = 0;
for (int x = 0; x <= _pictureWidth; x = x + _placeSizeWidth)
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; j++) if ((_pictureWidth - _placeSizeWidth) > _startPosX)
{ {
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); for (int y = 0; y <= _pictureHeight; y = y + _placeSizeHeight)
{
if ((_pictureHeight - _placeSizeHeight) > _startPosY)
{
DrawPlace(g);
_startPosY = _startPosY + _placeSizeHeight;
}
}
_startPosX = _startPosX + _placeSizeWidth;
_startPosY = 0;
} }
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
} }
} }
protected override void SetObjectPosition(Graphics g) protected override void SetObjectPosition(Graphics g)
{ {
for(int i = 0; i < _collection?.Count; i++) _startPosX = 5;
_startPosY = 5;
int i = 0;
for (int x = 0; x <= _pictureWidth; x = x + _placeSizeWidth)
{
if ((_pictureWidth - _placeSizeWidth) > _startPosX)
{ {
DrawningAircraft airplane = _collection?.Get(i); ObjPositionX = _startPosX;
if (airplane != null)
{ for (int y = 0; y <= _pictureHeight; y = y + _placeSizeHeight)
int inRow = _pictureWidth / _placeSizeWidth; {
airplane.SetPosition(((inRow - 1 - (i % inRow)) * _placeSizeWidth), ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight)); if ((_pictureHeight - _placeSizeHeight) > _startPosY)
airplane.DrawTransport(g); {
ObjPositionY = _startPosY;
if (i < (_collection?.Count))
{
DrawningAircraft obj = _collection.Get(i);
if (obj != null)
{
obj.SetpictureSize(_pictureWidth, _pictureHeight);
obj.SetPosition(Convert.ToInt32(ObjPositionX), Convert.ToInt32(ObjPositionY));
}
i++;
}
_startPosY = _startPosY + _placeSizeHeight;
}
}
_startPosX = _startPosX + _placeSizeWidth;
_startPosY = 5;
} }
} }
} }

View File

@ -11,13 +11,13 @@ namespace ProectMilitaryAircraft.CollectionGenericObjects;
/// Параметризованный набор объектов /// Параметризованный набор объектов
/// </summary> /// </summary>
/// <typeparam name="T">Параметр : ограничение - ссылочный тип</typeparam> /// <typeparam name="T">Параметр : ограничение - ссылочный тип</typeparam>
public class ListgenericObjects<T> public class ListgenericObjects<T> : ICollectionGenericObjects<T>
where T : class where T : class
{ {
/// <summary> /// <summary>
/// Список объектов, которые храниим /// Список объектов, которые храним
/// </summary> /// </summary>
private readonly List<T>? _collection; private readonly List<T?> _collection;
/// <summary> /// <summary>
/// Максимально допустимое число объектов в списке /// Максимально допустимое число объектов в списке
@ -28,91 +28,51 @@ public class ListgenericObjects<T>
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
public CollectionType GetCollectionType => CollectionType.List;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public ListgenericObjects(int count) public ListgenericObjects()
{ {
_maxCount = count;
_collection = new(); _collection = new();
} }
public T? Get(int position)
/// <summary>
/// Получение объекта из набора позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{ {
get
{
if (position < 0 || position >= _maxCount)
{
return null;
}
return _collection?[position]; if (position < 0 || position >= Count) return null;
} return _collection[position];
set
{
if (!(position >= 0 && position < Count && _collection?.Count < _maxCount))
{
return;
}
_collection?.Insert(position, value);
return;
}
} }
public bool Insert(T obj) public bool Insert(T obj)
{ {
if (_collection?.Count == _maxCount) if (Count != _maxCount)
{ {
return false; _collection.Add(obj);
return true;
} }
return false;
Insert(obj, 0);
return true;
} }
public bool Insert(T obj, int position) public bool Insert(T obj, int position)
{ {
if (_collection?.Count == _maxCount) if (position > 0 && position <= _maxCount && Count != _maxCount)
{ {
return false; _collection.Insert(position, obj);
return true;
} }
return false;
Insert(obj, 0);
return true;
} }
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= Count) if (_collection[position] != null)
{ {
return false; _collection.RemoveAt(position);
} return true;
_collection?.RemoveAt(position);
return true;
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetTheAirplanes(int? maxTheAirplanes = null)
{
for (int i = 0; i < _collection?.Count; ++i)
{
yield return _collection?[i];
if (maxTheAirplanes.HasValue && i == maxTheAirplanes.Value)
{
yield break;
}
} }
return false;
} }
} }

View File

@ -16,71 +16,103 @@ namespace ProectMilitaryAircraft.CollectionGenericObjects
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Массив объектов, которые храним
/// </summary> /// </summary>
private T?[] _massive; private T?[] _collection;
public int Count => _massive.Length;
public int SetMaxCount { set { if (value > 0) { _massive = new T?[value]; } } } public int Count => _collection.Length;
public int SetMaxCount
{
set
{
if (value > 0)
{
if (_collection.Length > 0)
{
Array.Resize(ref _collection, value);
}
else
{
_collection = new T?[value];
}
}
}
}
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public MassiveGenericObjects() public MassiveGenericObjects()
{ {
_massive = Array.Empty<T>(); _collection = Array.Empty<T?>();
} }
public T? Get(int position) public T? Get(int position)
{ {
if (position < 0 || position >= Count) return null; if (_collection[position] != null)
return _massive[position]; {
return _collection[position];
}
else
{
return null;
}
} }
public bool Insert(T obj) public bool Insert(T obj)
{ {
int index = 0; for (int i = 0; i < _collection.Length; i++)
while (_massive[index] != null)
{ {
index++; if (_collection[i] == null)
if (index == Count) { return true; } // false? {
_collection[i] = obj;
return true;
}
} }
while (index != 0)
{
_massive[index] = _massive[index - 1];
index--;
}
_massive[0] = obj;
return false; return false;
} }
public bool Insert(T obj, int position) public bool Insert(T obj, int position)
{ {
if (position < 0 || position >= Count) if (_collection[position] == null)
{ {
return false; _collection[position] = obj;
}
if (_massive[position] == null)
{
_massive[position] = obj;
return true; return true;
} }
int index = position; if (_collection[position] != null)
while (_massive[index] != null) index++;
if (index == Count) return false;
for (int i = index; i > position; i--)
{ {
_massive[i] = _massive[i - 1]; for (int i = position; i < _collection.Length; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
break;
}
for (int i = position; i <= 0; i--)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
break;
}
} }
_massive[position] = obj; return false;
return true;
} }
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= Count) return false; if (_collection[position] != null)
_massive[position] = null; {
return true; _collection[position] = null;
return true;
}
return false;
} }
} }
} }

View File

@ -30,7 +30,7 @@ public class StorageCollection<T>
} }
if (collectionType == CollectionType.List) if (collectionType == CollectionType.List)
{ {
_storages.Add(name, new MassiveGenericObjects<T>()); _storages.Add(name, new ListgenericObjects<T>());
} }
} }

View File

@ -120,8 +120,10 @@ public class DrawningAircraft
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns> /// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetpictureSize(int width, int height) public bool SetpictureSize(int width, int height)
{ {
// TODO провека, что объект "влезает" в размеры поля if (width <= _drawningMilitaryAircraftWidth || height <= _drawingMilitaryAircraftHeight)
// если влезает, сохраняем границы и корректируем позицию объекта, если она была установлена {
return false;
}
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
return true; return true;
@ -138,8 +140,12 @@ public class DrawningAircraft
{ {
return; return;
} }
//TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах if (x > _pictureWidth || x < 0 || y > _pictureHeight || y < 0)
{
x = 0;
y = 0;
}
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
@ -169,24 +175,21 @@ public class DrawningAircraft
return true; return true;
//Вправо //Вправо
case DirectionType.Right: case DirectionType.Right:
#pragma warning disable CS8629 // Тип значения, допускающего NULL, может быть NULL.
if (_startPosX.Value + EntityAircraft.Step <= _pictureWidth.Value) if (_startPosX.Value + _drawningMilitaryAircraftWidth + EntityAircraft.Step < _pictureWidth)
{ {
if (_startPosX + 98 <= _pictureWidth) _startPosX += (int)EntityAircraft.Step;
_startPosX += (int)EntityAircraft.Step;
} }
#pragma warning restore CS8629 // Тип значения, допускающего NULL, может быть NULL.
return true; return true;
//Влево //Влево
case DirectionType.Down: case DirectionType.Down:
#pragma warning disable CS8629 // Тип значения, допускающего NULL, может быть NULL.
if (_startPosY.Value + EntityAircraft.Step <= _pictureHeight.Value) if (_startPosY.Value + _drawingMilitaryAircraftHeight + EntityAircraft.Step < _pictureHeight)
{ {
if (_startPosY + 90 <= _pictureHeight) _startPosY += (int)EntityAircraft.Step;
_startPosY += (int)EntityAircraft.Step;
} }
#pragma warning restore CS8629 // Тип значения, допускающего NULL, может быть NULL.
return true; return true;
default: default:
return false; return false;

View File

@ -44,33 +44,6 @@ public class DrawningMilitaryAircraft : DrawningAircraft
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush abr = new SolidBrush(airCraft.AdditionalColor); Brush abr = new SolidBrush(airCraft.AdditionalColor);
Brush br = new SolidBrush(airCraft.BodyColor);
//крыло
g.FillRectangle(br, _startPosX.Value + 40, _startPosY.Value, 10, 80);
g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value, 10, 80);
//хвост
g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 27, 10, 5);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 27, 10, 5);
g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 47, 10, 5);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 47, 10, 5);
//Границы Самолета
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 30, 50, 20);
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 30, 50, 20);
//Хвост (центр)
g.FillRectangle(br, _startPosX.Value + 2, _startPosY.Value + 37, 10, 5);
g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 37, 10, 5);
//Кабина
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 40, _startPosX.Value + 60, _startPosY.Value + 25);
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 25, _startPosX.Value + 80, _startPosY.Value + 40);
g.DrawLine(pen, _startPosX.Value + 80, _startPosY.Value + 40, _startPosX.Value + 60, _startPosY.Value + 55);
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 50, _startPosX.Value + 60, _startPosY.Value + 55);
base.DrawTransport(g); base.DrawTransport(g);
//Ракеты //Ракеты

View File

@ -76,14 +76,13 @@ public partial class FormAircraftCollection : Form
if (_company + aircraft) if (_company + aircraft)
{ {
MessageBox.Show("Объект добавлен");
MessageBox.Show("не удалось добавить объект");
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
} }
else else
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("не удалось добавить объект");
pictureBox.Image = _company.Show();
} }
} }
@ -236,7 +235,7 @@ public partial class FormAircraftCollection : Form
return; return;
} }
ICollectionGenericObjects<DrawningAircraft>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString()?? string.Empty]; ICollectionGenericObjects<DrawningAircraft>? collection = _storageCollection[listBoxCollection.SelectedItem?.ToString()?? string.Empty];
if (collection == null) if (collection == null)
{ {
MessageBox.Show("Коллкция не проиницилизирована"); MessageBox.Show("Коллкция не проиницилизирована");
@ -246,11 +245,12 @@ public partial class FormAircraftCollection : Form
switch (comboBoxSelectorCompany.Text) switch (comboBoxSelectorCompany.Text)
{ {
case "Хранилище": case "Хранилище":
_company = new AircraftSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningAircraft>()); _company = new AircraftSharingService(pictureBox.Width, pictureBox.Height, collection);
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
break; break;
} }
panelCompanyTools.Enabled = true; panelCompanyTools.Enabled = true;
RefreshListBoxItems();
} }
} }