день рпп или завершение 4 лабы

This commit is contained in:
ALINA_KURBANOVA 2023-11-11 21:26:13 +04:00
parent 7beae4d50f
commit 57d38a4bbe
10 changed files with 436 additions and 177 deletions

View File

@ -2,12 +2,12 @@
namespace WarmlyLocomotive.MovementStrategy namespace WarmlyLocomotive.MovementStrategy
{ {
internal class DrawningObjectCar : IMoveableObject internal class DrawningObjectWarmlyLocomotive : IMoveableObject
{ {
private readonly DrawningWarmlyLocomotive? _drawningWarmlyLocomotive = null; private readonly DrawningWarmlyLocomotive? _drawningWarmlyLocomotive = null;
public DrawningObjectCar(DrawningWarmlyLocomotive drawningCar) public DrawningObjectWarmlyLocomotive(DrawningWarmlyLocomotive drawningWarmlylocomotive)
{ {
_drawningWarmlyLocomotive = drawningCar; _drawningWarmlyLocomotive = drawningWarmlylocomotive;
} }
public ObjectParameters? GetObjectPosition public ObjectParameters? GetObjectPosition
{ {

View File

@ -12,7 +12,7 @@ public class DrawningWarmlyLocomotive
/// Класс-сущность /// Класс-сущность
/// </summary> /// </summary>
public EntityWarmlyLocomotive? EntityWarmlyLocomotive { get; protected set; } public EntityWarmlyLocomotive? EntityWarmlyLocomotive { get; protected set; }
public IMoveableObject GetMoveableObject => new DrawningObjectCar(this); public IMoveableObject GetMoveableObject => new DrawningObjectWarmlyLocomotive(this);
/// <summary> /// <summary>
/// Ширина окна /// Ширина окна
/// </summary> /// </summary>
@ -22,19 +22,19 @@ public class DrawningWarmlyLocomotive
/// </summary> /// </summary>
private int _pictureHeight; private int _pictureHeight;
/// <summary> /// <summary>
/// Левая координата прорисовки локомотива /// Левая координата прорисовки тепловоза
/// </summary> /// </summary>
protected int _startPosX; protected int _startPosX;
/// <summary> /// <summary>
/// Верхняя кооридната прорисовки локомотива /// Верхняя кооридната прорисовки тепловоза
/// </summary> /// </summary>
protected int _startPosY; protected int _startPosY;
/// <summary> /// <summary>
/// Ширина прорисовки локомотива /// Ширина прорисовки тепловоза
/// </summary> /// </summary>
protected readonly int _WarmlyLocomotiveWidth = 200; protected readonly int _WarmlyLocomotiveWidth = 200;
/// <summary> /// <summary>
/// Высота прорисовки локомотива /// Высота прорисовки тепловоза
/// </summary> /// </summary>
protected readonly int _WarmlyLocomotiveHeight = 75; protected readonly int _WarmlyLocomotiveHeight = 75;
private readonly int _trumpetHeight = 25; private readonly int _trumpetHeight = 25;

View File

@ -1,7 +1,7 @@
namespace WarmlyLocomotive.Entities namespace WarmlyLocomotive.Entities
{ {
/// <summary> /// <summary>
/// Класс-сущность "Автомобиль" /// Класс-сущность "Тепловоз"
/// </summary> /// </summary>
public class EntityWarmlyLocomotive public class EntityWarmlyLocomotive
{ {

View File

@ -30,12 +30,19 @@
{ {
labelCollection = new Label(); labelCollection = new Label();
panelCollectionWarmlyLocomotive = new Panel(); panelCollectionWarmlyLocomotive = new Panel();
labelGenericCollection = new Label();
panel1 = new Panel();
buttonDelObject = new Button();
textBoxStorageName = new TextBox();
listBoxStorages = new ListBox();
buttonAddObject = new Button();
buttonreFreshCollection = new Button(); buttonreFreshCollection = new Button();
textBoxCollectionWarmlyLocomotive = new TextBox(); maskedTextBoxNumber = new TextBox();
buttonRemove = new Button(); buttonRemove = new Button();
buttonadd = new Button(); buttonAdd = new Button();
pictureBoxCollectionWarmlyLocomotive = new PictureBox(); pictureBoxCollectionWarmlyLocomotive = new PictureBox();
panelCollectionWarmlyLocomotive.SuspendLayout(); panelCollectionWarmlyLocomotive.SuspendLayout();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollectionWarmlyLocomotive).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollectionWarmlyLocomotive).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -51,18 +58,77 @@
// //
// panelCollectionWarmlyLocomotive // panelCollectionWarmlyLocomotive
// //
panelCollectionWarmlyLocomotive.Controls.Add(labelGenericCollection);
panelCollectionWarmlyLocomotive.Controls.Add(panel1);
panelCollectionWarmlyLocomotive.Controls.Add(buttonreFreshCollection); panelCollectionWarmlyLocomotive.Controls.Add(buttonreFreshCollection);
panelCollectionWarmlyLocomotive.Controls.Add(textBoxCollectionWarmlyLocomotive); panelCollectionWarmlyLocomotive.Controls.Add(maskedTextBoxNumber);
panelCollectionWarmlyLocomotive.Controls.Add(buttonRemove); panelCollectionWarmlyLocomotive.Controls.Add(buttonRemove);
panelCollectionWarmlyLocomotive.Controls.Add(buttonadd); panelCollectionWarmlyLocomotive.Controls.Add(buttonAdd);
panelCollectionWarmlyLocomotive.Location = new Point(662, 28); panelCollectionWarmlyLocomotive.Location = new Point(662, 28);
panelCollectionWarmlyLocomotive.Name = "panelCollectionWarmlyLocomotive"; panelCollectionWarmlyLocomotive.Name = "panelCollectionWarmlyLocomotive";
panelCollectionWarmlyLocomotive.Size = new Size(149, 350); panelCollectionWarmlyLocomotive.Size = new Size(149, 410);
panelCollectionWarmlyLocomotive.TabIndex = 1; panelCollectionWarmlyLocomotive.TabIndex = 1;
// //
// labelGenericCollection
//
labelGenericCollection.AutoSize = true;
labelGenericCollection.Location = new Point(17, -1);
labelGenericCollection.Name = "labelGenericCollection";
labelGenericCollection.Size = new Size(52, 15);
labelGenericCollection.TabIndex = 5;
labelGenericCollection.Text = "Наборы";
//
// panel1
//
panel1.Controls.Add(buttonDelObject);
panel1.Controls.Add(textBoxStorageName);
panel1.Controls.Add(listBoxStorages);
panel1.Controls.Add(buttonAddObject);
panel1.Location = new Point(3, 16);
panel1.Name = "panel1";
panel1.Size = new Size(143, 209);
panel1.TabIndex = 4;
//
// buttonDelObject
//
buttonDelObject.Location = new Point(15, 175);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(115, 23);
buttonDelObject.TabIndex = 3;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += buttonDelObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(13, 13);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(117, 23);
textBoxStorageName.TabIndex = 2;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(14, 90);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(119, 79);
listBoxStorages.TabIndex = 1;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(15, 51);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(118, 22);
buttonAddObject.TabIndex = 0;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += buttonAddObject_Click;
//
// buttonreFreshCollection // buttonreFreshCollection
// //
buttonreFreshCollection.Location = new Point(3, 193); buttonreFreshCollection.Location = new Point(3, 325);
buttonreFreshCollection.Name = "buttonreFreshCollection"; buttonreFreshCollection.Name = "buttonreFreshCollection";
buttonreFreshCollection.Size = new Size(146, 23); buttonreFreshCollection.Size = new Size(146, 23);
buttonreFreshCollection.TabIndex = 3; buttonreFreshCollection.TabIndex = 3;
@ -70,38 +136,38 @@
buttonreFreshCollection.UseVisualStyleBackColor = true; buttonreFreshCollection.UseVisualStyleBackColor = true;
buttonreFreshCollection.Click += buttonreFreshCollection_Click; buttonreFreshCollection.Click += buttonreFreshCollection_Click;
// //
// textBoxCollectionWarmlyLocomotive // maskedTextBoxNumber
// //
textBoxCollectionWarmlyLocomotive.Location = new Point(17, 128); maskedTextBoxNumber.Location = new Point(16, 296);
textBoxCollectionWarmlyLocomotive.Name = "textBoxCollectionWarmlyLocomotive"; maskedTextBoxNumber.Name = "maskedTextBoxNumber";
textBoxCollectionWarmlyLocomotive.Size = new Size(117, 23); maskedTextBoxNumber.Size = new Size(117, 23);
textBoxCollectionWarmlyLocomotive.TabIndex = 2; maskedTextBoxNumber.TabIndex = 2;
// //
// buttonRemove // buttonRemove
// //
buttonRemove.Location = new Point(3, 71); buttonRemove.Location = new Point(6, 369);
buttonRemove.Name = "buttonRemove"; buttonRemove.Name = "buttonRemove";
buttonRemove.Size = new Size(131, 23); buttonRemove.Size = new Size(143, 23);
buttonRemove.TabIndex = 1; buttonRemove.TabIndex = 1;
buttonRemove.Text = "Удалить тепловоз"; buttonRemove.Text = "Удалить тепловоз";
buttonRemove.UseVisualStyleBackColor = true; buttonRemove.UseVisualStyleBackColor = true;
buttonRemove.Click += buttonRemove_Click; buttonRemove.Click += buttonRemove_Click;
// //
// buttonadd // buttonAdd
// //
buttonadd.Location = new Point(3, 22); buttonAdd.Location = new Point(6, 247);
buttonadd.Name = "buttonadd"; buttonAdd.Name = "buttonAdd";
buttonadd.Size = new Size(131, 23); buttonAdd.Size = new Size(143, 22);
buttonadd.TabIndex = 0; buttonAdd.TabIndex = 0;
buttonadd.Text = "Добавить тепловоз"; buttonAdd.Text = "Добавить тепловоз";
buttonadd.UseVisualStyleBackColor = true; buttonAdd.UseVisualStyleBackColor = true;
buttonadd.Click += buttonAdd_Click; buttonAdd.Click += buttonAdd_Click;
// //
// pictureBoxCollectionWarmlyLocomotive // pictureBoxCollectionWarmlyLocomotive
// //
pictureBoxCollectionWarmlyLocomotive.Location = new Point(12, 28); pictureBoxCollectionWarmlyLocomotive.Location = new Point(12, 28);
pictureBoxCollectionWarmlyLocomotive.Name = "pictureBoxCollectionWarmlyLocomotive"; pictureBoxCollectionWarmlyLocomotive.Name = "pictureBoxCollectionWarmlyLocomotive";
pictureBoxCollectionWarmlyLocomotive.Size = new Size(647, 350); pictureBoxCollectionWarmlyLocomotive.Size = new Size(647, 410);
pictureBoxCollectionWarmlyLocomotive.TabIndex = 2; pictureBoxCollectionWarmlyLocomotive.TabIndex = 2;
pictureBoxCollectionWarmlyLocomotive.TabStop = false; pictureBoxCollectionWarmlyLocomotive.TabStop = false;
// //
@ -117,6 +183,8 @@
Text = "FormWarmlyLocomotiveCollection"; Text = "FormWarmlyLocomotiveCollection";
panelCollectionWarmlyLocomotive.ResumeLayout(false); panelCollectionWarmlyLocomotive.ResumeLayout(false);
panelCollectionWarmlyLocomotive.PerformLayout(); panelCollectionWarmlyLocomotive.PerformLayout();
panel1.ResumeLayout(false);
panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollectionWarmlyLocomotive).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollectionWarmlyLocomotive).EndInit();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
@ -127,9 +195,15 @@
private Label labelCollection; private Label labelCollection;
private Panel panelCollectionWarmlyLocomotive; private Panel panelCollectionWarmlyLocomotive;
private PictureBox pictureBoxCollectionWarmlyLocomotive; private PictureBox pictureBoxCollectionWarmlyLocomotive;
private Button buttonadd; private Button buttonAdd;
private Button buttonRemove; private Button buttonRemove;
private Button buttonreFreshCollection; private Button buttonreFreshCollection;
private TextBox textBoxCollectionWarmlyLocomotive; private TextBox maskedTextBoxNumber;
private Label labelGenericCollection;
private Panel panel1;
private Button buttonAddObject;
private ListBox listBoxStorages;
private TextBox textBoxStorageName;
private Button buttonDelObject;
} }
} }

View File

@ -1,28 +1,102 @@
using WarmlyLocomotive.DrawningObjects; using WarmlyLocomotive.Generics;
using WarmlyLocomotive.Generics;
using WarmlyLocomotive.MovementStrategy; using WarmlyLocomotive.MovementStrategy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WarmlyLocomotive namespace WarmlyLocomotive
{ {
public partial class FormWarmlyLocomotiveCollection : Form public partial class FormWarmlyLocomotiveCollection : Form
{ {
private readonly CarsGenericCollection<DrawningWarmlyLocomotive, /// <summary>
DrawningObjectCar> _warmlylocomotives; /// Набор объектов
/// </summary>
private readonly WarmlyLocomotivesGenericStorage _storage;
public FormWarmlyLocomotiveCollection() public FormWarmlyLocomotiveCollection()
{ {
InitializeComponent(); InitializeComponent();
_warmlylocomotives = new CarsGenericCollection<DrawningWarmlyLocomotive, DrawningObjectCar>(pictureBoxCollectionWarmlyLocomotive.Width, pictureBoxCollectionWarmlyLocomotive.Height); _storage = new WarmlyLocomotivesGenericStorage(pictureBoxCollectionWarmlyLocomotive.Width, pictureBoxCollectionWarmlyLocomotive.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 listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollectionWarmlyLocomotive.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowWarmlyLocomotives();
}
/// <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();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAdd_Click(object sender, EventArgs e) private void buttonAdd_Click(object sender, EventArgs e)
{ {
WarmlyLocomotiveForm form = new(); if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
WarmlyLocomotiveForm form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_warmlylocomotives + form.SelectedCar != null) if (obj + form.SelectedWarmlyLocomotive)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollectionWarmlyLocomotive.Image = _warmlylocomotives.ShowCars(); pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives();
} }
else else
{ {
@ -31,35 +105,73 @@ namespace WarmlyLocomotive
} }
} }
/// <summary>
private void buttonreFreshCollection_Click(object sender, EventArgs e) /// Удаление объекта из набора
{ /// </summary>
pictureBoxCollectionWarmlyLocomotive.Image = _warmlylocomotives.ShowCars(); /// <param name="sender"></param>
} /// <param name="e"></param>
private void buttonRemove_Click(object sender, EventArgs e) private void buttonRemove_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("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos; int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (textBoxCollectionWarmlyLocomotive.Text == null || !int.TryParse(textBoxCollectionWarmlyLocomotive.Text, out pos)) if (obj - pos != null)
{
MessageBox.Show("Введите номер парковочного места");
return;
}
if (_warmlylocomotives - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollectionWarmlyLocomotive.Image = _warmlylocomotives.ShowCars(); pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives();
} }
else else
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives();
}
/// <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();
}
} }
} }

View File

@ -4,95 +4,107 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace WarmlyLocomotive.Generics
namespace WarmlyLocomotive.Generics {
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetGeneric<T>
where T : class
{ {
internal class SetGeneric<T> /// <summary>
where T : class /// Список объектов, которые храним
/// </summary>
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{ {
/// <summary> _maxCount = count;
/// Массив объектов, которые храним _places = new List<T?>(count);
/// </summary> }
private readonly T?[] _places; /// <summary>
/// <summary> /// Добавление объекта в набор
/// Количество объектов в массиве /// </summary>
/// </summary> /// <param name="warmlylocomotive">Добавляемый тепловоз</param>
public int Count => _places.Length; /// <returns></returns>
/// <summary> public bool Insert(T warmlylocomotive)
/// Конструктор {
/// </summary> if (_places.Count == _maxCount)
/// <param name="count"></param>
///
public int startPointer = 0;
public SetGeneric(int count)
{ {
_places = new T?[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T car)
{
if (_places[Count - 1] != null)
return -1;
return Insert(car, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T car, int position)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
return -1;
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] = car;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < Count && position >= 0)
{
_places[position] = null;
return true;
}
return false; return false;
} }
/// <summary> Insert(warmlylocomotive, 0);
/// Получение объекта из набора по позиции return true;
/// </summary> }
/// <param name="position"></param> /// <summary>
/// <returns></returns> /// Добавление объекта в набор на конкретную позицию
public T? Get(int position) /// </summary>
/// <param name="airplane">Добавляемый самолет</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T airplane, int position)
{
if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return false;
_places.Insert(position, airplane);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= Count) return false;
_places.RemoveAt(position);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
get
{ {
if (position < Count && position >= 0) { return _places[position]; } if (position < 0 || position >= Count) return null;
return null; 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?> GetWarmlyLocomotives(int? maxWarmlyLocomotives = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxWarmlyLocomotives.HasValue && i == maxWarmlyLocomotives.Value)
{
yield break;
}
} }
} }
} }
}

