PIbd-23. Ivanov V.N. Lab Work 04 #4

Closed
Vyacheslav wants to merge 1 commits from LabWork4 into LabWork3
5 changed files with 386 additions and 102 deletions
Showing only changes of commit 2dbfb4694f - Show all commits

View File

@ -28,32 +28,98 @@
/// </summary>
private void InitializeComponent()
{
toolsLabel = new Panel();
toolsPanel = new Panel();
panelSets = new Panel();
textBoxStorageName = new TextBox();
listBoxObjects = new ListBox();
ButtonAddObject = new Button();
ButtonDelObject = new Button();
SetsLabel = new Label();
ButtonRefreshCollection = new Button();
ButtonDeleteBus = new Button();
maskedTextBoxNumber = new TextBox();
addBus = new Button();
label1 = new Label();
ButtonAddBus = new Button();
LabelTools = new Label();
pictureBoxCollection = new PictureBox();
toolsLabel.SuspendLayout();
toolsPanel.SuspendLayout();
panelSets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// toolsLabel
// toolsPanel
//
toolsLabel.Controls.Add(ButtonRefreshCollection);
toolsLabel.Controls.Add(ButtonDeleteBus);
toolsLabel.Controls.Add(maskedTextBoxNumber);
toolsLabel.Controls.Add(addBus);
toolsLabel.Controls.Add(label1);
toolsLabel.Location = new Point(678, -2);
toolsLabel.Name = "toolsLabel";
toolsLabel.Size = new Size(221, 451);
toolsLabel.TabIndex = 0;
toolsPanel.Controls.Add(panelSets);
toolsPanel.Controls.Add(ButtonRefreshCollection);
toolsPanel.Controls.Add(ButtonDeleteBus);
toolsPanel.Controls.Add(maskedTextBoxNumber);
toolsPanel.Controls.Add(ButtonAddBus);
toolsPanel.Controls.Add(LabelTools);
toolsPanel.Location = new Point(662, -2);
toolsPanel.Name = "toolsPanel";
toolsPanel.Size = new Size(221, 504);
toolsPanel.TabIndex = 0;
//
// panelSets
//
panelSets.Controls.Add(textBoxStorageName);
panelSets.Controls.Add(listBoxObjects);
panelSets.Controls.Add(ButtonAddObject);
panelSets.Controls.Add(ButtonDelObject);
panelSets.Controls.Add(SetsLabel);
panelSets.Location = new Point(8, 20);
panelSets.Name = "panelSets";
panelSets.Size = new Size(210, 208);
panelSets.TabIndex = 6;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(10, 27);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(190, 27);
textBoxStorageName.TabIndex = 12;
//
// listBoxObjects
//
listBoxObjects.FormattingEnabled = true;
listBoxObjects.ItemHeight = 20;
listBoxObjects.Location = new Point(10, 99);
listBoxObjects.Name = "listBoxObjects";
listBoxObjects.Size = new Size(190, 64);
listBoxObjects.TabIndex = 11;
listBoxObjects.SelectedIndexChanged += listBoxObjects_SelectedIndexChanged;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(10, 60);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(191, 33);
ButtonAddObject.TabIndex = 8;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(10, 169);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(192, 33);
ButtonDelObject.TabIndex = 7;
ButtonDelObject.Text = "Удалить набор\r\n";
ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// SetsLabel
//
SetsLabel.AutoSize = true;
SetsLabel.Location = new Point(3, 4);
SetsLabel.Name = "SetsLabel";
SetsLabel.Size = new Size(66, 20);
SetsLabel.TabIndex = 0;
SetsLabel.Text = "Наборы";
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(10, 194);
ButtonRefreshCollection.Location = new Point(8, 452);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(197, 41);
ButtonRefreshCollection.TabIndex = 4;
@ -63,7 +129,7 @@
//
// ButtonDeleteBus
//
ButtonDeleteBus.Location = new Point(10, 121);
ButtonDeleteBus.Location = new Point(8, 379);
ButtonDeleteBus.Name = "ButtonDeleteBus";
ButtonDeleteBus.Size = new Size(197, 41);
ButtonDeleteBus.TabIndex = 3;
@ -73,35 +139,35 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(50, 88);
maskedTextBoxNumber.Location = new Point(48, 346);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(125, 27);
maskedTextBoxNumber.TabIndex = 2;
//
// addBus
// ButtonAddBus
//
addBus.Location = new Point(10, 26);
addBus.Name = "addBus";
addBus.Size = new Size(197, 41);
addBus.TabIndex = 1;
addBus.Text = "Добавить автобус";
addBus.UseVisualStyleBackColor = true;
addBus.Click += ButtonAddBus_Click;
ButtonAddBus.Location = new Point(8, 284);
ButtonAddBus.Name = "ButtonAddBus";
ButtonAddBus.Size = new Size(197, 41);
ButtonAddBus.TabIndex = 1;
ButtonAddBus.Text = "Добавить автобус";
ButtonAddBus.UseVisualStyleBackColor = true;
ButtonAddBus.Click += ButtonAddBus_Click;
//
// label1
// LabelTools
//
label1.AutoSize = true;
label1.Location = new Point(5, 0);
label1.Name = "label1";
label1.Size = new Size(103, 20);
label1.TabIndex = 0;
label1.Text = "Инструменты";
LabelTools.AutoSize = true;
LabelTools.Location = new Point(5, 0);
LabelTools.Name = "LabelTools";
LabelTools.Size = new Size(103, 20);
LabelTools.TabIndex = 0;
LabelTools.Text = "Инструменты";
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(1, -2);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(676, 451);
pictureBoxCollection.Size = new Size(663, 504);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
@ -109,25 +175,33 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(900, 450);
ClientSize = new Size(882, 510);
Controls.Add(pictureBoxCollection);
Controls.Add(toolsLabel);
Controls.Add(toolsPanel);
Name = "FormBusCollection";
Text = "Набор автобусов";
toolsLabel.ResumeLayout(false);
toolsLabel.PerformLayout();
toolsPanel.ResumeLayout(false);
toolsPanel.PerformLayout();
panelSets.ResumeLayout(false);
panelSets.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
#endregion
private Panel toolsLabel;
private Label label1;
private Panel toolsPanel;
private Label LabelTools;
private Button ButtonDeleteBus;
private Button addBus;
private Button ButtonAddBus;
private Button ButtonRefreshCollection;
private PictureBox pictureBoxCollection;
public TextBox maskedTextBoxNumber;
private Panel panelSets;
private Label SetsLabel;
private Button ButtonDelObject;
private Button ButtonAddObject;
private ListBox listBoxObjects;
private TextBox textBoxStorageName;
}
}

