ISEbd-21. Pyatkin I.A. Lab work 04_hard. #11

Closed
IvanPyatkin wants to merge 1 commits from lab04hard into lab03hard
7 changed files with 404 additions and 81 deletions

View File

@ -12,7 +12,7 @@ namespace ProjectExcavator.MovementStrategy
/// </summary>
public class DrawingObjectExcavator : IMoveableObject
{
private readonly DrawingExcavator? _drawingExcavator = null;
public DrawingExcavator _drawingExcavator { get; private set; }
public DrawingObjectExcavator(DrawingExcavator drawingExcavator)
{
_drawingExcavator = drawingExcavator;

View File

@ -56,32 +56,33 @@ namespace ProjectExcavator.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(ExcavatorGenericCollection<T, U> collect, T?
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>
@ -89,7 +90,8 @@ namespace ProjectExcavator.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -133,12 +135,15 @@ namespace ProjectExcavator.Generics
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
DrawingExcavator? excavator = _collection.Get(i);
// TODO получение объекта
T? excavator = _collection[i];
if (excavator == null)
continue;
int r = i / width;
int s = width - 1 - (i % width);
// TODO установка позиции
excavator.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight);
// TODO прорисовка объекта
excavator.DrawTransport(g);
}
}

View File

@ -0,0 +1,83 @@
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
{
internal class ExcavatorGenericStorage
{
readonly Dictionary<string, ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>> _excavatorStorages;
public List<string> Keys => _excavatorStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public ExcavatorGenericStorage(int pictureWidth, int pictureHeight)
{
_excavatorStorages = new Dictionary<string, ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_removedObjects = new LinkedList<DrawingObjectExcavator>();
}
public void AddSet(string name)
{
if (!_excavatorStorages.ContainsKey(name))
{
ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator> newSet = new ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>(_pictureWidth, _pictureHeight);
_excavatorStorages.Add(name, newSet);
}
}
public void DelSet(string name)
{
if (_excavatorStorages.ContainsKey(name))
{
_excavatorStorages.Remove(name);
}
}
public ExcavatorGenericCollection<DrawingExcavator, DrawingObjectExcavator>? this[string ind]
{
get
{
if (_excavatorStorages.ContainsKey(ind))
{
return _excavatorStorages[ind];
}
return null;
}
}
public DrawingObjectExcavator? this[string dictIndex, int objIndex]
{
get
{
if (_excavatorStorages.ContainsKey(dictIndex))
{
var selectedDictElement = _excavatorStorages[dictIndex];
var selectedObject = selectedDictElement.GetU(objIndex);
return selectedObject;
}
return null;
}
}
private LinkedList<DrawingObjectExcavator> _removedObjects;
public DrawingObjectExcavator RemovedObject
{
set
{
_removedObjects.AddLast(value);
}
get
{
if (_removedObjects == null || _removedObjects.Count == 0)
{
return null;
}
var removedObject = _removedObjects.Last();
_removedObjects.RemoveLast();
return removedObject;
}
}
}
}

View File

@ -15,12 +15,13 @@ namespace ProjectExcavator
public FormExcavator()
{
InitializeComponent();
comboBoxStrategy.Items.Add("Öåíòð");
comboBoxStrategy.Items.Add("Ãðàíèöà");
}
private void Draw()
public FormExcavator(DrawingExcavator drawingObject)
{
_drawingExcavator = drawingObject;
InitializeComponent();
}
public void Draw()
{
if (_drawingExcavator == null)
{

View File

@ -34,9 +34,16 @@
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();
this.buttonGeneration = new System.Windows.Forms.Button();
this.buttonRemoveobj = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxCollection
@ -50,14 +57,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;
@ -67,36 +74,89 @@
//
// 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;
this.buttonRemoveEx.Text = "Удалить экскаватор";
this.buttonRemoveEx.UseVisualStyleBackColor = true;
this.buttonRemoveEx.Click += new System.EventHandler(this.ButtonRemoveEx_Click);
//
// 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;
this.buttonRefreshCollection.Text = "Обновить коллекцию";
this.buttonRefreshCollection.UseVisualStyleBackColor = true;
this.buttonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonGeneration);
this.groupBox1.Controls.Add(this.buttonRemoveobj);
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);
//
// buttonGeneration
//
this.buttonGeneration.Location = new System.Drawing.Point(6, 332);
@ -107,6 +167,16 @@
this.buttonGeneration.UseVisualStyleBackColor = true;
this.buttonGeneration.Click += new System.EventHandler(this.buttonGeneration_Click);
//
// buttonRemoveobj
//
this.buttonRemoveobj.Location = new System.Drawing.Point(16, 265);
this.buttonRemoveobj.Name = "buttonRemoveobj";
this.buttonRemoveobj.Size = new System.Drawing.Size(145, 23);
this.buttonRemoveobj.TabIndex = 6;
this.buttonRemoveobj.Text = "Удалённые обьекты";
this.buttonRemoveobj.UseVisualStyleBackColor = true;
this.buttonRemoveobj.Click += new System.EventHandler(this.buttonRemoveobj_Click);
//
// FormExcavatorCollection
//
this.ClientSize = new System.Drawing.Size(909, 461);
@ -116,6 +186,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
}
@ -128,6 +200,12 @@
private Button buttonRemoveEx;
private Button buttonRefreshCollection;
private GroupBox groupBox1;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private Button buttonDelObject;
private ListBox listBoxStorages;
private Button buttonAddObject;
private Button buttonGeneration;
private Button buttonRemoveobj;
}
}

View File

@ -12,16 +12,105 @@ namespace ProjectExcavator
/// <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();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowExcavator();
}
/// <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();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
MessageBox.Show("Набор удален");
pictureBoxCollection.Image = obj.ShowExcavator();
}
}
/// <summary>
/// Добавление объекта в набор
@ -30,14 +119,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 +150,28 @@ 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)
var removableObject = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty, pos];
if (_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _excavator.ShowExcavator();
_storage.RemovedObject = (DrawingObjectExcavator)removableObject;
pictureBoxCollection.Image = obj.ShowExcavator();
}
else
{
@ -75,7 +185,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();
}
private void buttonGeneration_Click(object sender, EventArgs e)
@ -83,5 +203,18 @@ namespace ProjectExcavator
RandGeneration form = new();
form.ShowDialog();
}
private void buttonRemoveobj_Click(object sender, EventArgs e)
{
DrawingObjectExcavator removedObject = _storage.RemovedObject;
if (removedObject == null)
{
MessageBox.Show("Не удалось показать удаленные объекты");
return;
}
FormExcavator formRemovedObject = new FormExcavator(removedObject._drawingExcavator);
formRemovedObject.Show();
formRemovedObject.Draw();
}
}
}

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,66 +55,83 @@ 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>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
public T Remove(int position)
{
if (position < 0 || position >= _places.Length)
if (position > Count || _places[position] == null)
{
return false;
return null;
}
_places[position] = null;
return true;
var result = _places[position];
_places.RemoveAt(position);
return result;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
get
{
// TODO проверка позиции
if (position < 0 || position >= _places.Count)
{
if (position < 0 || position >= _places.Length)
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;
}
}
}
}
}