Готовая 4 лабораторная

This commit is contained in:
russell 2023-11-04 16:38:42 +04:00
parent f92019e9c6
commit 380658dcf9
6 changed files with 417 additions and 167 deletions

View File

@ -70,14 +70,14 @@ namespace DumpTruck
ColorDialog secondDialog = new();
if (secondDialog.ShowDialog() == DialogResult.OK)
{
dumpBoxColor = secondDialog.Color;
dumpBoxColor = secondDialog.Color;
}
Color tentColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog thirdDialog = new();
if (thirdDialog.ShowDialog() == DialogResult.OK)
{
tentColor = thirdDialog.Color;
tentColor = thirdDialog.Color;
}
_drawingTruck = new DrawingDumpTruck(random.Next(100, 300), random.Next(1000, 3000),

View File

@ -1,41 +1,48 @@
namespace DumpTruck
{
partial class FormTruckCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
partial class FormTruckCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panelTools = new System.Windows.Forms.Panel();
this.panelStorages = new System.Windows.Forms.Panel();
this.labelStorages = new System.Windows.Forms.Label();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.buttonDelObject = new System.Windows.Forms.Button();
this.buttonAddObject = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.labelTools = new System.Windows.Forms.Label();
this.buttonRemoveTruck = new System.Windows.Forms.Button();
this.buttonAddTruck = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.panelTools.SuspendLayout();
this.panelStorages.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout();
//
@ -43,6 +50,7 @@
//
this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.panelTools.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panelTools.Controls.Add(this.panelStorages);
this.panelTools.Controls.Add(this.maskedTextBoxNumber);
this.panelTools.Controls.Add(this.buttonRefreshCollection);
this.panelTools.Controls.Add(this.labelTools);
@ -53,9 +61,74 @@
this.panelTools.Size = new System.Drawing.Size(203, 445);
this.panelTools.TabIndex = 0;
//
// panelStorages
//
this.panelStorages.Controls.Add(this.labelStorages);
this.panelStorages.Controls.Add(this.textBoxStorageName);
this.panelStorages.Controls.Add(this.buttonDelObject);
this.panelStorages.Controls.Add(this.buttonAddObject);
this.panelStorages.Controls.Add(this.listBoxStorages);
this.panelStorages.Location = new System.Drawing.Point(3, 18);
this.panelStorages.Name = "panelStorages";
this.panelStorages.Size = new System.Drawing.Size(200, 221);
this.panelStorages.TabIndex = 5;
//
// labelStorages
//
this.labelStorages.AutoSize = true;
this.labelStorages.Location = new System.Drawing.Point(25, 0);
this.labelStorages.Name = "labelStorages";
this.labelStorages.Size = new System.Drawing.Size(52, 15);
this.labelStorages.TabIndex = 4;
this.labelStorages.Text = "Наборы";
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(25, 26);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(150, 23);
this.textBoxStorageName.TabIndex = 3;
//
// buttonDelObject
//
this.buttonDelObject.Location = new System.Drawing.Point(25, 185);
this.buttonDelObject.Name = "buttonDelObject";
this.buttonDelObject.Size = new System.Drawing.Size(150, 23);
this.buttonDelObject.TabIndex = 2;
this.buttonDelObject.Text = "Удалить набор";
this.buttonDelObject.UseVisualStyleBackColor = true;
this.buttonDelObject.Click += new System.EventHandler(this.buttonDelObject_Click);
//
// buttonAddObject
//
this.buttonAddObject.Location = new System.Drawing.Point(25, 56);
this.buttonAddObject.Name = "buttonAddObject";
this.buttonAddObject.Size = new System.Drawing.Size(150, 23);
this.buttonAddObject.TabIndex = 1;
this.buttonAddObject.Text = "Добавить набор";
this.buttonAddObject.UseVisualStyleBackColor = true;
this.buttonAddObject.Click += new System.EventHandler(this.buttonAddObject_Click);
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 15;
this.listBoxStorages.Location = new System.Drawing.Point(25, 85);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(150, 94);
this.listBoxStorages.TabIndex = 0;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 302);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23);
this.maskedTextBoxNumber.TabIndex = 4;
//
// buttonRefreshCollection
//
this.buttonRefreshCollection.Location = new System.Drawing.Point(25, 168);
this.buttonRefreshCollection.Location = new System.Drawing.Point(28, 395);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(150, 30);
this.buttonRefreshCollection.TabIndex = 3;
@ -75,7 +148,7 @@
//
// buttonRemoveTruck
//
this.buttonRemoveTruck.Location = new System.Drawing.Point(25, 109);
this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 336);
this.buttonRemoveTruck.Name = "buttonRemoveTruck";
this.buttonRemoveTruck.Size = new System.Drawing.Size(150, 30);
this.buttonRemoveTruck.TabIndex = 2;
@ -85,7 +158,7 @@
//
// buttonAddTruck
//
this.buttonAddTruck.Location = new System.Drawing.Point(25, 30);
this.buttonAddTruck.Location = new System.Drawing.Point(28, 257);
this.buttonAddTruck.Name = "buttonAddTruck";
this.buttonAddTruck.Size = new System.Drawing.Size(150, 30);
this.buttonAddTruck.TabIndex = 1;
@ -101,13 +174,6 @@
this.pictureBoxCollection.TabIndex = 1;
this.pictureBoxCollection.TabStop = false;
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(25, 75);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23);
this.maskedTextBoxNumber.TabIndex = 4;
//
// FormTruckCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -119,19 +185,27 @@
this.Text = "FormTruckCollection";
this.panelTools.ResumeLayout(false);
this.panelTools.PerformLayout();
this.panelStorages.ResumeLayout(false);
this.panelStorages.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false);
}
}
#endregion
#endregion
private Panel panelTools;
private Button buttonAddTruck;
private Label labelTools;
private Button buttonRefreshCollection;
private Button buttonRemoveTruck;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
private Panel panelTools;
private Button buttonAddTruck;
private Label labelTools;
private Button buttonRefreshCollection;
private Button buttonRemoveTruck;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
private Panel panelStorages;
private Button buttonDelObject;
private Button buttonAddObject;
private ListBox listBoxStorages;
private Label labelStorages;
private TextBox textBoxStorageName;
}
}

