diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/AbstractCompany.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/AbstractCompany.cs new file mode 100644 index 0000000..7752bc8 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/AbstractCompany.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectDumpTruck.Drawnings; + +namespace ProjectDumpTruck.CollectionGenericObject; + +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 210; + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 80; + /// + /// Ширина окна + /// + protected readonly int _pictureWidth; + /// + /// Высота окна + /// + protected readonly int _pictureHeight; + /// + /// Коллекция грузовиков + /// + protected ICollectionGenericObject? _collection = null; + /// + /// Вычисление максимального количества элементов, который можно разместить в окне + /// + private int GetMaxCount => _pictureWidth * _pictureHeight / + (_placeSizeWidth * _placeSizeHeight); + /// + /// Конструктор + /// + /// Ширина окна + /// Высота окна + /// Коллекция грузовиков + public AbstractCompany(int picWidth, int picHeight, + ICollectionGenericObject collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static bool operator +(AbstractCompany company, DrawningTruck truck) + { + return company._collection?.Insert(truck) ?? false; + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static bool operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position) ?? false; + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningTruck? 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) + { + DrawningTruck? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); + +} diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/Autopark.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/Autopark.cs new file mode 100644 index 0000000..3523cc5 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/Autopark.cs @@ -0,0 +1,25 @@ +using ProjectDumpTruck.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.CollectionGenericObject; + +public class Autopark : AbstractCompany +{ + public Autopark(int picWidth, int picHeight, ICollectionGenericObject collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + throw new NotImplementedException(); + } + + protected override void SetObjectsPosition() + { + throw new NotImplementedException(); + } +} diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/ICollectionGenericObject.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/ICollectionGenericObject.cs index 9003992..59b57ef 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/ICollectionGenericObject.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObject/ICollectionGenericObject.cs @@ -20,6 +20,13 @@ public interface ICollectionGenericObject /// int SetMaxCount { set; } + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + bool Insert(T obj); + /// /// Добавление объекта на конкретную позиуцию /// @@ -33,8 +40,8 @@ public interface ICollectionGenericObject /// /// Позиция /// true - удаление прошло удачно, false - удаление не удалось - bool Remove(int position); + /// /// Получение объекта по позиции /// diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormDumpTruck.cs b/ProjectDumpTruck/ProjectDumpTruck/FormDumpTruck.cs index 9135466..90814c7 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/FormDumpTruck.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/FormDumpTruck.cs @@ -2,140 +2,139 @@ using ProjectDumpTruck.Drawnings; using ProjectDumpTruck.MovementStrategy; -namespace ProjectDumpTruck +namespace ProjectDumpTruck; + +public partial class FormDumpTruck : Form + { - public partial class FormDumpTruck : Form + private DrawningTruck? _drawningTruck; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + + public FormDumpTruck() { - private DrawningTruck? _drawningTruck; + InitializeComponent(); + _strategy = null; + } - /// - /// Стратегия перемещения - /// - private AbstractStrategy? _strategy; + private void Draw() + { + if (_drawningTruck == null) return; - public FormDumpTruck() + Bitmap bmp = new(pictureBoxDumpTruck1.Width, + pictureBoxDumpTruck1.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningTruck.DrawTransport(gr); + pictureBoxDumpTruck1.Image = bmp; + + } + + /// + /// Создание объекта класса-перемещения + /// + /// + private void CreateObject(string type) + { + + Random random = new(); + switch (type) { - InitializeComponent(); - _strategy = null; + case nameof(DrawningTruck): + _drawningTruck = new DrawningTruck(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(DrawningDumpTruck): + _drawningTruck = new DrawningDumpTruck(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; + } + _drawningTruck.SetPictureSize(pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height); + _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + //comboBoxStrategy.Enabled = true; + Draw(); + + } + + /// + /// Обработка нажатия кнокпки "Создать самосвал" + /// + /// + /// + private void ButtonCreateDumpTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningDumpTruck)); + + /// + /// Обработка нажатия кнокпки "Создать грузовик" + /// + /// + /// + private void ButtonCreateTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck)); + + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningTruck == null) return; + + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonLeft": + result = _drawningTruck.MoveTransport(DirectionType.Left); break; + case "buttonDown": + result = _drawningTruck.MoveTransport(DirectionType.Down); break; + case "buttonUp": + result = _drawningTruck.MoveTransport(DirectionType.Up); break; + case "buttonRight": + result = _drawningTruck.MoveTransport(DirectionType.Right); break; } - private void Draw() + if (result) Draw(); + + } + + /// + /// + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + + if (_drawningTruck == null) return; + + if (comboBoxStrategy.Enabled) { - if (_drawningTruck == null) return; - - Bitmap bmp = new(pictureBoxDumpTruck1.Width, - pictureBoxDumpTruck1.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningTruck.DrawTransport(gr); - pictureBoxDumpTruck1.Image = bmp; - - } - - /// - /// Создание объекта класса-перемещения - /// - /// - private void CreateObject(string type) - { - - Random random = new(); - switch (type) + _strategy = comboBoxStrategy.SelectedIndex switch { - case nameof(DrawningTruck): - _drawningTruck = new DrawningTruck(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(DrawningDumpTruck): - _drawningTruck = new DrawningDumpTruck(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; - } - _drawningTruck.SetPictureSize(pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height); - _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - //comboBoxStrategy.Enabled = true; - Draw(); - - } - - /// - /// Обработка нажатия кнокпки "Создать самосвал" - /// - /// - /// - private void ButtonCreateDumpTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningDumpTruck)); - - /// - /// Обработка нажатия кнокпки "Создать грузовик" - /// - /// - /// - private void ButtonCreateTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck)); - - private void ButtonMove_Click(object sender, EventArgs e) - { - if (_drawningTruck == null) return; - - string name = ((Button)sender)?.Name ?? string.Empty; - bool result = false; - switch (name) - { - case "buttonLeft": - result = _drawningTruck.MoveTransport(DirectionType.Left); break; - case "buttonDown": - result = _drawningTruck.MoveTransport(DirectionType.Down); break; - case "buttonUp": - result = _drawningTruck.MoveTransport(DirectionType.Up); break; - case "buttonRight": - result = _drawningTruck.MoveTransport(DirectionType.Right); break; - } - - if (result) Draw(); - - } - - /// - /// - /// - /// - /// - private void ButtonStrategyStep_Click(object sender, EventArgs e) - { - - if (_drawningTruck == null) return; - - if (comboBoxStrategy.Enabled) - { - _strategy = comboBoxStrategy.SelectedIndex switch - { - 0 => new MoveToCenter(), - 1 => new MoveToBorder(), - _ => null, - }; - - if (_strategy == null) return; - - _strategy.SetData(new MoveableTruck(_drawningTruck), pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height); - } + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; if (_strategy == null) return; - comboBoxStrategy.Enabled = false; - _strategy.MakeStep(); - Draw(); + _strategy.SetData(new MoveableTruck(_drawningTruck), pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height); + } - if (_strategy.GetStatus() == StrategyStatus.Finish) - { - comboBoxStrategy.Enabled = true; - _strategy = null; - } + if (_strategy == null) return; + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; } } } diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs new file mode 100644 index 0000000..a3aebd8 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs @@ -0,0 +1,168 @@ +namespace ProjectDumpTruck +{ + partial class FormTruckCollection + { + /// + /// 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(); + buttonRemoveTruck = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddDumpTruck = new Button(); + buttonAddTruck = 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(buttonRemoveTruck); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonAddDumpTruck); + groupBoxTools.Controls.Add(buttonAddTruck); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(888, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(238, 688); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Location = new Point(6, 517); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(226, 48); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Location = new Point(6, 360); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(226, 48); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тест"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveTruck + // + buttonRemoveTruck.Location = new Point(6, 258); + buttonRemoveTruck.Name = "buttonRemoveTruck"; + buttonRemoveTruck.Size = new Size(226, 48); + buttonRemoveTruck.TabIndex = 4; + buttonRemoveTruck.Text = "Удаление\r\n"; + buttonRemoveTruck.UseVisualStyleBackColor = true; + buttonRemoveTruck.Click += buttonRemoveTruck_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(6, 225); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(226, 27); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddDumpTruck + // + buttonAddDumpTruck.Location = new Point(6, 150); + buttonAddDumpTruck.Name = "buttonAddDumpTruck"; + buttonAddDumpTruck.Size = new Size(226, 48); + buttonAddDumpTruck.TabIndex = 2; + buttonAddDumpTruck.Text = "Добавление самосвала"; + buttonAddDumpTruck.UseVisualStyleBackColor = true; + buttonAddDumpTruck.Click += ButtonAddDumpTruck_Click; + // + // buttonAddTruck + // + buttonAddTruck.Location = new Point(6, 96); + buttonAddTruck.Name = "buttonAddTruck"; + buttonAddTruck.Size = new Size(226, 48); + buttonAddTruck.TabIndex = 1; + buttonAddTruck.Text = "Добавление грузовика"; + buttonAddTruck.UseVisualStyleBackColor = true; + buttonAddTruck.Click += ButtonAddTruck_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, 35); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(226, 28); + 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(888, 688); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormTruckCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1126, 688); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormTruckCollection"; + Text = "Коллекция грузовиков"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddTruck; + private Button buttonAddDumpTruck; + private Button buttonGoToCheck; + private Button buttonRemoveTruck; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + private Button buttonRefresh; + } +} \ No newline at end of file diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs new file mode 100644 index 0000000..34467d7 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs @@ -0,0 +1,129 @@ +using ProjectDumpTruck.CollectionGenericObject; +using ProjectDumpTruck.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 ProjectDumpTruck; + +public partial class FormTruckCollection : Form +{ + + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormTruckCollection() + { + InitializeComponent(); + } + + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new Autopark(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + /// + /// Создание объекта класса-перемещения + /// + /// + private void CreateObject(string type) + { + + if (_company == null) return; + + Random random = new(); + DrawningTruck drawningTruck; + switch (type) + { + case nameof(DrawningTruck): + drawningTruck = new DrawningTruck(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + + case nameof(DrawningDumpTruck): + drawningTruck = new DrawningDumpTruck(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; + } + + if (_company + drawningTruck) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else MessageBox.Show("Не удалось добавить объект"); + + } + + /// + /// Получение цвета + /// + /// Генератор случайных чисел + /// + private static Color GetColor(Random random) + { + Color color = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)); + ColorDialog dialog = new(); + + if (dialog.ShowDialog() == DialogResult.OK) { return color; } + + return color; + } + + private void ButtonAddTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck)); + + + private void ButtonAddDumpTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningDumpTruck)); + + private void buttonRemoveTruck_Click(object sender, EventArgs e) + { + + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) return; + + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; + + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_company - pos) + { + + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + + } + else MessageBox.Show("Не удалось удалить объект"); + + } + + private void ButtonRefresh_Click(object sender, EventArgs e) + { + + if (_company == null) return; + + pictureBox.Image = _company.Show(); + + } + + private void ButtonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) return; + + + } +} diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.resx b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.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