diff --git a/lab_0/CollectionGenericObject/AbstractCompany.cs b/lab_0/CollectionGenericObject/AbstractCompany.cs new file mode 100644 index 0000000..025fa32 --- /dev/null +++ b/lab_0/CollectionGenericObject/AbstractCompany.cs @@ -0,0 +1,116 @@ +using ProjectBus.Drawnings; + +namespace ProjectBus.CollectionGenericObject; +/// +/// Абстракция компании, хранящий коллекцию автомобилей +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 400; + + /// + /// Размер места (высота) + /// + 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, DrawningSimpleBus simplebus) + { + return company._collection.Insert(simplebus); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningSimpleBus? operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningSimpleBus? 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) + { + DrawningSimpleBus? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); +} + diff --git a/lab_0/CollectionGenericObject/BusStation.cs b/lab_0/CollectionGenericObject/BusStation.cs new file mode 100644 index 0000000..ddc7084 --- /dev/null +++ b/lab_0/CollectionGenericObject/BusStation.cs @@ -0,0 +1,56 @@ +using ProjectBus.Drawnings; +using ProjectBus.Entities; +using System; + +namespace ProjectBus.CollectionGenericObject; +/// +/// Реализация абстрактной компании - аренда поезда +/// +public class BusStation : AbstractCompany +{ + /// + /// Конструктор + /// + /// + /// + /// + public BusStation(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + Pen pen = new(Color.Black); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++) + { + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * j), new((int)(_placeSizeWidth * (i + 0.5f)), _placeSizeHeight * j)); + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * j), new(_placeSizeWidth * i, _placeSizeHeight * (j + 1))); + } + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * (_pictureHeight / _placeSizeHeight)), new((int)(_placeSizeWidth * (i + 0.5f)), _placeSizeHeight * (_pictureHeight / _placeSizeHeight))); + } + + + } + + protected override void SetObjectsPosition() + { + int n = 0; + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++) + { + DrawningSimpleBus? drawningSimpleBus = _collection?.Get(n); + n++; + if (drawningSimpleBus != null) + { + drawningSimpleBus.SetPictureSize(_pictureWidth, _pictureHeight); + drawningSimpleBus.SetPosition(i * _placeSizeWidth + 5, j * _placeSizeHeight + 5); + } + } + } + } +} + + diff --git a/lab_0/FormBus.Designer.cs b/lab_0/FormBus.Designer.cs index cdcb98c..d4e6261 100644 --- a/lab_0/FormBus.Designer.cs +++ b/lab_0/FormBus.Designer.cs @@ -29,12 +29,10 @@ private void InitializeComponent() { pictureBoxBus = new PictureBox(); - buttonCreate = new Button(); buttonLeft = new Button(); buttonRight = new Button(); buttonUp = new Button(); buttonDown = new Button(); - buttonCreateSimpleBus = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxBus).BeginInit(); @@ -49,17 +47,6 @@ pictureBoxBus.TabIndex = 7; pictureBoxBus.TabStop = false; // - // buttonCreate - // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 501); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(410, 46); - buttonCreate.TabIndex = 2; - buttonCreate.Text = "Создать автобус с доп.отсеком"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += ButtonCreate_Click; - // // buttonLeft // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -108,17 +95,6 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // - // buttonCreateSimpleBus - // - buttonCreateSimpleBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateSimpleBus.Location = new Point(428, 501); - buttonCreateSimpleBus.Name = "buttonCreateSimpleBus"; - buttonCreateSimpleBus.Size = new Size(431, 46); - buttonCreateSimpleBus.TabIndex = 8; - buttonCreateSimpleBus.Text = "Создать обычный автобус"; - buttonCreateSimpleBus.UseVisualStyleBackColor = true; - buttonCreateSimpleBus.Click += buttonCreateSimpleBus_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -146,12 +122,10 @@ ClientSize = new Size(1155, 557); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateSimpleBus); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonLeft); - Controls.Add(buttonCreate); Controls.Add(pictureBoxBus); Name = "FormBus"; Text = "Автобус"; @@ -161,12 +135,10 @@ #endregion private PictureBox pictureBoxBus; - private Button buttonCreate; private Button buttonLeft; private Button buttonRight; private Button buttonUp; private Button buttonDown; - private Button buttonCreateSimpleBus; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/lab_0/FormBus.cs b/lab_0/FormBus.cs index f63567b..d2c49c5 100644 --- a/lab_0/FormBus.cs +++ b/lab_0/FormBus.cs @@ -26,6 +26,22 @@ public partial class FormBus : Form /// private AbstractStrategy? _strategy; + /// + /// Получение объекта + /// + public DrawningSimpleBus SetSimpleBus + { + set + { + _drawningSimpleBus = value; + _drawningSimpleBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + + /// /// конструктор формы @@ -52,51 +68,6 @@ public partial class FormBus : Form pictureBoxBus.Image = bmp; } - /// - /// Создание объекта класса-перемещения - /// - /// Тип создаваемого объекта - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawningSimpleBus): - _drawningSimpleBus = new DrawningSimpleBus(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(DrawningBus): - _drawningSimpleBus = new DrawningBus(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; - } - - _drawningSimpleBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height); - _drawningSimpleBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - - Draw(); - } - - /// - /// Обработка нажатия кнопки "Создать автобус с доп отсеком" - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus)); - - /// - /// Обработка нажатия кнопки "Создать обычный автобус" - /// - /// - /// - private void buttonCreateSimpleBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSimpleBus)); - private void ButtonMove_Click(object sender, EventArgs e) @@ -179,3 +150,4 @@ public partial class FormBus : Form } + diff --git a/lab_0/FormSimpleBusCollection.Designer.cs b/lab_0/FormSimpleBusCollection.Designer.cs new file mode 100644 index 0000000..4d5aa9f --- /dev/null +++ b/lab_0/FormSimpleBusCollection.Designer.cs @@ -0,0 +1,174 @@ +namespace ProjectBus +{ + partial class FormSimpleBusCollection + { + /// + /// 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(); + buttonDelBus = new Button(); + maskedTextBox = new MaskedTextBox(); + buttonAddBus = new Button(); + buttonAddSimpleBus = 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(buttonDelBus); + groupBoxTools.Controls.Add(maskedTextBox); + groupBoxTools.Controls.Add(buttonAddBus); + groupBoxTools.Controls.Add(buttonAddSimpleBus); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(1063, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(333, 962); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 731); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(315, 84); + buttonRefresh.TabIndex = 7; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click_1; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(6, 565); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(315, 84); + buttonGoToCheck.TabIndex = 6; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click_1; + // + // buttonDelBus + // + buttonDelBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonDelBus.Location = new Point(6, 416); + buttonDelBus.Name = "buttonDelBus"; + buttonDelBus.Size = new Size(315, 84); + buttonDelBus.TabIndex = 5; + buttonDelBus.Text = "Удалить автобус"; + buttonDelBus.UseVisualStyleBackColor = true; + buttonDelBus.Click += ButtonDelBus_Click; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(6, 355); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(315, 39); + maskedTextBox.TabIndex = 4; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonAddBus + // + buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddBus.Location = new Point(6, 225); + buttonAddBus.Name = "buttonAddBus"; + buttonAddBus.Size = new Size(315, 84); + buttonAddBus.TabIndex = 2; + buttonAddBus.Text = "Добавление автобуса с доп. отсеком"; + buttonAddBus.UseVisualStyleBackColor = true; + buttonAddBus.Click += ButtonAddBus_Click; + // + // buttonAddSimpleBus + // + buttonAddSimpleBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddSimpleBus.Location = new Point(6, 118); + buttonAddSimpleBus.Name = "buttonAddSimpleBus"; + buttonAddSimpleBus.Size = new Size(315, 84); + buttonAddSimpleBus.TabIndex = 1; + buttonAddSimpleBus.Text = "Добавление автобуса"; + buttonAddSimpleBus.UseVisualStyleBackColor = true; + buttonAddSimpleBus.Click += ButtonAddSimpleBus_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, 47); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(315, 40); + 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(1063, 962); + pictureBox.TabIndex = 3; + pictureBox.TabStop = false; + // + // FormSimpleBusCollection + // + AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1396, 962); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormSimpleBusCollection"; + Text = "Коллекция автобусов"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddBus; + private Button buttonAddSimpleBus; + private Button buttonDelBus; + private MaskedTextBox maskedTextBox; + private PictureBox pictureBox; + private Button buttonRefresh; + private Button buttonGoToCheck; + } +} \ No newline at end of file diff --git a/lab_0/FormSimpleBusCollection.cs b/lab_0/FormSimpleBusCollection.cs new file mode 100644 index 0000000..0c52296 --- /dev/null +++ b/lab_0/FormSimpleBusCollection.cs @@ -0,0 +1,182 @@ +using ProjectBus.MovementStrategy; +using ProjectBus.Drawnings; +using ProjectBus.CollectionGenericObject; + +namespace ProjectBus; + +public partial class FormSimpleBusCollection : Form +{ + private AbstractCompany? _company = null; + public FormSimpleBusCollection() + { + InitializeComponent(); + } + + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "хранилище": + _company = new BusStation(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + private void ButtonAddSimpleBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSimpleBus)); + + /// + /// Добавление спортивного автомобиля + /// + /// + /// + private void ButtonAddBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus)); + + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + + /// + /// Получение цвета + /// + /// Генератор случайных чисел + /// + + + /// + /// Удаление объекта + /// + /// + /// + + + /// + /// Передача объекта в другую форму + /// + /// + /// + + + /// + /// Перерисовка коллекции + /// + /// + /// + + + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + Random random = new(); + DrawningSimpleBus drawningSimpleBus; + switch (type) + { + case nameof(DrawningSimpleBus): + drawningSimpleBus = new DrawningSimpleBus(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningBus): + // вызов диалогового окна для выбора цвета + drawningSimpleBus = new DrawningBus(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 + drawningSimpleBus != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + _ = MessageBox.Show(drawningSimpleBus.ToString()); + } + } + 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 ButtonDelBus_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) + { + return; + } + + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + + int pos = Convert.ToInt32(maskedTextBox.Text); + if (_company - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + + } + + private void ButtonRefresh_Click_1(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + + } + + private void ButtonGoToCheck_Click_1(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + DrawningSimpleBus? simplebus = null; + int counter = 100; + while (simplebus == null) + { + simplebus = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (simplebus == null) + { + return; + } + + FormBus form = new() + { + SetSimpleBus = simplebus + }; + form.ShowDialog(); + + } +} + diff --git a/lab_0/FormSimpleBusCollection.resx b/lab_0/FormSimpleBusCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/lab_0/FormSimpleBusCollection.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/lab_0/Program.cs b/lab_0/Program.cs index 919414c..beb3124 100644 --- a/lab_0/Program.cs +++ b/lab_0/Program.cs @@ -13,7 +13,7 @@ namespace lab_0 // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormBus()); + Application.Run(new FormSimpleBusCollection()); } } } \ No newline at end of file