View File

@ -7,20 +7,18 @@ using WarmlyLocomotive.MovementStrategy;
namespace WarmlyLocomotive namespace WarmlyLocomotive
{ {
/// <summary> /// <summary>
/// Ôîđěŕ đŕáîňű ń îáúĺęňîě "Ńďîđňčâíűé ŕâňîěîáčëü" /// Ôîðìà ðàáîòû ñ îáúåêòîì "Òåïëîâîç ñ òðóáîé è ïðèöåïîì"
/// </summary> /// </summary>
public partial class WarmlyLocomotiveForm : Form public partial class WarmlyLocomotiveForm : Form
{ {
private DrawningWarmlyLocomotive? _drawningWarmlyLocomotive; private DrawningWarmlyLocomotive? _drawningWarmlyLocomotive;
private AbstractStrategy? _abstractStrategy;
private AbstractStrategy? _strategy; private AbstractStrategy? _strategy;
public DrawningWarmlyLocomotive? SelectedCar { get; private set; } public DrawningWarmlyLocomotive? SelectedWarmlyLocomotive { get; private set; }
public WarmlyLocomotiveForm() public WarmlyLocomotiveForm()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null; _strategy = null;
SelectedCar = null; SelectedWarmlyLocomotive = null;
} }
/// <summary> /// <summary>
/// Ìåòîä ïðîðèñîâêè ìàøèíû /// Ìåòîä ïðîðèñîâêè ìàøèíû
@ -99,32 +97,32 @@ namespace WarmlyLocomotive
} }
if (comboBoxWarmlyLocomotive.Enabled) if (comboBoxWarmlyLocomotive.Enabled)
{ {
_abstractStrategy = comboBoxWarmlyLocomotive.SelectedIndex _strategy = comboBoxWarmlyLocomotive.SelectedIndex
switch switch
{ {
0 => new MoveToCenter(), 0 => new MoveToCenter(),
1 => new MoveToBorder(), 1 => new MoveToBorder(),
_ => null, _ => null,
}; };
if (_abstractStrategy == null) if (_strategy == null)
{ {
return; return;
} }
_abstractStrategy.SetData(new _strategy.SetData(new
DrawningObjectCar(_drawningWarmlyLocomotive), pictureBoxWarmlyLocomotive.Width, DrawningObjectWarmlyLocomotive(_drawningWarmlyLocomotive), pictureBoxWarmlyLocomotive.Width,
pictureBoxWarmlyLocomotive.Height); pictureBoxWarmlyLocomotive.Height);
comboBoxWarmlyLocomotive.Enabled = false; comboBoxWarmlyLocomotive.Enabled = false;
} }
if (_abstractStrategy == null) if (_strategy == null)
{ {
return; return;
} }
_abstractStrategy.MakeStep(); _strategy.MakeStep();
Draw(); Draw();
if (_abstractStrategy.GetStatus() == Status.Finish) if (_strategy.GetStatus() == Status.Finish)
{ {
comboBoxWarmlyLocomotive.Enabled = true; comboBoxWarmlyLocomotive.Enabled = true;
_abstractStrategy = null; _strategy = null;
} }
} }
/// <summary> /// <summary>
@ -132,6 +130,37 @@ namespace WarmlyLocomotive
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void buttonCreate_WarmlyLocomotivePro_Click(object sender, EventArgs e)
{
Random random = new();
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dopDialog = new();
if (dopDialog.ShowDialog() == DialogResult.OK)
{
dopColor = dopDialog.Color;
}
_drawningWarmlyLocomotive = new DrawningWarmlyLocomotiveWithTrumpet(random.Next(100, 300),
random.Next(1000, 3000),
color,
dopColor,
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height);
_drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void buttonSelectWarmlyLocomotive_Click(object sender, EventArgs e)
{
SelectedWarmlyLocomotive = _drawningWarmlyLocomotive;
DialogResult = DialogResult.OK;
}
private void buttonCreate_Pro_Click(object sender, EventArgs e) private void buttonCreate_Pro_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
@ -158,11 +187,5 @@ namespace WarmlyLocomotive
100)); 100));
Draw(); Draw();
} }
private void buttonSelectCar_Click(object sender, EventArgs e)
{
SelectedCar = _drawningWarmlyLocomotive;
DialogResult = DialogResult.OK;
}
} }
} }

