diff --git a/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/AbstractCompany.cs b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..8beb3a2 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectCatamaran.Drawnings; + +namespace ProjectCatamaran.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, DrawningBoat boat) + { + return company._collection?.Insert(boat) ?? false; + } + + public static bool operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position) ?? false; + } + + public DrawningBoat? GetRandomObject() + { + Random rnd = new Random(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + + public Bitmap Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackground(graphics); + SetObjectPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningBoat? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + + return bitmap; + } + protected abstract void DrawBackground(Graphics g); + protected abstract void SetObjectPosition(); +} + diff --git a/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/BoatSharingService.cs b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/BoatSharingService.cs new file mode 100644 index 0000000..b5ee8f4 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/BoatSharingService.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectCatamaran.Drawnings; + +namespace ProjectCatamaran.CollectionGenericObjects; +public class BoatSharingService : AbstractCompany +{ + public BoatSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackground(Graphics g) + { + throw new NotImplementedException(); + } + + protected override void SetObjectPosition() + { + throw new NotImplementedException(); + } +} diff --git a/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..dc39bc6 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.CollectionGenericObjects; + +public interface ICollectionGenericObjects where T : class +{ + int Count { get; } + int SetMaxCount { set; } + bool Insert(T obj); + bool Insert(T obj, int position); + bool Remove(int position); + + T? Get(int position); +} + diff --git a/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..8c26b44 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.CollectionGenericObjects; + +public class MassiveGenericObjects : ICollectionGenericObjects where T : class +{ + private T?[] _collection; + public int Count => _collection.Length; + + public int SetMaxCount + { + set + { + if (value > 0) + { + _collection = new T?[value]; + } + } + } + + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + public T? Get(int position) + { + //todo + return _collection[position]; + } + + public bool Insert(T obj) + { + //todo + return false; + } + + public bool Insert(T obj, int position) + { + //todo + return false; + } + + public bool Remove(int position) + { + //todo + return true; + } +} + diff --git a/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.Designer.cs b/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.Designer.cs new file mode 100644 index 0000000..46b8509 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.Designer.cs @@ -0,0 +1,173 @@ +namespace ProjectCatamaran +{ + partial class FormBoatCollection + { + /// + /// 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(); + buttonRemoveBoat = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddCatamaran = new Button(); + buttonAddBoat = 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(buttonRemoveBoat); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonAddCatamaran); + groupBoxTools.Controls.Add(buttonAddBoat); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(631, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(169, 450); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 381); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(157, 39); + 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, 324); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(157, 39); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveBoat + // + buttonRemoveBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveBoat.Location = new Point(6, 279); + buttonRemoveBoat.Name = "buttonRemoveBoat"; + buttonRemoveBoat.Size = new Size(157, 39); + buttonRemoveBoat.TabIndex = 4; + buttonRemoveBoat.Text = "Удалить лодку"; + buttonRemoveBoat.UseVisualStyleBackColor = true; + buttonRemoveBoat.Click += ButtonRemoveBoat_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(1, 233); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(162, 23); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddCatamaran + // + buttonAddCatamaran.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddCatamaran.Location = new Point(6, 125); + buttonAddCatamaran.Name = "buttonAddCatamaran"; + buttonAddCatamaran.Size = new Size(157, 39); + buttonAddCatamaran.TabIndex = 2; + buttonAddCatamaran.Text = "Добавление катамарана"; + buttonAddCatamaran.UseVisualStyleBackColor = true; + buttonAddCatamaran.Click += ButtonAddCatamaran_Click; + // + // buttonAddBoat + // + buttonAddBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddBoat.Location = new Point(6, 80); + buttonAddBoat.Name = "buttonAddBoat"; + buttonAddBoat.Size = new Size(157, 39); + buttonAddBoat.TabIndex = 1; + buttonAddBoat.Text = "Добавление лодки"; + buttonAddBoat.UseVisualStyleBackColor = true; + buttonAddBoat.Click += ButtonAddBoat_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, 22); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(157, 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(631, 450); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormBoatCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormBoatCollection"; + Text = "Коллекция лодок"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private Button buttonAddCatamaran; + private Button buttonAddBoat; + private ComboBox comboBoxSelectorCompany; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + private Button buttonRemoveBoat; + private Button buttonRefresh; + private Button buttonGoToCheck; + } +} \ No newline at end of file diff --git a/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.cs b/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.cs new file mode 100644 index 0000000..b22475c --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.cs @@ -0,0 +1,150 @@ +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; +using ProjectCatamaran.CollectionGenericObjects; +using ProjectCatamaran.Drawnings; + +namespace ProjectCatamaran; +public partial class FormBoatCollection : Form +{ + private AbstractCompany? _company; + public FormBoatCollection() + { + InitializeComponent(); + } + + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new BoatSharingService(pictureBox.Width, pictureBox.Height, + new MassiveGenericObjects()); + break; + } + } + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + Random random = new(); + DrawningBoat drawningBoat; + switch (type) + { + case nameof(DrawningBoat): + drawningBoat = new DrawningBoat(random.Next(100, 300), random.Next(1000, 3000), + GetColor(random)); + break; + case nameof(DrawningCatamaran): + drawningBoat = new DrawningCatamaran(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 + drawningBoat) + { + 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(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + + return color; + } + + private void ButtonAddBoat_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningBoat)); + } + + private void ButtonAddCatamaran_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningCatamaran)); + + } + + private void ButtonRemoveBoat_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 ButtonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + DrawningBoat? boat = null; + int counter = 100; + while (boat == null) + { + boat = _company.GetRandomObject(); + counter--; + if (counter <= 0) + break; + } + + if (boat == null) + return; + FormCatamaran form = new() + { + SetBoat = boat + }; + form.ShowDialog(); + } + + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + } +} diff --git a/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.resx b/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/FormBoatCollection.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/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs index 5c03681..6b7659f 100644 --- a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs +++ b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs @@ -29,14 +29,13 @@ private void InitializeComponent() { pictureBoxCatamaran = new PictureBox(); - buttonCreateCatamaran = new Button(); buttonUp = new Button(); buttonRight = new Button(); buttonLeft = new Button(); buttonDown = new Button(); - buttonCreateBoat = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); + colorDialog1 = new ColorDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxCatamaran).BeginInit(); SuspendLayout(); // @@ -45,21 +44,10 @@ pictureBoxCatamaran.Dock = DockStyle.Fill; pictureBoxCatamaran.Location = new Point(0, 0); pictureBoxCatamaran.Name = "pictureBoxCatamaran"; - pictureBoxCatamaran.Size = new Size(801, 450); + pictureBoxCatamaran.Size = new Size(800, 450); pictureBoxCatamaran.TabIndex = 0; pictureBoxCatamaran.TabStop = false; // - // buttonCreateCatamaran - // - buttonCreateCatamaran.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonCreateCatamaran.Location = new Point(12, 402); - buttonCreateCatamaran.Name = "buttonCreateCatamaran"; - buttonCreateCatamaran.Size = new Size(166, 40); - buttonCreateCatamaran.TabIndex = 1; - buttonCreateCatamaran.Text = "Создать катамаран"; - buttonCreateCatamaran.UseVisualStyleBackColor = true; - buttonCreateCatamaran.Click += ButtonCreateCatamaran_Click; - // // buttonUp // buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -108,17 +96,6 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // - // buttonCreateBoat - // - buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonCreateBoat.Location = new Point(184, 402); - buttonCreateBoat.Name = "buttonCreateBoat"; - buttonCreateBoat.Size = new Size(166, 40); - buttonCreateBoat.TabIndex = 6; - buttonCreateBoat.Text = "Создать лодку"; - buttonCreateBoat.UseVisualStyleBackColor = true; - buttonCreateBoat.Click += ButtonCreateBoat_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -147,12 +124,10 @@ ClientSize = new Size(800, 450); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateBoat); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonRight); Controls.Add(buttonUp); - Controls.Add(buttonCreateCatamaran); Controls.Add(pictureBoxCatamaran); Name = "FormCatamaran"; Text = "Катамаран"; @@ -163,13 +138,12 @@ #endregion private PictureBox pictureBoxCatamaran; - private Button buttonCreateCatamaran; private Button buttonUp; private Button buttonRight; private Button buttonLeft; private Button buttonDown; - private Button buttonCreateBoat; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; + private ColorDialog colorDialog1; } } \ No newline at end of file diff --git a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs index e008569..c547223 100644 --- a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs +++ b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs @@ -17,6 +17,18 @@ namespace ProjectCatamaran { private DrawningBoat? _drawningBoat; private AbstractStrategy? _strategy; + + public DrawningBoat SetBoat + { + set + { + _drawningBoat = value; + _drawningBoat.SetPictureSize(pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } public FormCatamaran() { InitializeComponent(); @@ -64,39 +76,6 @@ namespace ProjectCatamaran } } - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawningBoat): - _drawningBoat = new DrawningBoat(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(DrawningCatamaran): - _drawningBoat = new DrawningCatamaran(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; - } - _drawningBoat.SetPictureSize(pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); - _drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - private void ButtonCreateCatamaran_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawningCatamaran)); - } - - private void ButtonCreateBoat_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawningBoat)); - } /// /// Обработка нажатия кнопки "Шаг" /// diff --git a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.resx b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.resx index af32865..818dfae 100644 --- a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.resx +++ b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file