From 0a45e204e6a22bb21520196eb861a96de0ed34a1 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sat, 23 Mar 2024 18:09:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=B0=D0=BF=D0=BA=D0=B0=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D0=BA=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 117 ++++++++++++++++++ .../ICollectionGenericObjects.cs | 51 ++++++++ .../MassiveGenericObjects.cs | 114 +++++++++++++++++ .../PlaneSharingService.cs | 57 +++++++++ 4 files changed, 339 insertions(+) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/PlaneSharingService.cs diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..6019f3d --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,117 @@ +using ProjectAirplaneWithRadar.Drawnings; + +namespace ProjectAirplaneWithRadar.CollectionGenericObjects +{ + /// + /// Абстракция компании, хранящий коллекцию автомобилей + /// + public abstract class AbstractCompany + { + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 210; + + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 110; + + /// + /// Ширина окна + /// + protected readonly int _pictureWidth; + + /// + /// Высота окна + /// + protected readonly int _pictureHeight; + + /// + /// Коллекция автомобилей + /// + protected ICollectionGenericObjects? _collection = null; + + /// + /// Вычисление максимального количества элементов, который можно разместить в окне + /// + private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + + /// + /// Конструктор + /// + /// Ширина окна + /// Высота окна + /// Коллекция автомобилей + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static int operator +(AbstractCompany company, DrawningAirplane airplane) + { + return company._collection.Insert(airplane); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningAirplane operator -(AbstractCompany company, int position) + { + return company._collection.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningAirplane? GetRandomObject() + { + Random rnd = new(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + + /// + /// Вывод всей коллекции + /// + /// + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackgound(graphics); + + SetObjectsPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningAirplane? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..d2f6a7f --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,51 @@ +using ProjectAirplaneWithRadar.Drawnings; + +namespace ProjectAirplaneWithRadar.CollectionGenericObjects +{ + /// + /// Интерфейс описания действий для набора хранимых объектов + /// + /// Параметр: ограничение - ссылочный тип + public interface ICollectionGenericObjects + where T : class + { + /// + /// Количество объектов в коллекции + /// + int Count { get; } + + /// + /// Установка максимального количества элементов + /// + int SetMaxCount { set; } + + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj); + + /// + /// Добавление объекта в коллекцию на конкретную позицию + /// + /// Добавляемый объект + /// Позиция + /// 1 - вставка прошла удачно, -1 - вставка не удалась + int Insert(T obj, int position); + + /// + /// Удаление объекта из коллекции с конкретной позиции + /// + /// Позиция + /// + T? Remove(int position); + + /// + /// Получение объекта по позиции + /// + /// Позиция + /// Объект + T? Get(int position); + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..f15d0de --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,114 @@ +namespace ProjectAirplaneWithRadar.CollectionGenericObjects +{ + /// + /// Параметризованный набор объектов + /// + /// Параметр: ограничение - ссылочный тип + public class MassiveGenericObjects : ICollectionGenericObjects + where T : class + { + /// + /// Массив объектов, которые храним + /// + private T?[] _collection; + + 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]; + } + } + } + } + + /// + /// Конструктор + /// + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) + { + if (position < 0 || position >= Count) + return null; + return _collection[position]; + } + + public int Insert(T obj) + { + for (int i = 0; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + return -1; + } + + public int Insert(T obj, int position) + { + if (position < 0 || position >= Count) + return -1; + + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + + int temp = position + 1; + while (temp < Count) + { + if (_collection[temp] == null) + { + _collection[temp] = obj; + return temp; + } + temp++; + } + + temp = position - 1; + while (temp > 0) + { + if (_collection[temp] == null) + { + _collection[temp] = obj; + return temp; + } + temp--; + } + + return -1; + } + + public T? Remove(int position) + { + if (position < 0 || position >= Count) + return null; + + if (_collection[position] == null) + { + return null; + } + + T? temp = _collection[position]; + _collection[position] = null; + return temp; + } + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/PlaneSharingService.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/PlaneSharingService.cs new file mode 100644 index 0000000..86437bd --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/PlaneSharingService.cs @@ -0,0 +1,57 @@ +using ProjectAirplaneWithRadar.Drawnings; + +namespace ProjectAirplaneWithRadar.CollectionGenericObjects +{ + + public class PlaneSharingService : AbstractCompany + { + public PlaneSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + Pen pen = new(Color.Black, 4); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, + i * _placeSizeWidth + _placeSizeWidth - 40, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + protected override void SetObjectsPosition() + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = width - 1; + int curHeight = 0; + + for (int i = 0; i < (_collection?.Count ?? 0); i++) + { + if (_collection.Get(i) != null) + { + _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 10, curHeight * _placeSizeHeight + 5); + } + + if (curWidth > 0) + curWidth--; + else + { + curWidth = width - 1; + curHeight++; + } + if (curHeight > height) + { + return; + } + } + } + } + +} \ No newline at end of file