View File

@ -2,7 +2,7 @@
using WarmlyLocomotive.MovementStrategy; using WarmlyLocomotive.MovementStrategy;
namespace WarmlyLocomotive.Generics namespace WarmlyLocomotive.Generics
{ {
internal class CarsGenericCollection<T, U> internal class WarmlyLocomotivesGenericCollection<T, U>
where T : DrawningWarmlyLocomotive where T : DrawningWarmlyLocomotive
where U : IMoveableObject where U : IMoveableObject
{ {
@ -31,7 +31,7 @@ namespace WarmlyLocomotive.Generics
/// </summary> /// </summary>
/// <param name="picWidth"></param> /// <param name="picWidth"></param>
/// <param name="picHeight"></param> /// <param name="picHeight"></param>
public CarsGenericCollection(int picWidth, int picHeight) public WarmlyLocomotivesGenericCollection(int picWidth, int picHeight)
{ {
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight; int height = picHeight / _placeSizeHeight;
@ -45,14 +45,13 @@ namespace WarmlyLocomotive.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
public static int operator +(CarsGenericCollection<T, U> collect, T? public static bool operator +(WarmlyLocomotivesGenericCollection<T, U> collect, T? obj)
obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect._collection.Insert(obj); return (bool)collect?._collection.Insert(obj);
} }
/// <summary> /// <summary>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания
@ -60,15 +59,14 @@ namespace WarmlyLocomotive.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="pos"></param> /// <param name="pos"></param>
/// <returns></returns> /// <returns></returns>
public static bool operator -(CarsGenericCollection<T, U> collect, int public static bool operator -(WarmlyLocomotivesGenericCollection<T, U> collect, int pos)
pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
} }
return true; return false;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
@ -77,13 +75,13 @@ namespace WarmlyLocomotive.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Bitmap ShowCars() public Bitmap ShowWarmlyLocomotives()
{ {
Bitmap bmp = new(_pictureWidth, _pictureHeight); Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
@ -117,16 +115,16 @@ namespace WarmlyLocomotive.Generics
/// <param name="g"></param> /// <param name="g"></param>
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
DrawningWarmlyLocomotive warmlylocomotive; int i = 0;
int numPlacesInRow = _pictureWidth / _placeSizeWidth; int numPlacesInRow = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++) foreach (var warmlylocomotive in _collection.GetWarmlyLocomotives())
{ {
warmlylocomotive = _collection.Get(i);
if (warmlylocomotive != null) if (warmlylocomotive != null)
{ {
warmlylocomotive.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10); warmlylocomotive.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
warmlylocomotive.DrawTransport(g); warmlylocomotive.DrawTransport(g);
} }
i++;
} }
} }
} }

