ISEbd-21. Pyatkin I.A. Lab work 04. #4

Closed
IvanPyatkin wants to merge 1 commits from lab04 into lab03
5 changed files with 349 additions and 70 deletions

View File

@ -56,30 +56,31 @@ namespace ProjectExcavator.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(ExcavatorGenericCollection<T, U> collect, T? obj)
public static bool operator +(ExcavatorGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect?._collection.Insert(obj) ?? -1;
return collect?._collection.Insert(obj) ?? false;
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(ExcavatorGenericCollection<T, U> collect, int pos)
public static T? operator -(ExcavatorGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
return true;
}
return false;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
@ -87,7 +88,8 @@ namespace ProjectExcavator.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -110,7 +112,8 @@ namespace ProjectExcavator.Generics
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{//линия разметки места
g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j *
@ -130,7 +133,7 @@ namespace ProjectExcavator.Generics
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
DrawingExcavator? excavator = _collection.Get(i);
T? excavator = _collection[i];
if (excavator == null)
continue;
int r = i / width;

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectExcavator.DrawingObjects;
using ProjectExcavator.MovementStrategy;
namespace ProjectExcavator.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class ExcavatorGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, ExcavatorGenericCollection<DrawingExcavator,
DrawingObjectExcavator>> _excavatorStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _excavatorStorages.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 ExcavatorGenericStorage(int pictureWidth, int pictureHeight)
{
_excavatorStorages = new Dictionary<string,
ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_excavatorStorages.ContainsKey(name))
{
ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator> newSet = new ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>(_pictureWidth, _pictureHeight);
_excavatorStorages.Add(name, newSet);
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
{
if (_excavatorStorages.ContainsKey(name))
{
_excavatorStorages.Remove(name);
}
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>? this[string ind]
{
get
{
if (_excavatorStorages.ContainsKey(ind))
{
return _excavatorStorages[ind];
}
return null;
}
}
}
}

View File

@ -34,8 +34,14 @@
this.buttonRemoveEx = new System.Windows.Forms.Button();
this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.buttonDelObject = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonAddObject = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxCollection
@ -49,14 +55,14 @@
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(26, 125);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(35, 339);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 23);
this.maskedTextBoxNumber.TabIndex = 1;
//
// buttonAddEx
//
this.buttonAddEx.Location = new System.Drawing.Point(6, 57);
this.buttonAddEx.Location = new System.Drawing.Point(13, 310);
this.buttonAddEx.Name = "buttonAddEx";
this.buttonAddEx.Size = new System.Drawing.Size(150, 23);
this.buttonAddEx.TabIndex = 2;
@ -66,7 +72,7 @@
//
// buttonRemoveEx
//
this.buttonRemoveEx.Location = new System.Drawing.Point(6, 166);
this.buttonRemoveEx.Location = new System.Drawing.Point(13, 368);
this.buttonRemoveEx.Name = "buttonRemoveEx";
this.buttonRemoveEx.Size = new System.Drawing.Size(150, 23);
this.buttonRemoveEx.TabIndex = 3;
@ -76,7 +82,7 @@
//
// buttonRefreshCollection
//
this.buttonRefreshCollection.Location = new System.Drawing.Point(6, 230);
this.buttonRefreshCollection.Location = new System.Drawing.Point(13, 432);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(148, 23);
this.buttonRefreshCollection.TabIndex = 4;
@ -86,17 +92,68 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.buttonAddEx);
this.groupBox1.Controls.Add(this.buttonRefreshCollection);
this.groupBox1.Controls.Add(this.maskedTextBoxNumber);
this.groupBox1.Controls.Add(this.buttonRemoveEx);
this.groupBox1.Location = new System.Drawing.Point(749, 0);
this.groupBox1.Location = new System.Drawing.Point(741, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(160, 461);
this.groupBox1.Size = new System.Drawing.Size(168, 461);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.textBoxStorageName);
this.groupBox2.Controls.Add(this.buttonDelObject);
this.groupBox2.Controls.Add(this.listBoxStorages);
this.groupBox2.Controls.Add(this.buttonAddObject);
this.groupBox2.Location = new System.Drawing.Point(13, 28);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(143, 214);
this.groupBox2.TabIndex = 5;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Наборы";
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(15, 30);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(122, 23);
this.textBoxStorageName.TabIndex = 6;
//
// buttonDelObject
//
this.buttonDelObject.Location = new System.Drawing.Point(15, 185);
this.buttonDelObject.Name = "buttonDelObject";
this.buttonDelObject.Size = new System.Drawing.Size(122, 23);
this.buttonDelObject.TabIndex = 8;
this.buttonDelObject.Text = "Удалить набор";
this.buttonDelObject.UseVisualStyleBackColor = true;
this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 15;
this.listBoxStorages.Location = new System.Drawing.Point(15, 88);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(122, 79);
this.listBoxStorages.TabIndex = 6;
this.listBoxStorages.Click += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
//
// buttonAddObject
//
this.buttonAddObject.Location = new System.Drawing.Point(15, 59);
this.buttonAddObject.Name = "buttonAddObject";
this.buttonAddObject.Size = new System.Drawing.Size(122, 23);
this.buttonAddObject.TabIndex = 7;
this.buttonAddObject.Text = "Добавить набор";
this.buttonAddObject.UseVisualStyleBackColor = true;
this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// FormExcavatorCollection
//
this.ClientSize = new System.Drawing.Size(909, 461);
@ -106,6 +163,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
}
@ -118,5 +177,10 @@
private Button buttonRemoveEx;
private Button buttonRefreshCollection;
private GroupBox groupBox1;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private Button buttonDelObject;
private ListBox listBoxStorages;
private Button buttonAddObject;
}
}