View File

@ -21,7 +21,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
/// <summary>
/// Набор объектов
/// </summary>
private readonly TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _theBuses;
private readonly TheBusesGenericStorage _storage;
/// <summary>
/// Конструктор
@ -29,8 +29,77 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
public FormBusCollection()
{
InitializeComponent();
_theBuses = new TheBusesGenericCollection<DrawningBus, DrawningObjectBus>
(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new TheBusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxObjects.SelectedIndex;
listBoxObjects.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxObjects.Items.Add(_storage.Keys[i]);
}
if (listBoxObjects.Items.Count > 0 && (index == -1 || index >= listBoxObjects.Items.Count))
{
listBoxObjects.SelectedIndex = 0;
}
else if (listBoxObjects.Items.Count > 0 && index > -1 && index < listBoxObjects.Items.Count)
{
listBoxObjects.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[listBoxObjects.SelectedItem?.ToString() ?? string.Empty]?.ShowTheBuses();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxObjects.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект{listBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxObjects.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
/// <summary>
@ -40,19 +109,29 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
/// <param name="e"></param>
private void ButtonAddBus_Click(object sender, EventArgs e)
{
if (listBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormDoubleDeckerBus form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_theBuses + form.SelectedBus != -1)
if (obj + form.SelectedBus)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _theBuses.ShowTheBuses();
pictureBoxCollection.Image = obj.ShowTheBuses();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
@ -62,17 +141,26 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
/// <param name="e"></param>
private void ButtonDeleteBus_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (listBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxObjects.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 (_theBuses - pos != true)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _theBuses.ShowTheBuses();
pictureBoxCollection.Image = obj.ShowTheBuses();
}
else
{
@ -87,7 +175,17 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _theBuses.ShowTheBuses();
if (listBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxObjects.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowTheBuses();
}
}
}

View File

@ -14,14 +14,19 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в cписке
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в cписке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
@ -29,7 +34,8 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
@ -37,9 +43,15 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// </summary>
/// <param name="bus">Добавляемый автобус</param>
/// <returns></returns>
public int Insert(T bus)
public bool Insert(T bus)
{
return Insert(bus, 0);
if (_places.Count == _maxCount)
{
return false;
}
Insert(bus, 0);
return true;
}
/// <summary>
@ -48,36 +60,15 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="bus">Добавляемый автобус</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T bus, int position)
public bool Insert(T bus, int position)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
{
return -1;
return false;
}
for (i = position; i < Count; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
{
return -1;
}
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = bus;
return position;
_places.Insert(position, bus);
return true;
}
/// <summary>
@ -92,7 +83,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
return false;
}
_places[position] = null;
_places.RemoveAt(position);
return true;
}
@ -101,14 +92,43 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count)
get
{
return null;
}
if (position < 0 || position >= _maxCount)
{
return null;
}
return _places[position];
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetTheBuses(int? maxTheBuses = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTheBuses.HasValue && i == maxTheBuses.Value)
{
yield break;
}
}
}
}
}
}

View File

@ -63,13 +63,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(TheBusesGenericCollection<T, U> collect, T? obj)
public static bool operator +(TheBusesGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
return collect?._collection.Insert(obj) ?? false;
}
/// <summary>
@ -78,14 +79,15 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(TheBusesGenericCollection<T, U> collect, int pos)
public static T? operator -(TheBusesGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return false;
return obj;
}
/// <summary>
@ -95,7 +97,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
@ -136,17 +138,17 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var bus in _collection.GetTheBuses())
{
DrawningBus bus = _collection.Get(i);
if (bus != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight);
bus.DrawTransport(g);
}
i++;
}
}
}
}
}

View File

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