From c06d9588bfd6268d86ab64930e6e48f7ace2e9ee Mon Sep 17 00:00:00 2001 From: Itos Date: Thu, 3 Oct 2024 00:21:27 +0300 Subject: [PATCH] LAB4_DONE --- .../Drawings/DrawingGasolineTanker.cs | 1 - .../Drawings/DrawingTruck.cs | 19 +- .../FormGasolineTanker.cs | 43 +++- .../FormTruckCollection.Designer.cs | 185 ++++++++++++------ .../FormTruckCollection.cs | 116 ++++++++--- .../FormTruckCollection.resx | 6 + .../Generic/SetGeneric.cs | 100 ++++------ .../Generic/TruckGenericCollection.cs | 47 ++--- .../Generic/TruckGenericStorage.cs | 63 ++++++ .../MovementStratg/AbstractStrategy.cs | 7 +- .../MovementStratg/IMoveableObject.cs | 4 - .../MovementStratg/MoveToCenter.cs | 4 +- .../MovementStratg/ObjectParameters.cs | 1 + 13 files changed, 395 insertions(+), 201 deletions(-) create mode 100644 GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericStorage.cs diff --git a/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs b/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs index de8e838..a3a81ca 100644 --- a/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs +++ b/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs @@ -11,7 +11,6 @@ namespace ProjectGasolineTanker.Drawings public class DrawingGasolineTanker : DrawingTruck { - public DrawingGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, bool tank, bool wheel, int width, int height) : base(speed, weight, bodyColor, width, height, 130, 70) { if (EntityTruck != null) diff --git a/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingTruck.cs b/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingTruck.cs index 77e16cc..977c7f6 100644 --- a/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingTruck.cs +++ b/GasolineTanker/ProjectGasolineTanker/Drawings/DrawingTruck.cs @@ -9,7 +9,7 @@ using ProjectGasolineTanker.MovementStratg; namespace ProjectGasolineTanker.Drawings { - + public class DrawingTruck { public IMoveableObject GetMoveableObject => new DrawingObjectTruck(this); @@ -27,7 +27,7 @@ namespace ProjectGasolineTanker.Drawings protected readonly int _tankerWidth = 130; protected readonly int _tankerHeight = 70; - + public int GetPosX => _startPosX; public int GetPosY => _startPosY; @@ -50,7 +50,7 @@ namespace ProjectGasolineTanker.Drawings protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int tankerWidth, int tankerHeight) { - // TODO: Продумать проверки + if (width < _tankerWidth || height < _tankerHeight) { return; @@ -117,11 +117,11 @@ namespace ProjectGasolineTanker.Drawings Pen pen = new(Color.Black); Brush additionalBrush = new SolidBrush(EntityTruck.BodyColor); - g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 11, 71, 28); //канистра-бак - g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 20, 71, 9); //полосы + g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 11, 71, 28); + g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 20, 71, 9); - g.DrawRectangle(pen, _startPosX + 29, _startPosY + 11, 71, 28); //канистра-бак - g.DrawRectangle(pen, _startPosX + 29, _startPosY + 20, 71, 9); //полосы + g.DrawRectangle(pen, _startPosX + 29, _startPosY + 11, 71, 28); + g.DrawRectangle(pen, _startPosX + 29, _startPosY + 20, 71, 9); Brush additionalBrush1 = new @@ -145,7 +145,6 @@ namespace ProjectGasolineTanker.Drawings g.FillRectangle(additionalBrush2, _startPosX + 4, _startPosY + 40, 130, 25); g.DrawLine(pen, _startPosX + 4, _startPosY + 65, _startPosX + 25, _startPosY + 40); - //колёса Brush gr = new SolidBrush(Color.Gray); g.FillEllipse(additionalBrush, _startPosX + 15, _startPosY + 50, 26, 26); @@ -171,9 +170,9 @@ namespace ProjectGasolineTanker.Drawings //вверх DirectionType.Up => _startPosY - EntityTruck.Step > 0, // вправо - DirectionType.Right => _startPosX + _tankerWidth + EntityTruck.Step < _pictureWidth,// TODO: Продумать логику + DirectionType.Right => _startPosX + _tankerWidth + EntityTruck.Step < _pictureWidth, //вниз - DirectionType.Down => _startPosY + _tankerHeight + EntityTruck.Step < _pictureHeight,// TODO: Продумать логику + DirectionType.Down => _startPosY + _tankerHeight + EntityTruck.Step < _pictureHeight, _ => false, }; } diff --git a/GasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs b/GasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs index d2894da..510e0d8 100644 --- a/GasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs +++ b/GasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs @@ -4,14 +4,23 @@ using ProjectGasolineTanker.MovementStratg; namespace ProjectGasolineTanker { + /// + /// + /// public partial class FormGasolineTanker : Form { - + /// + /// - + /// private DrawingTruck? _drawingTruck; - + /// + /// + /// private AbstractStrategy? _abstractStrategy; - + /// + /// + /// public DrawingTruck? SelectedTruck { get; private set; } public FormGasolineTanker() @@ -20,7 +29,9 @@ namespace ProjectGasolineTanker _abstractStrategy = null; SelectedTruck = null; } - + /// + /// + /// private void Draw() { if (_drawingTruck == null) @@ -33,7 +44,11 @@ namespace ProjectGasolineTanker pictureBoxGasolineTanker.Image = bmp; } - + /// + /// " " + /// + /// + /// private void buttonCreateProjectGasolineTanker_Click(object sender, EventArgs e) { Random random = new(); @@ -56,7 +71,11 @@ namespace ProjectGasolineTanker _drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } - + /// + /// " ProjectGasolineTanker" + /// + /// + /// private void buttonCreateTruck_Click(object sender, EventArgs e) { Random random = new(); @@ -75,7 +94,11 @@ namespace ProjectGasolineTanker Draw(); } - + /// + /// + /// + /// + /// private void buttonMove_Click(object sender, EventArgs e) { if (_drawingTruck == null) @@ -100,7 +123,11 @@ namespace ProjectGasolineTanker } Draw(); } - + /// + /// "" + /// + /// + /// private void buttonStep_Click(object sender, EventArgs e) { if (_drawingTruck == null) diff --git a/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.Designer.cs b/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.Designer.cs index a3042c1..ed7380f 100644 --- a/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.Designer.cs +++ b/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.Designer.cs @@ -28,88 +28,155 @@ /// private void InitializeComponent() { + this.buttonAddTruck = new System.Windows.Forms.Button(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.labelInstruments = new System.Windows.Forms.Label(); - this.buttonAdd = new System.Windows.Forms.Button(); - this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); - this.buttonDelete = new System.Windows.Forms.Button(); this.buttonUpdate = new System.Windows.Forms.Button(); + this.buttonDeleteTruck = new System.Windows.Forms.Button(); + this.colorDialog = new System.Windows.Forms.ColorDialog(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonAddStorage = new System.Windows.Forms.Button(); + this.buttonDeleteStorage = new System.Windows.Forms.Button(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); // + // buttonAddTruck + // + this.buttonAddTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonAddTruck.Location = new System.Drawing.Point(734, 420); + this.buttonAddTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonAddTruck.Name = "buttonAddTruck"; + this.buttonAddTruck.Size = new System.Drawing.Size(157, 37); + this.buttonAddTruck.TabIndex = 0; + this.buttonAddTruck.Text = "Добавить грузовик"; + this.buttonAddTruck.UseVisualStyleBackColor = true; + this.buttonAddTruck.Click += new System.EventHandler(this.buttonAddTruck_Click); + // // pictureBoxCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(2, 1); - this.pictureBoxCollection.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.pictureBoxCollection.Location = new System.Drawing.Point(1, 12); + this.pictureBoxCollection.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(803, 376); - this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.Size = new System.Drawing.Size(668, 381); + this.pictureBoxCollection.TabIndex = 1; this.pictureBoxCollection.TabStop = false; // // labelInstruments // + this.labelInstruments.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.labelInstruments.AutoSize = true; - this.labelInstruments.Location = new System.Drawing.Point(852, 22); + this.labelInstruments.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.labelInstruments.Location = new System.Drawing.Point(753, 12); this.labelInstruments.Name = "labelInstruments"; - this.labelInstruments.Size = new System.Drawing.Size(83, 15); - this.labelInstruments.TabIndex = 1; + this.labelInstruments.Size = new System.Drawing.Size(136, 28); + this.labelInstruments.TabIndex = 2; this.labelInstruments.Text = "Инструменты"; // - // buttonAdd - // - this.buttonAdd.Location = new System.Drawing.Point(840, 64); - this.buttonAdd.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.buttonAdd.Name = "buttonAdd"; - this.buttonAdd.Size = new System.Drawing.Size(161, 44); - this.buttonAdd.TabIndex = 2; - this.buttonAdd.Text = "Добавить грузовик"; - this.buttonAdd.UseVisualStyleBackColor = true; - this.buttonAdd.Click += new System.EventHandler(this.ButtonAddTruck_Click); - // - // maskedTextBoxNumber - // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(874, 140); - this.maskedTextBoxNumber.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(110, 23); - this.maskedTextBoxNumber.TabIndex = 3; - // - // buttonDelete - // - this.buttonDelete.Location = new System.Drawing.Point(840, 167); - this.buttonDelete.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.buttonDelete.Name = "buttonDelete"; - this.buttonDelete.Size = new System.Drawing.Size(165, 45); - this.buttonDelete.TabIndex = 4; - this.buttonDelete.Text = "Удалить грузовик"; - this.buttonDelete.UseVisualStyleBackColor = true; - this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveTruck_Click); - // // buttonUpdate // - this.buttonUpdate.Location = new System.Drawing.Point(840, 260); - this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonUpdate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUpdate.Location = new System.Drawing.Point(741, 172); + this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonUpdate.Name = "buttonUpdate"; - this.buttonUpdate.Size = new System.Drawing.Size(165, 45); - this.buttonUpdate.TabIndex = 5; - this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.Size = new System.Drawing.Size(150, 33); + this.buttonUpdate.TabIndex = 3; + this.buttonUpdate.Text = "Обновить набор"; this.buttonUpdate.UseVisualStyleBackColor = true; - this.buttonUpdate.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // buttonDeleteTruck + // + this.buttonDeleteTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDeleteTruck.Location = new System.Drawing.Point(734, 537); + this.buttonDeleteTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonDeleteTruck.Name = "buttonDeleteTruck"; + this.buttonDeleteTruck.Size = new System.Drawing.Size(157, 37); + this.buttonDeleteTruck.TabIndex = 4; + this.buttonDeleteTruck.Text = "Удалить грузовик"; + this.buttonDeleteTruck.UseVisualStyleBackColor = true; + this.buttonDeleteTruck.Click += new System.EventHandler(this.buttonDeleteTruck_Click); + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(734, 480); + this.maskedTextBoxNumber.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.maskedTextBoxNumber.Mask = "00"; + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(157, 27); + this.maskedTextBoxNumber.TabIndex = 5; + this.maskedTextBoxNumber.ValidatingType = typeof(int); + // + // label1 + // + this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(741, 63); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(66, 20); + this.label1.TabIndex = 7; + this.label1.Text = "Наборы"; + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 20; + this.listBoxStorages.Location = new System.Drawing.Point(741, 209); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(150, 144); + this.listBoxStorages.TabIndex = 8; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged); + // + // buttonAddStorage + // + this.buttonAddStorage.Location = new System.Drawing.Point(741, 132); + this.buttonAddStorage.Name = "buttonAddStorage"; + this.buttonAddStorage.Size = new System.Drawing.Size(150, 33); + this.buttonAddStorage.TabIndex = 9; + this.buttonAddStorage.Text = "Добавить набор"; + this.buttonAddStorage.UseVisualStyleBackColor = true; + this.buttonAddStorage.Click += new System.EventHandler(this.buttonAddStorage_Click); + // + // buttonDeleteStorage + // + this.buttonDeleteStorage.Location = new System.Drawing.Point(741, 360); + this.buttonDeleteStorage.Name = "buttonDeleteStorage"; + this.buttonDeleteStorage.Size = new System.Drawing.Size(150, 33); + this.buttonDeleteStorage.TabIndex = 10; + this.buttonDeleteStorage.Text = "Удалить набор"; + this.buttonDeleteStorage.UseVisualStyleBackColor = true; + this.buttonDeleteStorage.TabIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged); + this.buttonDeleteStorage.Click += new System.EventHandler(this.buttonDeleteStorage_Click); + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(741, 99); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(150, 27); + this.textBoxStorageName.TabIndex = 11; // // FormTruckCollection // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1152, 382); - this.Controls.Add(this.buttonUpdate); - this.Controls.Add(this.buttonDelete); + this.ClientSize = new System.Drawing.Size(911, 689); + this.Controls.Add(this.textBoxStorageName); + this.Controls.Add(this.buttonDeleteStorage); + this.Controls.Add(this.buttonAddStorage); + this.Controls.Add(this.listBoxStorages); + this.Controls.Add(this.label1); + this.Controls.Add(this.buttonAddTruck); this.Controls.Add(this.maskedTextBoxNumber); - this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.buttonDeleteTruck); + this.Controls.Add(this.buttonUpdate); this.Controls.Add(this.labelInstruments); this.Controls.Add(this.pictureBoxCollection); - this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormTruckCollection"; - this.Text = "Грузовик"; + this.Text = "Набор грузовиков"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -118,11 +185,17 @@ #endregion + private Button buttonAddTruck; private PictureBox pictureBoxCollection; private Label labelInstruments; - private Button buttonAdd; - private TextBox maskedTextBoxNumber; - private Button buttonDelete; private Button buttonUpdate; + private Button buttonDeleteTruck; + private ColorDialog colorDialog; + private MaskedTextBox maskedTextBoxNumber; + private Label label1; + private ListBox listBoxStorages; + private Button buttonAddStorage; + private Button buttonDeleteStorage; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.cs b/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.cs index 3801af1..4f48e79 100644 --- a/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.cs +++ b/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.cs @@ -7,33 +7,92 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -using ProjectGasolineTanker.Drawings; using ProjectGasolineTanker.Generic; +using ProjectGasolineTanker.Drawings; using ProjectGasolineTanker.MovementStratg; +using ProjectGasolineTanker; namespace ProjectGasolineTanker { public partial class FormTruckCollection : Form { - private readonly TruckGenericCollection _truck; + private readonly TruckGenericStorage _storage; public FormTruckCollection() { InitializeComponent(); - _truck = new TruckGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new TruckGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorages.Items.Add(_storage.Keys[i]); + } + if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) + { + listBoxStorages.SelectedIndex = 0; + } + else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count) + { + listBoxStorages.SelectedIndex = index; + } } - private void ButtonAddTruck_Click(object sender, EventArgs e) + private void buttonAddStorage_Click(object sender, EventArgs e) { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + + private void listBoxObjects_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTruck(); + } + + private void buttonDeleteStorage_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } + } + + private void buttonAddTruck_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + MessageBox.Show("Не выбран набор"); + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormGasolineTanker form = new(); + if (form.ShowDialog() == DialogResult.OK) { - if (_truck + form.SelectedTruck != -1) + if (obj + form.SelectedTruck != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _truck.ShowTruck(); + pictureBoxCollection.Image = obj.ShowTruck(); } else { @@ -42,44 +101,55 @@ namespace ProjectGasolineTanker } } - private void ButtonRemoveTruck_Click(object sender, EventArgs e) + private void buttonDeleteTruck_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } - int pos = 0; try { - pos = Convert.ToInt32(maskedTextBoxNumber.Text) ; - + pos = Convert.ToInt32(maskedTextBoxNumber.Text); } catch { - MessageBox.Show("Ошибка ввода данных"); + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - - - if (_truck - (_truck.ReturnLength() - pos)) + if (obj - pos) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _truck.ShowTruck(); + pictureBoxCollection.Image = obj.ShowTruck(); } else { MessageBox.Show("Не удалось удалить объект"); } - } - - private void ButtonRefreshCollection_Click(object sender, EventArgs - e) + + private void buttonUpdate_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _truck.ShowTruck(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowTruck(); } - } } diff --git a/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.resx b/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.resx index f298a7b..f1dbd20 100644 --- a/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.resx +++ b/GasolineTanker/ProjectGasolineTanker/FormTruckCollection.resx @@ -57,4 +57,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 36 + \ No newline at end of file diff --git a/GasolineTanker/ProjectGasolineTanker/Generic/SetGeneric.cs b/GasolineTanker/ProjectGasolineTanker/Generic/SetGeneric.cs index e2551c0..2375bf6 100644 --- a/GasolineTanker/ProjectGasolineTanker/Generic/SetGeneric.cs +++ b/GasolineTanker/ProjectGasolineTanker/Generic/SetGeneric.cs @@ -10,97 +10,75 @@ namespace ProjectGasolineTanker.Generic where T : class { - private readonly T?[] _places; + private readonly List _places; - public int Count => _places.Length - 1; + public int Count => _places.Count; + + private readonly int _maxCount; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } public int Insert(T truck) { - int pos = Count - 1; - - if (_places[Count] != null) - { - - - for (int i = pos; i > 0; --i) - { - if (_places[i] == null) - { - pos = i; - break; - } - } - - for (int i = pos + 1; i <= Count; ++i) - { - _places[i - 1] = _places[i]; - } - } - - _places[Count] = truck; - return pos; + _places.Insert(0, truck); + return 0; } - public bool Insert(T truck, int position) { - // TODO проверка позиции - if (position < 0 || position > Count) + if (position < 0 || position >= Count || Count >= _maxCount) { return false; } - if (_places[Count] != null) - { - - int pos = Count; - for (int i = Count; i > 0; --i) - { - - if (_places[i] == null) - { - pos = i; - break; - } - } - - - for (int i = Count; i >= pos; --i) - { - _places[i - 1] = _places[i]; - } - } - - _places[Count] = truck; + _places.Insert(position, truck); return true; } - public bool Remove(int position) { - - if (position < 0 || position > Count) + if (position < 0 || position >= Count) { return false; } - _places[position] = null; + _places.RemoveAt(position); return true; } - - public T? Get(int position) + public T? this[int position] { - - if (position < 0 || position > Count) + get { - return null; + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + } + set + { + if (position < 0 || position > Count || Count >= _maxCount) + { + return; + } + _places.Insert(position, value); + } + } + + public IEnumerable GetTruck(int? maxTruck = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTruck.HasValue && i == maxTruck.Value) + { + yield break; + } } - return _places[position]; } } } diff --git a/GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericCollection.cs b/GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericCollection.cs index bdb7034..f72cdb9 100644 --- a/GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericCollection.cs +++ b/GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericCollection.cs @@ -21,6 +21,7 @@ namespace ProjectGasolineTanker.Generic private readonly SetGeneric _collection; + public TruckGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -40,24 +41,18 @@ namespace ProjectGasolineTanker.Generic } public static bool operator -(TruckGenericCollection collect, int pos) { - if (collect._collection.Get(pos) == null) + if (collect._collection.GetTruck(pos) == null) { return false; } return collect?._collection.Remove(pos) ?? false; } - public int ReturnLength() - { - return _collection.Count; - } - public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } - public Bitmap ShowTruck() { Bitmap bmp = new(_pictureWidth, _pictureHeight); @@ -67,7 +62,6 @@ namespace ProjectGasolineTanker.Generic return bmp; } - private void DrawBackground(Graphics gr) { Pen pen = new(Color.Black, 3); @@ -75,44 +69,31 @@ namespace ProjectGasolineTanker.Generic { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { + // линия разметки gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } } + private void DrawObjects(Graphics g) { - int x = 0; - int y = 0; - int index = -1; + int y = _pictureHeight / _placeSizeHeight - 1; - for (int i = 0; i <= _collection.Count; ++i) + foreach (var truck in _collection.GetTruck()) { - - DrawingTruck _truck = _collection.Get(i); - - x = 0; - y = 0; - if (_truck != null) + if (truck != null) { - - index = i; - while (index - _pictureWidth / _placeSizeWidth >= 0) - { - y++; - index -= _pictureWidth / _placeSizeWidth; - } - - if (index > 0) + if (x > _pictureWidth / _placeSizeWidth - 1) { - x += index; + x = 0; + --y; } - - x = _pictureWidth / _placeSizeWidth - 1 - x; - _truck.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); - _truck.DrawTransport(g); + truck.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); + truck.DrawTransport(g); + ++x; } } } diff --git a/GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericStorage.cs b/GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericStorage.cs new file mode 100644 index 0000000..26f5f06 --- /dev/null +++ b/GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericStorage.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectGasolineTanker.Drawings; +using ProjectGasolineTanker.MovementStratg; + +namespace ProjectGasolineTanker.Generic +{ + internal class TruckGenericStorage + { + readonly Dictionary> _TruckStorages; + public List Keys => _TruckStorages.Keys.ToList(); + //Ширина окна отрисовки + private readonly int _pictureWidth; + //Высота окна отрисовки + private readonly int _pictureHeight; + + public TruckGenericStorage(int pictureWidth, int pictureHeight) + { + _TruckStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(string name) + { + + foreach (string nameStorage in Keys) + { + if (nameStorage == name) + { + MessageBox.Show("Набор с заданным именем уже занят", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + _TruckStorages.Add(name, new TruckGenericCollection(_pictureWidth, _pictureHeight)); + } + + public void DelSet(string name) + { + + if (_TruckStorages.ContainsKey(name)) + { + _TruckStorages.Remove(name); + } + + } + + public TruckGenericCollection? this[string ind] + { + get + { + if (_TruckStorages.ContainsKey(ind)) + { + return _TruckStorages[ind]; + } + return null; + } + } + } +} diff --git a/GasolineTanker/ProjectGasolineTanker/MovementStratg/AbstractStrategy.cs b/GasolineTanker/ProjectGasolineTanker/MovementStratg/AbstractStrategy.cs index 0051dca..c0b8d05 100644 --- a/GasolineTanker/ProjectGasolineTanker/MovementStratg/AbstractStrategy.cs +++ b/GasolineTanker/ProjectGasolineTanker/MovementStratg/AbstractStrategy.cs @@ -9,7 +9,6 @@ using ProjectGasolineTanker.Entities; namespace ProjectGasolineTanker.MovementStratg { - public abstract class AbstractStrategy { private IMoveableObject? _moveableObject; @@ -54,9 +53,9 @@ namespace ProjectGasolineTanker.MovementStratg protected bool MoveRight() => MoveTo(DirectionType.Right); protected bool MoveUp() => MoveTo(DirectionType.Up); - + protected bool MoveDown() => MoveTo(DirectionType.Down); - + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; protected int? GetStep() @@ -71,7 +70,7 @@ namespace ProjectGasolineTanker.MovementStratg protected abstract void MoveToTarget(); protected abstract bool IsTargetDestinaion(); - + private bool MoveTo(DirectionType directionType) { if (_state != Status.InProgress) diff --git a/GasolineTanker/ProjectGasolineTanker/MovementStratg/IMoveableObject.cs b/GasolineTanker/ProjectGasolineTanker/MovementStratg/IMoveableObject.cs index 220e961..e4cb7be 100644 --- a/GasolineTanker/ProjectGasolineTanker/MovementStratg/IMoveableObject.cs +++ b/GasolineTanker/ProjectGasolineTanker/MovementStratg/IMoveableObject.cs @@ -12,15 +12,11 @@ namespace ProjectGasolineTanker.MovementStratg public interface IMoveableObject { - ObjectParameters? GetObjectPosition { get; } int GetStep { get; } bool CheckCanMove(DirectionType direction); - void MoveObject(DirectionType direction); - - } } diff --git a/GasolineTanker/ProjectGasolineTanker/MovementStratg/MoveToCenter.cs b/GasolineTanker/ProjectGasolineTanker/MovementStratg/MoveToCenter.cs index 2038b2a..082acc4 100644 --- a/GasolineTanker/ProjectGasolineTanker/MovementStratg/MoveToCenter.cs +++ b/GasolineTanker/ProjectGasolineTanker/MovementStratg/MoveToCenter.cs @@ -6,7 +6,9 @@ using System.Threading.Tasks; namespace ProjectGasolineTanker.MovementStratg { - + /// + /// Стратегия перемещения объекта в центр экрана + /// public class MoveToCenter : AbstractStrategy { protected override bool IsTargetDestinaion() diff --git a/GasolineTanker/ProjectGasolineTanker/MovementStratg/ObjectParameters.cs b/GasolineTanker/ProjectGasolineTanker/MovementStratg/ObjectParameters.cs index 7e444ba..7504755 100644 --- a/GasolineTanker/ProjectGasolineTanker/MovementStratg/ObjectParameters.cs +++ b/GasolineTanker/ProjectGasolineTanker/MovementStratg/ObjectParameters.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; namespace ProjectGasolineTanker.MovementStratg { + public class ObjectParameters { private readonly int _x;