View File

@ -5,23 +5,95 @@ using ProjectExcavator.MovementStrategy;
namespace ProjectExcavator
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingExcavator
/// Форма для работы с набором объектов класса DrawningExcavator
/// </summary>
public partial class FormExcavatorCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly ExcavatorGenericCollection<DrawingExcavator,
DrawingObjectExcavator> _excavator;
private readonly ExcavatorGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormExcavatorCollection()
{
InitializeComponent();
_excavator = new ExcavatorGenericCollection<DrawingExcavator,
DrawingObjectExcavator>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new ExcavatorGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
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;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddObject_Click(object sender, EventArgs e)
{
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]?.ShowExcavator();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_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();
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowExcavator();
}
}
/// <summary>
/// Добавление объекта в набор
@ -30,14 +102,23 @@ namespace ProjectExcavator
/// <param name="e"></param>
private void ButtonAddEx_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormExcavator form = new();
if (form.ShowDialog() == DialogResult.OK)
{
int result = _excavator + form.SelectedExcavator;
if (result != -2)
if (obj + form.SelectedExcavator)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _excavator.ShowExcavator();
pictureBoxCollection.Image = obj.ShowExcavator();
}
else
{
@ -52,16 +133,26 @@ namespace ProjectExcavator
/// <param name="e"></param>
private void ButtonRemoveEx_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 (_excavator - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _excavator.ShowExcavator();
pictureBoxCollection.Image = obj.ShowExcavator();
}
else
{
@ -75,7 +166,17 @@ namespace ProjectExcavator
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _excavator.ShowExcavator();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowExcavator();
}
}
}

View File

@ -14,34 +14,40 @@ namespace ProjectExcavator.Generics
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
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="excavator">Добавляемый экскаватор</param>
/// <returns></returns>
public int Insert(T excavator)
public bool Insert(T excavator)
{
for (int i = _places.Length - 1; i > 0; i--)
// TODO вставка в начало набора
if (_places.Count >= _maxCount)
{
_places[i] = _places[i - 1];
return false;
}
_places[0] = excavator;
return Insert(excavator, 0);
_places.Insert(0, excavator);
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -49,39 +55,21 @@ namespace ProjectExcavator.Generics
/// <param name="excavator">Добавляемый экскаватор</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T excavator, int position)
public bool Insert(T excavator, int position)
{
if (position < 0 || position >= _places.Length)
// TODO проверка позиции
if (position < 0 || position >= _places.Count)
{
return -1;
return false;
}
if (_places[position] != null)
// TODO проверка, что есть место для вставки
if (_places.Count >= _maxCount)
{
return -1;
return false;
}
int NullIndex = 0;
for (int i = position; i < _places.Length; i++)
{
if (_places[i] == null)
{
NullIndex = i;
break;
}
}
if (NullIndex == 0)
{
return -1;
}
for (int i = NullIndex; i >= position; i--)
{
_places[i + 1] = _places[i];
}
_places[position] = excavator;
return position;
// TODO вставка по позиции
_places.Insert(position, excavator);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -90,11 +78,13 @@ namespace ProjectExcavator.Generics
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= _places.Length)
// TODO проверка позиции
if (position < 0 || position >= _places.Count)
{
return false;
}
_places[position] = null;
// TODO удаление объекта из списка
_places.RemoveAt(position);
return true;
}
/// <summary>
@ -102,13 +92,47 @@ namespace ProjectExcavator.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= _places.Length)
get
{
return null;
// TODO проверка позиции
if (position < 0 || position >= _places.Count)
{
return null;
}
return _places[position];
}
set
{
// TODO проверка позиции
if (position < 0 || position >= _places.Count)
{
return;
}
// TODO проверка свободных мест в списке
if (_places.Count >= _maxCount)
{
return;
}
// TODO вставка в список по позиции
_places.Insert(position, value);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetExcavators(int? maxExcavators = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxExcavators.HasValue && i == maxExcavators.Value)
{
yield break;
}
}
return _places[position];
}
}
}