From 343844e578e8d7cbc91b9e8fdb8cfa3047f32401 Mon Sep 17 00:00:00 2001 From: cyxaruk Date: Sat, 23 Mar 2024 17:23:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=BE=D0=B2=D0=B8=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BB=D0=B0=D0=B1=D1=8B=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 123 ++++++++++++++++++ .../MachineSharingService.cs | 25 ++++ .../MassiveGenericObjects.cs | 97 ++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs create mode 100644 ProjectCar/ProjectCar/CollectionGenericObjects/MachineSharingService.cs create mode 100644 ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..80e84f9 --- /dev/null +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,123 @@ +using ProjectGasMachine.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasMachine.CollectionGenericObjects; + +/// +/// Абстракция компании, хранящий коллекцию автомобилей +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 280; + + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 80; + + /// + /// Ширина окна + /// + 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, DrawningMachine machine) + { + return company._collection.Insert(machine); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningMachine? operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position); + } + + + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningMachine? GetRandomObject() + { + Random rnd = new(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + + /// + /// Вывод всей коллекции + /// + /// + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackground(graphics); + + SetObjectsPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningMachine? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackground(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); +} diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/MachineSharingService.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/MachineSharingService.cs new file mode 100644 index 0000000..a842369 --- /dev/null +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/MachineSharingService.cs @@ -0,0 +1,25 @@ +using ProjectGasMachine.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasMachine.CollectionGenericObjects; + +public class MachineSharingService : AbstractCompany +{ + public MachineSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackground(Graphics g) + { + throw new NotImplementedException(); + } + + protected override void SetObjectsPosition() + { + throw new NotImplementedException(); + } +} diff --git a/ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..78396da --- /dev/null +++ b/ProjectCar/ProjectCar/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,97 @@ +namespace ProjectGasMachine.CollectionGenericObjects; + +/// +/// Параметризованный набор объектов +/// +/// Параметр: ограничение - ссылочный тип +public class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + /// + /// массив объектов, которые храним + /// + private T?[] _collection; + + + public int Count => _collection.Length; + + public int SetMaxCount { set { if (value > 0) { _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) + { + // TODO вставка в свободное место набора + 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; + } + + for (int i = position + 1; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return position; + } + } + + for (int i = position - 1; i >= 0; i--) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return position; + } + } + + return -1; + } + + + public T? Remove(int position) + { + if (position < 0 || position > Count || _collection[position] == null) + { + return null; + } + + T? obj = _collection[position]; + _collection[position] = null; + return obj; + } +}