сделал лабу, готова для сдачи, спросить насчет изменения обратно на булевый инсерт

This commit is contained in:
1SooNoo1 2023-10-24 00:53:48 +04:00
parent 668275771f
commit bccce47300
5 changed files with 307 additions and 68 deletions

View File

@ -26,17 +26,17 @@ namespace ProjectExcavator.Generic
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
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;
}
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);
@ -44,7 +44,7 @@ namespace ProjectExcavator.Generic
return obj;
}
public U? GetU(int pos) => (U)_collection.Get(pos)?.GetMoveableObject;
public U? GetU(int pos) => (U?)_collection[pos]?.GetMoveableObject;
/// <summary>
/// Вывод всего набора объектов
/// </summary>
@ -72,22 +72,18 @@ namespace ProjectExcavator.Generic
}
private void DrawObjects(Graphics g)
{
for(int i = 0; i < _collection.Count;i++)
int i = 0;
foreach (var excavator in _collection.GetExcavators())
{
T t = _collection.Get(i);
if(t != null)
if (excavator != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t is DrawningExcavator)
{
//тут преведение избыточно
t.DrawTransport(g);
}
excavator.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (excavator is DrawningExcavator)
(excavator as DrawningExcavator).DrawTransport(g);
else
{
t.DrawTransport(g);
}
excavator.DrawTransport(g);
}
i++;
}
}
}

View File

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

View File

@ -33,6 +33,10 @@
maskedTextBoxNumber = new MaskedTextBox();
buttonRemoveExcavator = new Button();
buttonRefreshCollection = new Button();
listBoxStorages = new ListBox();
AddCollectButton = new Button();
DeleteCollectButton = new Button();
textBoxStorageName = new TextBox();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
@ -46,7 +50,7 @@
//
// buttonAddExcavator
//
buttonAddExcavator.Location = new Point(623, 26);
buttonAddExcavator.Location = new Point(623, 244);
buttonAddExcavator.Name = "buttonAddExcavator";
buttonAddExcavator.Size = new Size(174, 51);
buttonAddExcavator.TabIndex = 1;
@ -56,14 +60,14 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(623, 83);
maskedTextBoxNumber.Location = new Point(623, 301);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(174, 23);
maskedTextBoxNumber.TabIndex = 2;
//
// buttonRemoveExcavator
//
buttonRemoveExcavator.Location = new Point(623, 128);
buttonRemoveExcavator.Location = new Point(623, 330);
buttonRemoveExcavator.Name = "buttonRemoveExcavator";
buttonRemoveExcavator.Size = new Size(174, 38);
buttonRemoveExcavator.TabIndex = 3;
@ -73,19 +77,60 @@
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(625, 172);
buttonRefreshCollection.Location = new Point(623, 374);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(172, 33);
buttonRefreshCollection.Size = new Size(174, 33);
buttonRefreshCollection.TabIndex = 4;
buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += buttonRefreshCollection_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(623, 90);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(174, 79);
listBoxStorages.TabIndex = 5;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// AddCollectButton
//
AddCollectButton.Location = new Point(623, 53);
AddCollectButton.Name = "AddCollectButton";
AddCollectButton.Size = new Size(174, 31);
AddCollectButton.TabIndex = 6;
AddCollectButton.Text = "Добавить набор";
AddCollectButton.UseVisualStyleBackColor = true;
AddCollectButton.Click += ButtonAddObject_Click;
//
// DeleteCollectButton
//
DeleteCollectButton.Location = new Point(623, 175);
DeleteCollectButton.Name = "DeleteCollectButton";
DeleteCollectButton.Size = new Size(174, 28);
DeleteCollectButton.TabIndex = 7;
DeleteCollectButton.Text = "Удалить набор";
DeleteCollectButton.UseVisualStyleBackColor = true;
DeleteCollectButton.Click += ButtonDelObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(623, 24);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(174, 23);
textBoxStorageName.TabIndex = 8;
//
// FormExcavatorCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(textBoxStorageName);
Controls.Add(DeleteCollectButton);
Controls.Add(AddCollectButton);
Controls.Add(listBoxStorages);
Controls.Add(buttonRefreshCollection);
Controls.Add(buttonRemoveExcavator);
Controls.Add(maskedTextBoxNumber);
@ -105,5 +150,9 @@
private MaskedTextBox maskedTextBoxNumber;
private Button buttonRemoveExcavator;
private Button buttonRefreshCollection;
private ListBox listBoxStorages;
private Button AddCollectButton;
private Button DeleteCollectButton;
private TextBox textBoxStorageName;
}
}

View File

@ -16,49 +16,146 @@ namespace ProjectExcavator
public partial class FormExcavatorCollection : Form
{
private readonly ExcavatorGenericCollection<DrawningExcavator, DrawningObjectExcavator> _excavators;
/// <summary>
/// Набор объектов
/// </summary>
private readonly ExcavatorsGenericStorage _storage;
public FormExcavatorCollection()
{
InitializeComponent();
_excavators = new ExcavatorGenericCollection<DrawningExcavator, DrawningObjectExcavator>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new ExcavatorsGenericStorage(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;
}
}
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();
}
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();
}
}
private void buttonAddExcavator_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
ExcavatorForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_excavators + form.SelectedExcavator != -1)
if (obj + form.SelectedExcavator)
{
MessageBox.Show("Объект добален");
pictureBoxCollection.Image = _excavators.ShowExcavator();
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowExcavator();
}
else
{
MessageBox.Show("Не удалось добавить новый обьект");
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonRemoveExcavator_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 = Convert.ToInt32(maskedTextBoxNumber.Text);
if(_excavators - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _excavators.ShowExcavator();
pictureBoxCollection.Image = obj.ShowExcavator();
}
else
{
MessageBox.Show("Не удалось удалить обхект");
MessageBox.Show("Не удалось удалить объект");
}
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
private void buttonRefreshCollection_Click(object sender, EventArgs
e)
{
pictureBoxCollection.Image = _excavators.ShowExcavator();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowExcavator();
}
}
}

View File

@ -4,54 +4,70 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectExcavator
namespace ProjectExcavator.Generic
{
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T>(_maxCount);
}
public int Insert(T excavator)
public bool Insert(T excavator)
{
return Insert(excavator,0);
}
public int Insert(T excavator, int position)
{
if (position < 0 || position >= _places.Length)
return -1;
for (; position < _places.Length; position++)
if(_places.Count >= _maxCount)
{
if (_places[position] == null)
{
break;
}
}
if (position == _places.Length)
return -1;
for (; position > 0; position--)
{
_places[position] = _places[position - 1];
}
_places[position] = excavator;
return position;
}
public bool Remove(int position) {
if (position >= _places.Length || position<0)
return false;
_places[position] = null;
}
_places.Insert(0, excavator);
return true;
}
public T? Get(int position)
public bool Insert(T excavator, int position)
{
if(position >= _places.Length || position < 0)
if (position < 0 || position >= _maxCount)
return false;
if (Count >= _maxCount)
return false;
_places.Insert(0,excavator);
return true;
}
public bool Remove(int position) {
if (position > _places.Count || position<0)
return false;
if (position >= Count)
return false;
_places.RemoveAt(position);
return true;
}
public T? this[int position]
{
get
{
return null;
if (position < 0 || position > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetExcavators(int? maxCars = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value)
{
yield break;
}
}
return _places[position];
}
}
}