From b545af6f929eb5aaaf2b08a886850ee6f376f115 Mon Sep 17 00:00:00 2001 From: LESN1K Date: Sun, 10 Mar 2024 15:33:04 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9A=D0=BE=D0=BB=D0=BB=D0=B5=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ICollectionGenericObjects.cs | 50 ++++++++ .../MassiveGenericObjects.cs | 107 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..4509b8d --- /dev/null +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,50 @@ +namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects +{ + /// + /// Интерфейс описания действий для набора хранимых объектов + /// + /// Параметр: ограничение - ссылочный тип + public interface ICollectionGenericObjects + where T : class + { + /// + /// Количество объектов в коллекции + /// + int Count { get; } + + /// + /// Установка максимального количества элементов + /// + int SetMaxCount { set; } + + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + bool Insert(T obj); + + /// + /// Добавление объекта в коллекцию на конкретную позицию + /// + /// Добавляемый объект + /// Позиция + /// true - вставка прошла удачно, false - вставка не удалась + bool Insert(T obj, int position); + + /// + /// Удаление объекта из коллекции с конкретной позиции + /// + /// Позиция + /// true - удаление прошло удачно, false - удаление не удалось + bool Remove(int position); + + /// + /// Получение объекта по позиции + /// + /// Позиция + /// Объект + T? Get(int position); + + } +} diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..af8ad8d --- /dev/null +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,107 @@ +namespace ProjectMonorail.Scripts.Monorail.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) + { + // TODO проверка позиции + return _collection[position]; + } + + public bool Insert(T obj) + { + // TODO вставка в свободное место набора + for (int i = 0; i < Count; i++) + { + if (InsertingElementCollection(i, obj)) return true; + } + + return false; + } + + public bool Insert(T obj, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // ищется свободное место после этой позиции и идет вставка туда + // если нет после, ищем до + // TODO вставка + + if (InsertingElementCollection(position, obj)) return true; + + for (int i = position + 1; i < Count; i++) + { + if (InsertingElementCollection(i, obj)) return true; + } + + for (int i = position - 1; i >= 0; i--) + { + if (InsertingElementCollection(i, obj)) return true; + } + + return false; + } + + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присвоив элементу массива значение null + + if (_collection[position] == null) return false; + + _collection[position] = null; + + return true; + } + + /// + /// Если элемент массива пустой, то происходит вставка нового элемента + /// + /// Индекс элемента + /// Элемент + /// false - элемент массива не равен null, true - равен null + private bool InsertingElementCollection(int index, T obj) + { + if (_collection[index] != null) return false; + + _collection[index] = obj; + return true; + } + + } +} \ No newline at end of file -- 2.25.1 From 27d06f5bbc231676eac38617eaa84208a8e95c96 Mon Sep 17 00:00:00 2001 From: ENDORFIT Date: Sun, 10 Mar 2024 19:05:31 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BF=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectMonorail/FormMonorail.Designer.cs | 28 --- ProjectMonorail/FormMonorail.cs | 66 ++----- .../FormMonorailCollection.Designer.cs | 173 ++++++++++++++++++ ProjectMonorail/FormMonorailCollection.cs | 148 +++++++++++++++ ProjectMonorail/FormMonorailCollection.resx | 120 ++++++++++++ ProjectMonorail/Program.cs | 2 +- .../AbstractCompany.cs | 115 ++++++++++++ .../MassiveGenericObjects.cs | 8 +- .../MonorailSharingService.cs | 57 ++++++ 9 files changed, 634 insertions(+), 83 deletions(-) create mode 100644 ProjectMonorail/FormMonorailCollection.Designer.cs create mode 100644 ProjectMonorail/FormMonorailCollection.cs create mode 100644 ProjectMonorail/FormMonorailCollection.resx create mode 100644 ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs create mode 100644 ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MonorailSharingService.cs diff --git a/ProjectMonorail/FormMonorail.Designer.cs b/ProjectMonorail/FormMonorail.Designer.cs index 2f42482..c501670 100644 --- a/ProjectMonorail/FormMonorail.Designer.cs +++ b/ProjectMonorail/FormMonorail.Designer.cs @@ -33,8 +33,6 @@ buttonMove_Left = new Button(); buttonMove_Up = new Button(); pictureBoxMonorail = new PictureBox(); - buttonCreateModernMonorail = new Button(); - buttonCreateMonorail = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxMonorail).BeginInit(); @@ -98,28 +96,6 @@ pictureBoxMonorail.TabIndex = 4; pictureBoxMonorail.TabStop = false; // - // buttonCreateModernMonorail - // - buttonCreateModernMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateModernMonorail.Location = new Point(12, 439); - buttonCreateModernMonorail.Name = "buttonCreateModernMonorail"; - buttonCreateModernMonorail.Size = new Size(220, 23); - buttonCreateModernMonorail.TabIndex = 5; - buttonCreateModernMonorail.Text = "Создать современный монорельс"; - buttonCreateModernMonorail.UseVisualStyleBackColor = true; - buttonCreateModernMonorail.Click += ButtonCreateModernMonorail_Click; - // - // buttonCreateMonorail - // - buttonCreateMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateMonorail.Location = new Point(238, 439); - buttonCreateMonorail.Name = "buttonCreateMonorail"; - buttonCreateMonorail.Size = new Size(194, 23); - buttonCreateMonorail.TabIndex = 6; - buttonCreateMonorail.Text = "Создать монорельс"; - buttonCreateMonorail.UseVisualStyleBackColor = true; - buttonCreateMonorail.Click += buttonCreateMonorail_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -147,8 +123,6 @@ ClientSize = new Size(813, 474); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateMonorail); - Controls.Add(buttonCreateModernMonorail); Controls.Add(buttonMove_Up); Controls.Add(buttonMove_Left); Controls.Add(buttonMove_Down); @@ -167,8 +141,6 @@ private Button buttonMove_Left; private Button buttonMove_Up; private PictureBox pictureBoxMonorail; - private Button buttonCreateModernMonorail; - private Button buttonCreateMonorail; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/ProjectMonorail/FormMonorail.cs b/ProjectMonorail/FormMonorail.cs index 63dc9c2..a7a4184 100644 --- a/ProjectMonorail/FormMonorail.cs +++ b/ProjectMonorail/FormMonorail.cs @@ -18,6 +18,20 @@ namespace ProjectMonorail /// private AbstractStrategy? _strategy; + public DrawingMonorail SetMonorail + { + set + { + _drawningMonorail = value; + _drawningMonorail.SetPictureSize(pictureBoxMonorail.Width, pictureBoxMonorail.Height); + Random random = new Random(); + _drawningMonorail.SetPosition(random.Next(0, 100), random.Next(0, 100)); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + /// /// Конструктор формы /// @@ -68,58 +82,6 @@ namespace ProjectMonorail if (result) Draw(); } - /// - /// Создание объекта класса-перемещения - /// - /// Тип создоваемого объекта - private void CreateObject(string type) - { - Random random = new(); - - switch (type) - { - case nameof(DrawingMonorail): - _drawningMonorail = new DrawingMonorail(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); - break; - case nameof(DrawingModernMonorail): - bool randomTrack = Convert.ToBoolean(random.Next(0, 2)); - _drawningMonorail = new DrawingModernMonorail(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - randomTrack, - (randomTrack ? Convert.ToBoolean(random.Next(0, 2)) : true)); - break; - default: - return; - } - - _drawningMonorail.SetPictureSize(pictureBoxMonorail.Width, pictureBoxMonorail.Height); - _drawningMonorail.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - - Draw(); - } - - /// - /// Обработка нажатия кнопки "Создать современный монорельс" - /// - private void ButtonCreateModernMonorail_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawingModernMonorail)); - } - - /// - /// Обработка нажатия кнопки "Создать монорельс" - /// - /// - /// - private void buttonCreateMonorail_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawingMonorail)); - } - /// /// /// diff --git a/ProjectMonorail/FormMonorailCollection.Designer.cs b/ProjectMonorail/FormMonorailCollection.Designer.cs new file mode 100644 index 0000000..acbc189 --- /dev/null +++ b/ProjectMonorail/FormMonorailCollection.Designer.cs @@ -0,0 +1,173 @@ +namespace ProjectMonorail +{ + partial class FormMonorailCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + groupBoxTools = new GroupBox(); + buttonRefresh = new Button(); + buttonGoToCheck = new Button(); + buttonRemoveMonorail = new Button(); + maskedTextBox = new MaskedTextBox(); + buttonAddModernMonorail = new Button(); + buttonAddMonorail = new Button(); + comboBoxSelectorCompany = new ComboBox(); + pictureBox = new PictureBox(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonRefresh); + groupBoxTools.Controls.Add(buttonGoToCheck); + groupBoxTools.Controls.Add(buttonRemoveMonorail); + groupBoxTools.Controls.Add(maskedTextBox); + groupBoxTools.Controls.Add(buttonAddModernMonorail); + groupBoxTools.Controls.Add(buttonAddMonorail); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(970, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(241, 662); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 581); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(229, 48); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(6, 527); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(229, 48); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveMonorail + // + buttonRemoveMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveMonorail.Location = new Point(6, 350); + buttonRemoveMonorail.Name = "buttonRemoveMonorail"; + buttonRemoveMonorail.Size = new Size(229, 48); + buttonRemoveMonorail.TabIndex = 4; + buttonRemoveMonorail.Text = "Удалить монорельс"; + buttonRemoveMonorail.UseVisualStyleBackColor = true; + buttonRemoveMonorail.Click += buttonRemoveMonorail_Click; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(6, 321); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(223, 23); + maskedTextBox.TabIndex = 3; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonAddModernMonorail + // + buttonAddModernMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddModernMonorail.Location = new Point(6, 161); + buttonAddModernMonorail.Name = "buttonAddModernMonorail"; + buttonAddModernMonorail.Size = new Size(229, 48); + buttonAddModernMonorail.TabIndex = 2; + buttonAddModernMonorail.Text = "Добавление современного монорельса"; + buttonAddModernMonorail.UseVisualStyleBackColor = true; + buttonAddModernMonorail.Click += ButtonAddModernMonorail_Click; + // + // buttonAddMonorail + // + buttonAddMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddMonorail.Location = new Point(6, 107); + buttonAddMonorail.Name = "buttonAddMonorail"; + buttonAddMonorail.Size = new Size(229, 48); + buttonAddMonorail.TabIndex = 1; + buttonAddMonorail.Text = "Добавление монорельса"; + buttonAddMonorail.UseVisualStyleBackColor = true; + buttonAddMonorail.Click += ButtonAddMonorail_Click; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(6, 46); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(229, 23); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(970, 662); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormMonorailCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1211, 662); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormMonorailCollection"; + Text = "Коллекция монорельсов"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonRemoveMonorail; + private MaskedTextBox maskedTextBox; + private Button buttonAddModernMonorail; + private Button buttonAddMonorail; + private PictureBox pictureBox; + private Button buttonGoToCheck; + private Button buttonRefresh; + } +} \ No newline at end of file diff --git a/ProjectMonorail/FormMonorailCollection.cs b/ProjectMonorail/FormMonorailCollection.cs new file mode 100644 index 0000000..b94fae4 --- /dev/null +++ b/ProjectMonorail/FormMonorailCollection.cs @@ -0,0 +1,148 @@ +using ProjectMonorail.Scripts.Monorail.CollectionGenericObjects; +using ProjectMonorail.Scripts.Monorail.Drawnings; + +namespace ProjectMonorail +{ + /// + /// + /// + public partial class FormMonorailCollection : Form + { + /// + /// + /// + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormMonorailCollection() + { + InitializeComponent(); + } + + /// + /// + /// + /// + /// + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new MonorailSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + /// + /// Создание объекта класса-перемещения + /// + /// Тип создоваемого объекта + private void CreateObject(string type) + { + if (_company == null) return; + + Random random = new(); + DrawingMonorail drawningMonorail; + switch (type) + { + case nameof(DrawingMonorail): + drawningMonorail = new DrawingMonorail(random.Next(100, 300), random.Next(1000, 3000), + GetColor(random)); + break; + case nameof(DrawingModernMonorail): + bool randomTrack = Convert.ToBoolean(random.Next(0, 2)); + drawningMonorail = new DrawingModernMonorail(random.Next(100, 300), random.Next(1000, 3000), GetColor(random), GetColor(random), randomTrack, (randomTrack ? Convert.ToBoolean(random.Next(0, 2)) : true)); + break; + default: + return; + } + + if (_company + drawningMonorail) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + + /// + /// Получение цвета + /// + /// Генератор случайных чесел + /// + private static Color GetColor(Random random) + { + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new ColorDialog(); + + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + + return color; + } + + private void ButtonAddMonorail_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingMonorail)); + } + + private void ButtonAddModernMonorail_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingModernMonorail)); + } + + private void buttonRemoveMonorail_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) return; + + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; + + int position = Convert.ToInt32(maskedTextBox.Text); + + if (_company - position) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) return; + + DrawingMonorail? monorail = null; + int coutner = 100; + + while (monorail == null && coutner-- > 0) + { + monorail = _company.GetRandomObject(); + } + + if (monorail == null) return; + + FormMonorail form = new FormMonorail() + { + SetMonorail = monorail + }; + form.ShowDialog(); + } + + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) return; + pictureBox.Image = _company.Show(); + } + } +} diff --git a/ProjectMonorail/FormMonorailCollection.resx b/ProjectMonorail/FormMonorailCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectMonorail/FormMonorailCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProjectMonorail/Program.cs b/ProjectMonorail/Program.cs index 3f68a06..beba8fb 100644 --- a/ProjectMonorail/Program.cs +++ b/ProjectMonorail/Program.cs @@ -11,7 +11,7 @@ namespace ProjectMonorail // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMonorail()); + Application.Run(new FormMonorailCollection()); } } } \ No newline at end of file diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..3b6b519 --- /dev/null +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,115 @@ +using ProjectMonorail.Scripts.Monorail.Drawnings; + +namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects +{ + /// + /// Абстракция компании, хранящий коллекцию монорельсов + /// + public abstract class AbstractCompany + { + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 210; + + /// + /// Размер места (высота) + /// + 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 pictureWidth, int pictureHeight, ICollectionGenericObjects collection) + { + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static bool operator +(AbstractCompany company, DrawingMonorail monorail) + { + return company._collection?.Insert(monorail) ?? false; + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static bool operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position) ?? false; + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawingMonorail? 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) + { + DrawingMonorail? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); + } +} diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs index af8ad8d..873a47b 100644 --- a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,7 @@ -namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects +using ProjectMonorail.Scripts.Monorail.Drawnings; +using System.Diagnostics; + +namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects { public class MassiveGenericObjects : ICollectionGenericObjects where T : class @@ -40,6 +43,7 @@ public T? Get(int position) { // TODO проверка позиции + if (_collection[position] == null) return null; return _collection[position]; } @@ -48,7 +52,7 @@ // TODO вставка в свободное место набора for (int i = 0; i < Count; i++) { - if (InsertingElementCollection(i, obj)) return true; + if (InsertingElementCollection(i, obj)) return true; } return false; diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MonorailSharingService.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MonorailSharingService.cs new file mode 100644 index 0000000..3d82f79 --- /dev/null +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MonorailSharingService.cs @@ -0,0 +1,57 @@ +using ProjectMonorail.Scripts.Monorail.Drawnings; +using System.Diagnostics; +using System.Drawing; + +namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects +{ + public class MonorailSharingService : AbstractCompany + { + public MonorailSharingService(int pictureWidth, int pictureHeight, ICollectionGenericObjects collection) : base(pictureWidth, pictureHeight, collection) {} + + private int maxCountX; + private int maxCountY; + private int offsetX = 30; + + protected override void DrawBackgound(Graphics g) + { + Pen pen = new Pen(Color.Black, 4); + + int maxCountX = _pictureWidth / _placeSizeWidth; + int maxCountY = _pictureHeight / _placeSizeHeight; + + + for (int i = 0; i < maxCountX; i++) + { + for (int j = 0; j < maxCountY; j++) + { + g.DrawLine(pen, i * offsetX + i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth + i * offsetX + i * _placeSizeWidth, j * _placeSizeHeight); + g.DrawLine(pen, i * offsetX + i * _placeSizeWidth, j * _placeSizeHeight, i * offsetX + i * _placeSizeWidth, _placeSizeHeight + j * _placeSizeHeight); + g.DrawLine(pen, i * offsetX + i * _placeSizeWidth, _placeSizeHeight + j * _placeSizeHeight, _placeSizeWidth + i * offsetX + i * _placeSizeWidth, _placeSizeHeight + j * _placeSizeHeight); + } + } + } + + protected override void SetObjectsPosition() + { + int maxCountX = _pictureWidth / _placeSizeWidth; + int maxCountY = _pictureHeight / _placeSizeHeight; + + int boarderOffsetX = 20; + int boarderOffsetY = 20; + + int currentIndex = -1; + + for (int j = 0; j < maxCountY; j++) + { + for (int i = 0; i < maxCountX; i++) + { + currentIndex++; + if (_collection.Get(currentIndex) == null) continue; + + _collection.Get(currentIndex).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(currentIndex).SetPosition(boarderOffsetX + i * _placeSizeWidth + i * offsetX, boarderOffsetY + j * _placeSizeHeight); + } + } + } + } +} -- 2.25.1 From 3ce36fbe16184e57e453c5ab9d740dad69ad3b02 Mon Sep 17 00:00:00 2001 From: ENDORFIT Date: Mon, 11 Mar 2024 15:22:10 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectMonorail/FormMonorailCollection.cs | 6 ++--- .../AbstractCompany.cs | 10 +++---- ...aringService.cs => DepotSharingService.cs} | 11 ++++---- .../ICollectionGenericObjects.cs | 10 ++++--- .../MassiveGenericObjects.cs | 27 ++++++++++--------- 5 files changed, 33 insertions(+), 31 deletions(-) rename ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/{MonorailSharingService.cs => DepotSharingService.cs} (82%) diff --git a/ProjectMonorail/FormMonorailCollection.cs b/ProjectMonorail/FormMonorailCollection.cs index b94fae4..7ab4a7f 100644 --- a/ProjectMonorail/FormMonorailCollection.cs +++ b/ProjectMonorail/FormMonorailCollection.cs @@ -31,7 +31,7 @@ namespace ProjectMonorail switch (comboBoxSelectorCompany.Text) { case "Хранилище": - _company = new MonorailSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + _company = new DepotSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); break; } } @@ -60,7 +60,7 @@ namespace ProjectMonorail return; } - if (_company + drawningMonorail) + if (_company + drawningMonorail != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); @@ -107,7 +107,7 @@ namespace ProjectMonorail int position = Convert.ToInt32(maskedTextBox.Text); - if (_company - position) + if (_company - position != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _company.Show(); diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs index 3b6b519..9c29fae 100644 --- a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/AbstractCompany.cs @@ -10,7 +10,7 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects /// /// Размер места (ширина) /// - protected readonly int _placeSizeWidth = 210; + protected readonly int _placeSizeWidth = 240; /// /// Размер места (высота) @@ -57,9 +57,9 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects /// Компания /// Добавляемый объект /// - public static bool operator +(AbstractCompany company, DrawingMonorail monorail) + public static int operator +(AbstractCompany company, DrawingMonorail monorail) { - return company._collection?.Insert(monorail) ?? false; + return company._collection?.Insert(monorail) ?? -1; } /// @@ -68,9 +68,9 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects /// Компания /// Номер удаляемого объекта /// - public static bool operator -(AbstractCompany company, int position) + public static DrawingMonorail operator -(AbstractCompany company, int position) { - return company._collection?.Remove(position) ?? false; + return company._collection?.Remove(position) ?? null; } /// diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MonorailSharingService.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/DepotSharingService.cs similarity index 82% rename from ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MonorailSharingService.cs rename to ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/DepotSharingService.cs index 3d82f79..86200c9 100644 --- a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MonorailSharingService.cs +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/DepotSharingService.cs @@ -4,9 +4,9 @@ using System.Drawing; namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects { - public class MonorailSharingService : AbstractCompany + public class DepotSharingService : AbstractCompany { - public MonorailSharingService(int pictureWidth, int pictureHeight, ICollectionGenericObjects collection) : base(pictureWidth, pictureHeight, collection) {} + public DepotSharingService(int pictureWidth, int pictureHeight, ICollectionGenericObjects collection) : base(pictureWidth, pictureHeight, collection) {} private int maxCountX; private int maxCountY; @@ -39,13 +39,12 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects int boarderOffsetX = 20; int boarderOffsetY = 20; - int currentIndex = -1; + int currentIndex = 0; - for (int j = 0; j < maxCountY; j++) + for (int j = maxCountY - 1; j >= 0; j--) { - for (int i = 0; i < maxCountX; i++) + for (int i = 0; i < maxCountX; i++, currentIndex++) { - currentIndex++; if (_collection.Get(currentIndex) == null) continue; _collection.Get(currentIndex).SetPictureSize(_pictureWidth, _pictureHeight); diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs index 4509b8d..182b693 100644 --- a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,4 +1,6 @@ -namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects +using ProjectMonorail.Scripts.Monorail.Drawnings; + +namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects { /// /// Интерфейс описания действий для набора хранимых объектов @@ -22,7 +24,7 @@ /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj); + int Insert(T obj); /// /// Добавление объекта в коллекцию на конкретную позицию @@ -30,14 +32,14 @@ /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj, int position); + int Insert(T obj, int position); /// /// Удаление объекта из коллекции с конкретной позиции /// /// Позиция /// true - удаление прошло удачно, false - удаление не удалось - bool Remove(int position); + T? Remove(int position); /// /// Получение объекта по позиции diff --git a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs index 873a47b..81ca0b2 100644 --- a/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectMonorail/Scripts/Monorail/CollectionGenericObjects/MassiveGenericObjects.cs @@ -43,54 +43,55 @@ namespace ProjectMonorail.Scripts.Monorail.CollectionGenericObjects public T? Get(int position) { // TODO проверка позиции - if (_collection[position] == null) return null; + if (!(position >= 0 && position < Count) || _collection[position] == null) return null; return _collection[position]; } - public bool Insert(T obj) + public int Insert(T obj) { // TODO вставка в свободное место набора for (int i = 0; i < Count; i++) { - if (InsertingElementCollection(i, obj)) return true; + if (InsertingElementCollection(i, obj)) return i; } - return false; + return -1; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то // ищется свободное место после этой позиции и идет вставка туда // если нет после, ищем до // TODO вставка - - if (InsertingElementCollection(position, obj)) return true; + if (!(position >= 0 && position < Count)) return -1; + if (InsertingElementCollection(position, obj)) return position; for (int i = position + 1; i < Count; i++) { - if (InsertingElementCollection(i, obj)) return true; + if (InsertingElementCollection(i, obj)) return i; } for (int i = position - 1; i >= 0; i--) { - if (InsertingElementCollection(i, obj)) return true; + if (InsertingElementCollection(i, obj)) return i; } - return false; + return -1; } - public bool Remove(int position) + public T Remove(int position) { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null - if (_collection[position] == null) return false; + if (!(position >= 0 && position < Count) || _collection[position] == null) return null; + T obj = _collection[position]; _collection[position] = null; - return true; + return obj; } /// -- 2.25.1