View File

@ -0,0 +1,40 @@
using WarmlyLocomotive.DrawningObjects;
using WarmlyLocomotive.MovementStrategy;
namespace WarmlyLocomotive.Generics
{
internal class WarmlyLocomotivesGenericStorage
{
readonly Dictionary<string, WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>> _warmlylocomotiveStorages;
public List<string> Keys => _warmlylocomotiveStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public WarmlyLocomotivesGenericStorage(int pictureWidth, int pictureHeight)
{
_warmlylocomotiveStorages = new Dictionary<string,
WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_warmlylocomotiveStorages.Add(name, new WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_warmlylocomotiveStorages.ContainsKey(name))
return;
_warmlylocomotiveStorages.Remove(name);
}
public WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>? this[string ind]
{
get
{
if (_warmlylocomotiveStorages.ContainsKey(ind))
return _warmlylocomotiveStorages[ind];
return null;
}
}
}
}

View File

@ -149,7 +149,7 @@
buttonSelectCar.TabIndex = 11; buttonSelectCar.TabIndex = 11;
buttonSelectCar.Text = "Выбор тепловоза"; buttonSelectCar.Text = "Выбор тепловоза";
buttonSelectCar.UseVisualStyleBackColor = true; buttonSelectCar.UseVisualStyleBackColor = true;
buttonSelectCar.Click += buttonSelectCar_Click; buttonSelectCar.Click += buttonSelectWarmlyLocomotive_Click;
// //
// WarmlyLocomotiveForm // WarmlyLocomotiveForm
// //