View File

@ -13,65 +13,154 @@ using DumpTruck.MovementStrategy;
namespace DumpTruck
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingTruck
/// </summary>
public partial class FormTruckCollection : Form
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingTruck
/// </summary>
public partial class FormTruckCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly TrucksGenericCollection<DrawingTruck, DrawningObjectTruck> _trucks;
private readonly TrucksGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormTruckCollection()
{
InitializeComponent();
_trucks = new TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>(pictureBoxCollection.Width, pictureBoxCollection.Height);
{
InitializeComponent();
_storage = new TrucksGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void buttonAddTruck_Click(object sender, EventArgs e)
private void ReloadObjects()
{
FormDumpTruck form = new();
if (form.ShowDialog() == DialogResult.OK)
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
if (_trucks + form.SelectedTruck != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _trucks.ShowTrucks();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
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 buttonRemoveTruck_Click(object sender, EventArgs e)
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddObject_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrucks();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_trucks - pos)
if (MessageBox.Show($"Удалить объект { listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _trucks.ShowTrucks();
}
else
{
MessageBox.Show("Не удалось удалить объект");
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
private void buttonAddTruck_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormDumpTruck form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (obj + form.SelectedTruck != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowTrucks();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
pictureBoxCollection.Image = _trucks.ShowTrucks();
}
}
private void buttonRemoveTruck_Click(object sender, EventArgs e)
{
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 = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowTrucks();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowTrucks();
}
}
}

View File

@ -16,25 +16,30 @@ namespace DumpTruck.Generics
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="truck">Добавляемый грузовик</param>
/// <returns></returns>
public int Insert(T truck)
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="truck">Добавляемый грузовик</param>
/// <returns></returns>
public int Insert(T truck)
{
return Insert(truck, 0);
}
@ -46,25 +51,8 @@ namespace DumpTruck.Generics
/// <returns></returns>
public int Insert(T truck, int position)
{
if (position < 0 || position >= Count) return -1;
int index = -1;
for (int i = position; i < Count; i++)
{
if (_places[i] == null)
{
index = i;
break;
}
}
if (index < 0) return -1;
for (int i = index; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = truck;
if (position < 0 || position > Count || Count >= _maxCount) return -1;
_places.Insert(position, truck);
return position;
}
/// <summary>
@ -75,18 +63,44 @@ namespace DumpTruck.Generics
public bool Remove(int position)
{
if (position < 0 || position >= Count) return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
get
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
set
{
if (position < 0 || position > Count || Count >= _maxCount) return;
_places.Insert(position, value);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetTrucks(int? maxTrucks = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTrucks.HasValue && i == maxTrucks.Value)
{
yield break;
}
}
}
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.DrawingObjects;
@ -65,35 +64,35 @@ namespace DumpTruck.Generics
}
return collect._collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(TrucksGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
return collect._collection.Remove(pos);
}
return false;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowTrucks()
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static T? operator -(TrucksGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowTrucks()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
@ -127,15 +126,15 @@ namespace DumpTruck.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int index = 0;
foreach (var truck in _collection.GetTrucks())
{
T? obj = _collection.Get(i);
if (obj != null)
if (truck != null)
{
obj.SetPosition(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, i / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
obj.DrawTransport(g);
truck.SetPosition(index % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, index / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
truck.DrawTransport(g);
}
index++;
}
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.DrawingObjects;
using DumpTruck.MovementStrategy;
namespace DumpTruck.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class TrucksGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>> _truckStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _truckStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public TrucksGenericStorage(int pictureWidth, int pictureHeight)
{
_truckStorages = new Dictionary<string, TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_truckStorages.ContainsKey(name)) _truckStorages.Add(name, new TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_truckStorages.ContainsKey(name)) _truckStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>?
this[string ind]
{
get
{
if (_truckStorages.ContainsKey(ind)) return _truckStorages[ind];
return null;
}
}
}
}