From 4d17d8edeebf72f01a2a48f2c49b5d7f289856da Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Mon, 1 Apr 2024 00:32:36 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=203=20=D0=BA=D1=80?= =?UTF-8?q?=D0=BE=D0=BC=D0=B5=20=D0=B0=D0=B1=D1=81=D1=82=D1=80=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 120 ++++++++++++ .../CollectionGenericObjects/BusStation.cs | 25 +++ .../ICollectionGenericObjects.cs | 54 ++++++ .../MassiveGenericObjects.cs | 98 ++++++++++ .../FormBusCollection.Designer.cs | 172 ++++++++++++++++++ .../DoubleDeckerBus/FormBusCollection.cs | 153 ++++++++++++++++ .../DoubleDeckerBus/FormBusCollection.resx | 120 ++++++++++++ .../FormDoubleDeckerBus.Designer.cs | 28 --- .../DoubleDeckerBus/FormDoubleDeckerBus.cs | 47 ++--- DoubleDeckerBus/DoubleDeckerBus/Program.cs | 2 +- 10 files changed, 755 insertions(+), 64 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..f55bf5a --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,120 @@ +using DoubleDeckerBus.Drawnings; +using System; +using System.CodeDom; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms.VisualStyles; + +namespace DoubleDeckerBus.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 picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static bool operator +(AbstractCompany company, DrawingBus bus) + { + return company._collection?.Insert(bus) ?? false; + } + + /// + /// Переугрузка оператора вычитания для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static bool operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position) ?? false; + } + + public DrawingBus? 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) + { + DrawingBus? obj = _collection?.Get(i); + obj?.DrawTrasnport(graphics); + } + + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackground(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); + +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs new file mode 100644 index 0000000..6027ca1 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs @@ -0,0 +1,25 @@ +using DoubleDeckerBus.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.CollectionGenericObjects; + +public class BusStation : AbstractCompany +{ + public BusStation(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/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..8c67a07 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.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/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..2c40eb9 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.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 bool Insert(T obj) + { + if (Insert(obj, 0)) + { + return true; + } + return false; + } + + public bool Insert(T obj, int position) + { + //проверка позиции + if (position < 0 || position > Count) + { + return false; + } + + if (_collection[position] == null) + { + _collection[position] = obj; + return true; + } + else + { + if (Insert(obj, position + 1)) + { + return true; + } + if (Insert(obj, position - 1)) + { + return true; + } + } + return false; + + } + + public bool Remove(int position) + { + if (position < 0 || position > Count) + { + return false; + } + _collection[position] = null; + return true; + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs new file mode 100644 index 0000000..dececd8 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -0,0 +1,172 @@ +namespace DoubleDeckerBus +{ + partial class FormBusCollection + { + /// + /// 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(); + buttonRemoveBus = new Button(); + maskedTextBox = new MaskedTextBox(); + buttonAddDoublrDeckerBus = new Button(); + buttonAddBus = new Button(); + comboBoxSelectCompany = 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(buttonRemoveBus); + groupBoxTools.Controls.Add(maskedTextBox); + groupBoxTools.Controls.Add(buttonAddDoublrDeckerBus); + groupBoxTools.Controls.Add(buttonAddBus); + groupBoxTools.Controls.Add(comboBoxSelectCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(600, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(200, 450); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Tools"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 390); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(188, 35); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Refresh"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(6, 312); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(188, 35); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Send to check"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveBus + // + buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveBus.Location = new Point(6, 225); + buttonRemoveBus.Name = "buttonRemoveBus"; + buttonRemoveBus.Size = new Size(188, 35); + buttonRemoveBus.TabIndex = 4; + buttonRemoveBus.Text = "Remove bus"; + buttonRemoveBus.UseVisualStyleBackColor = true; + buttonRemoveBus.Click += ButtonRemoveBus_Click; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(6, 196); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(182, 23); + maskedTextBox.TabIndex = 3; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonAddDoublrDeckerBus + // + buttonAddDoublrDeckerBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddDoublrDeckerBus.Location = new Point(6, 126); + buttonAddDoublrDeckerBus.Name = "buttonAddDoublrDeckerBus"; + buttonAddDoublrDeckerBus.Size = new Size(188, 35); + buttonAddDoublrDeckerBus.TabIndex = 2; + buttonAddDoublrDeckerBus.Text = "Add double-decker bus"; + buttonAddDoublrDeckerBus.UseVisualStyleBackColor = true; + buttonAddDoublrDeckerBus.Click += ButtonAddDoublrDeckerBus_Click; + // + // buttonAddBus + // + buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddBus.Location = new Point(6, 85); + buttonAddBus.Name = "buttonAddBus"; + buttonAddBus.Size = new Size(188, 35); + buttonAddBus.TabIndex = 1; + buttonAddBus.Text = "Add bus"; + buttonAddBus.UseVisualStyleBackColor = true; + buttonAddBus.Click += ButtonAddBus_Click; + // + // comboBoxSelectCompany + // + comboBoxSelectCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectCompany.FormattingEnabled = true; + comboBoxSelectCompany.Items.AddRange(new object[] { "Storage" }); + comboBoxSelectCompany.Location = new Point(6, 22); + comboBoxSelectCompany.Name = "comboBoxSelectCompany"; + comboBoxSelectCompany.Size = new Size(188, 23); + comboBoxSelectCompany.TabIndex = 0; + comboBoxSelectCompany.SelectedIndexChanged += comboBoxSelectCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(600, 450); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormBusCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormBusCollection"; + Text = "Bus collection"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectCompany; + private Button buttonAddDoublrDeckerBus; + private Button buttonAddBus; + private Button buttonRemoveBus; + private MaskedTextBox maskedTextBox; + private PictureBox pictureBox; + private Button buttonRefresh; + private Button buttonGoToCheck; + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs new file mode 100644 index 0000000..037219a --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs @@ -0,0 +1,153 @@ +using DoubleDeckerBus.CollectionGenericObjects; +using DoubleDeckerBus.Drawnings; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace DoubleDeckerBus; + +public partial class FormBusCollection : Form +{ + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormBusCollection() + { + InitializeComponent(); + } + + private void comboBoxSelectCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectCompany.Text) + { + case "Storage": + _company = new BusStation(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + + Random random = new(); + DrawingBus drawingBus; + switch (type) + { + case nameof(DrawingBus): + drawingBus = new DrawingBus(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawingDoubleDeckerBus): + drawingBus = new DrawingDoubleDeckerBus(random.Next(100, 300), random.Next(1000, 3000), + GetColor(random), + GetColor(random), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + + if (_company + drawingBus) + { + MessageBox.Show("Object added"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Failed to add object"); + } + } + + 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(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + + return color; + } + + private void ButtonAddBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingBus)); + + private void ButtonAddDoublrDeckerBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingDoubleDeckerBus)); + + private void ButtonRemoveBus_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) + { + return; + } + + if (MessageBox.Show("Remove object?", "Removal", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + + int pos = Convert.ToInt32(maskedTextBox.Text); + if (_company - pos) + { + MessageBox.Show("Object removed"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Failed to remove object"); + } + + } + + private void ButtonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + DrawingBus? bus = null; + int counter = 100; + while (bus == null) + { + bus = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (bus == null) + { + return; + } + + FormDoubleDeckerBus form = new() + { + SetBus = bus + }; + form.ShowDialog(); + } + + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.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/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs index a56dad7..b1a1711 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs @@ -28,29 +28,16 @@ /// private void InitializeComponent() { - buttonCreateDoubleDeckerBus = new Button(); pictureBoxDoubleDeckerBus = new PictureBox(); buttonLeft = new Button(); buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); - CreateBus = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); SuspendLayout(); // - // buttonCreateDoubleDeckerBus - // - buttonCreateDoubleDeckerBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateDoubleDeckerBus.Location = new Point(12, 415); - buttonCreateDoubleDeckerBus.Name = "buttonCreateDoubleDeckerBus"; - buttonCreateDoubleDeckerBus.Size = new Size(159, 23); - buttonCreateDoubleDeckerBus.TabIndex = 1; - buttonCreateDoubleDeckerBus.Text = "Create DoubleDeckerBus"; - buttonCreateDoubleDeckerBus.UseVisualStyleBackColor = true; - buttonCreateDoubleDeckerBus.Click += buttonCreateDoubleDeckerBus_Click; - // // pictureBoxDoubleDeckerBus // pictureBoxDoubleDeckerBus.Dock = DockStyle.Fill; @@ -108,17 +95,6 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // - // CreateBus - // - CreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - CreateBus.Location = new Point(188, 415); - CreateBus.Name = "CreateBus"; - CreateBus.Size = new Size(159, 23); - CreateBus.TabIndex = 7; - CreateBus.Text = "Create Bus"; - CreateBus.UseVisualStyleBackColor = true; - CreateBus.Click += buttonCreateBus_Click; - // // comboBoxStrategy // comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; @@ -148,12 +124,10 @@ ClientSize = new Size(800, 450); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(CreateBus); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonLeft); - Controls.Add(buttonCreateDoubleDeckerBus); Controls.Add(pictureBoxDoubleDeckerBus); Name = "FormDoubleDeckerBus"; StartPosition = FormStartPosition.CenterScreen; @@ -163,13 +137,11 @@ } #endregion - private Button buttonCreateDoubleDeckerBus; private PictureBox pictureBoxDoubleDeckerBus; private Button buttonLeft; private Button buttonUp; private Button buttonDown; private Button buttonRight; - private Button CreateBus; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs index e3168c9..5b34b25 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs @@ -17,6 +17,18 @@ namespace DoubleDeckerBus private DrawingBus? _drawingBus; private AbstractStrategy? _strategy; + + public DrawingBus SetBus + { + set + { + _drawingBus = value; + _drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } public FormDoubleDeckerBus() { InitializeComponent(); @@ -37,41 +49,6 @@ namespace DoubleDeckerBus pictureBoxDoubleDeckerBus.Image = bmp; } - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawingBus): - _drawingBus = new DrawingBus(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(DrawingDoubleDeckerBus): - _drawingBus = new DrawingDoubleDeckerBus(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)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - break; - default: - return; - } - - _drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); - _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - - private void buttonCreateDoubleDeckerBus_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawingDoubleDeckerBus)); - } - private void buttonCreateBus_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawingBus)); - } - private void buttonMove_Click(object sender, EventArgs e) { if (_drawingBus == null) diff --git a/DoubleDeckerBus/DoubleDeckerBus/Program.cs b/DoubleDeckerBus/DoubleDeckerBus/Program.cs index 913b697..8dd5e23 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Program.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Program.cs @@ -11,7 +11,7 @@ namespace DoubleDeckerBus // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormDoubleDeckerBus()); + Application.Run(new FormBusCollection()); } } } \ No newline at end of file -- 2.25.1 From 15399b84b1705df80a204a60efa5242538a3f851 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Mon, 1 Apr 2024 14:58:14 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B0=D0=B2=D1=82=D0=BE=D0=B1=D1=83=D1=81=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BE=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0,=20=D0=BE=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D1=82=D1=8C=20setObjectsPo?= =?UTF-8?q?sitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 2 +- .../CollectionGenericObjects/BusStation.cs | 41 ++++++++++++++++++- .../FormBusCollection.Designer.cs | 6 +-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs index f55bf5a..6693ce4 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs @@ -17,7 +17,7 @@ public abstract class AbstractCompany /// /// Размер места(ширина) /// - protected readonly int _placeSizeWidth = 210; + protected readonly int _placeSizeWidth = 130; /// /// Размер места(высота) diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs index 6027ca1..3e598d3 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs @@ -9,17 +9,54 @@ namespace DoubleDeckerBus.CollectionGenericObjects; public class BusStation : AbstractCompany { + + private int[]? _arrayOfCoordinates; + public BusStation(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) { } protected override void DrawBackground(Graphics g) { - throw new NotImplementedException(); + Pen pen = new Pen(Color.Black, 3); + + int gap = 15; + int y = 10; + int size_of_array = 2; + + while (y + _placeSizeHeight < _pictureHeight - gap) + { + int x = _pictureWidth - gap; + + while (x - _placeSizeWidth > gap) + { + g.DrawLine(pen, x, y, x - _placeSizeWidth, y); + g.DrawLine(pen, x, y, x, y + _placeSizeHeight); + g.DrawLine(pen, x, y + _placeSizeHeight, x - _placeSizeWidth, y + _placeSizeHeight); + + Array.Resize(ref _arrayOfCoordinates, size_of_array); + _arrayOfCoordinates[size_of_array - 2] = x; + _arrayOfCoordinates[size_of_array - 1] = y; + + x -= (_placeSizeWidth + 70); + } + y += _placeSizeHeight; + size_of_array += 2; + } } protected override void SetObjectsPosition() { - throw new NotImplementedException(); + if (_arrayOfCoordinates == null || _collection == null) + { + return; + } + + for (int i = 0, coordinate_index = 0; i < _collection.Count; i++, coordinate_index += 2) + { + _collection.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i)?.SetPosition(_arrayOfCoordinates[coordinate_index], _arrayOfCoordinates[coordinate_index + 1]); + + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs index dececd8..0bef3a8 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -51,7 +51,7 @@ groupBoxTools.Controls.Add(buttonAddBus); groupBoxTools.Controls.Add(comboBoxSelectCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(600, 0); + groupBoxTools.Location = new Point(870, 0); groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Size = new Size(200, 450); groupBoxTools.TabIndex = 0; @@ -138,7 +138,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(600, 450); + pictureBox.Size = new Size(870, 450); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -146,7 +146,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(1070, 450); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormBusCollection"; -- 2.25.1 From c4eacd38e5611f451ce568312b6e27cdf403ab6f Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Mon, 15 Apr 2024 13:32:00 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F=203=D1=8C=D1=8F=20=D0=BB=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 8 +-- .../CollectionGenericObjects/BusStation.cs | 12 ++--- .../ICollectionGenericObjects.cs | 6 +-- .../MassiveGenericObjects.cs | 51 ++++++++++--------- .../DoubleDeckerBus/FormBusCollection.cs | 4 +- 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs index 6693ce4..0ab649f 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs @@ -64,9 +64,9 @@ public abstract class AbstractCompany /// Компания /// Добавляемый объект /// - public static bool operator +(AbstractCompany company, DrawingBus bus) + public static int? operator +(AbstractCompany company, DrawingBus bus) { - return company._collection?.Insert(bus) ?? false; + return company._collection?.Insert(bus); } /// @@ -75,9 +75,9 @@ public abstract class AbstractCompany /// Компания /// Номер удаляемого объекта /// - public static bool operator -(AbstractCompany company, int position) + public static DrawingBus? operator -(AbstractCompany company, int position) { - return company._collection?.Remove(position) ?? false; + return company._collection?.Remove(position); } public DrawingBus? GetRandomObject() diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs index 3e598d3..cc974ed 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/BusStation.cs @@ -21,9 +21,9 @@ public class BusStation : AbstractCompany Pen pen = new Pen(Color.Black, 3); int gap = 15; - int y = 10; + int y = gap; int size_of_array = 2; - + while (y + _placeSizeHeight < _pictureHeight - gap) { int x = _pictureWidth - gap; @@ -35,13 +35,13 @@ public class BusStation : AbstractCompany g.DrawLine(pen, x, y + _placeSizeHeight, x - _placeSizeWidth, y + _placeSizeHeight); Array.Resize(ref _arrayOfCoordinates, size_of_array); - _arrayOfCoordinates[size_of_array - 2] = x; - _arrayOfCoordinates[size_of_array - 1] = y; + _arrayOfCoordinates[size_of_array - 2] = x - 120; + _arrayOfCoordinates[size_of_array - 1] = y + gap; - x -= (_placeSizeWidth + 70); + x -= (_placeSizeWidth + (_placeSizeWidth/2)); + size_of_array += 2; } y += _placeSizeHeight; - size_of_array += 2; } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs index 8c67a07..b5d541d 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -28,7 +28,7 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// true - вставка прошла успешно, false - вставка не удалась - bool Insert(T obj); + int Insert(T obj); /// /// Добавление элемента на конкретную позицию @@ -36,14 +36,14 @@ public interface ICollectionGenericObjects /// Добавляемый объект /// Позиция /// 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/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs index 2c40eb9..16ceba5 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -39,7 +39,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects _collection = Array.Empty(); } - public T? Get(int position)//возможно придётся доделать + public T? Get(int position) { if (position < 0 || position > Count) { @@ -49,50 +49,53 @@ public class MassiveGenericObjects : ICollectionGenericObjects } - public bool Insert(T obj) + public int Insert(T obj) { - if (Insert(obj, 0)) - { - return true; - } - return false; + return Insert(obj, 0); } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { - //проверка позиции + if (position < 0 || position > Count) { - return false; + return -1; } - if (_collection[position] == null) + int copy_of_position = position - 1; + + while (position < Count) { - _collection[position] = obj; - return true; + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + position++; } - else + while (copy_of_position > 0) { - if (Insert(obj, position + 1)) + if (_collection[position] == null) { - return true; - } - if (Insert(obj, position - 1)) - { - return true; + _collection[position] = obj; + return position; } + position--; } - return false; + + return -1; } - public bool Remove(int position) + public T? Remove(int position) { if (position < 0 || position > Count) { - return false; + return null; } + + T? removed_object = _collection[position]; _collection[position] = null; - return true; + return removed_object; } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs index 037219a..fb2f7aa 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs @@ -58,7 +58,7 @@ public partial class FormBusCollection : Form return; } - if (_company + drawingBus) + if ((_company + drawingBus) != -1) { MessageBox.Show("Object added"); pictureBox.Image = _company.Show(); @@ -98,7 +98,7 @@ public partial class FormBusCollection : Form } int pos = Convert.ToInt32(maskedTextBox.Text); - if (_company - pos) + if (_company - pos != null) { MessageBox.Show("Object removed"); pictureBox.Image = _company.Show(); -- 2.25.1 From eb48bafca80a0a749fe5ee1ff661c42bdd3f2542 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Thu, 25 Apr 2024 20:25:22 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B2=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B5=20=D0=B2?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF=D0=BE=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B7=D0=B8=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionGenericObjects/MassiveGenericObjects.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs index 16ceba5..614f73c 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -75,12 +75,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects } while (copy_of_position > 0) { - if (_collection[position] == null) + if (_collection[copy_of_position] == null) { - _collection[position] = obj; - return position; + _collection[copy_of_position] = obj; + return copy_of_position; } - position--; + copy_of_position--; } return -1; -- 2.25.1 From 59d11e7b84a1127b95b1252772a6c1b029c70288 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Thu, 25 Apr 2024 21:06:06 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BE=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionGenericObjects/MassiveGenericObjects.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs index 614f73c..887317e 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -41,7 +41,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position < 0 || position > Count) + if (position < 0 || position >= Count) { return null; } @@ -57,7 +57,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Insert(T obj, int position) { - if (position < 0 || position > Count) + if (position < 0 || position >= Count) { return -1; } @@ -89,7 +89,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Remove(int position) { - if (position < 0 || position > Count) + if (position < 0 || position >= Count) { return null; } -- 2.25.1