4 лаба Кувшинов ПИбд-21 Простая #7

Closed
TImourka wants to merge 4 commits from laba4 into laba3
6 changed files with 312 additions and 54 deletions
Showing only changes of commit 036252a578 - Show all commits

View File

@ -34,8 +34,14 @@
this.ButtonAddTrain = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveTrain = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.textBoxSetName = new System.Windows.Forms.TextBox();
this.buttonAddSet = new System.Windows.Forms.Button();
this.listBoxStorage = new System.Windows.Forms.ListBox();
this.buttonRemoveSet = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxCollection
@ -49,6 +55,8 @@
//
// panel1
//
this.panel1.AccessibleName = "";
this.panel1.Controls.Add(this.panel2);
this.panel1.Controls.Add(this.maskedTextBoxNumber);
this.panel1.Controls.Add(this.ButtonAddTrain);
this.panel1.Controls.Add(this.ButtonRefreshCollection);
@ -61,14 +69,14 @@
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(59, 170);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(33, 365);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20);
this.maskedTextBoxNumber.TabIndex = 4;
//
// ButtonAddTrain
//
this.ButtonAddTrain.Location = new System.Drawing.Point(71, 31);
this.ButtonAddTrain.Location = new System.Drawing.Point(43, 327);
this.ButtonAddTrain.Name = "ButtonAddTrain";
this.ButtonAddTrain.Size = new System.Drawing.Size(75, 23);
this.ButtonAddTrain.TabIndex = 3;
@ -78,9 +86,9 @@
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(71, 320);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(21, 438);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(75, 23);
this.ButtonRefreshCollection.Size = new System.Drawing.Size(121, 23);
this.ButtonRefreshCollection.TabIndex = 2;
this.ButtonRefreshCollection.Text = "Refresh Collection";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -88,14 +96,62 @@
//
// ButtonRemoveTrain
//
this.ButtonRemoveTrain.Location = new System.Drawing.Point(71, 196);
this.ButtonRemoveTrain.Location = new System.Drawing.Point(33, 400);
this.ButtonRemoveTrain.Name = "ButtonRemoveTrain";
this.ButtonRemoveTrain.Size = new System.Drawing.Size(75, 23);
this.ButtonRemoveTrain.Size = new System.Drawing.Size(100, 23);
this.ButtonRemoveTrain.TabIndex = 0;
this.ButtonRemoveTrain.Text = "Remove Train";
this.ButtonRemoveTrain.UseVisualStyleBackColor = true;
this.ButtonRemoveTrain.Click += new System.EventHandler(this.ButtonRemoveTrain_Click);
//
// panel2
//
this.panel2.AccessibleName = "";
this.panel2.Controls.Add(this.buttonRemoveSet);
this.panel2.Controls.Add(this.listBoxStorage);
this.panel2.Controls.Add(this.buttonAddSet);
this.panel2.Controls.Add(this.textBoxSetName);
this.panel2.Location = new System.Drawing.Point(14, 12);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(140, 208);
this.panel2.TabIndex = 5;
//
// textBoxSetName
//
this.textBoxSetName.Location = new System.Drawing.Point(8, 12);
this.textBoxSetName.Name = "textBoxSetName";
this.textBoxSetName.Size = new System.Drawing.Size(120, 20);
this.textBoxSetName.TabIndex = 0;
//
// buttonAddSet
//
this.buttonAddSet.Location = new System.Drawing.Point(8, 38);
this.buttonAddSet.Name = "buttonAddSet";
this.buttonAddSet.Size = new System.Drawing.Size(120, 23);
this.buttonAddSet.TabIndex = 1;
this.buttonAddSet.Text = "Add set";
this.buttonAddSet.UseVisualStyleBackColor = true;
this.buttonAddSet.Click += new System.EventHandler(this.buttonAddSet_Click);
//
// listBoxStorage
//
this.listBoxStorage.FormattingEnabled = true;
this.listBoxStorage.Location = new System.Drawing.Point(8, 67);
this.listBoxStorage.Name = "listBoxStorage";
this.listBoxStorage.Size = new System.Drawing.Size(120, 95);
this.listBoxStorage.TabIndex = 2;
this.listBoxStorage.SelectedIndexChanged += new System.EventHandler(this.listBoxStorage_SelectedIndexChanged);
//
// buttonRemoveSet
//
this.buttonRemoveSet.Location = new System.Drawing.Point(8, 168);
this.buttonRemoveSet.Name = "buttonRemoveSet";
this.buttonRemoveSet.Size = new System.Drawing.Size(120, 23);
this.buttonRemoveSet.TabIndex = 3;
this.buttonRemoveSet.Text = "Remove set";
this.buttonRemoveSet.UseVisualStyleBackColor = true;
this.buttonRemoveSet.Click += new System.EventHandler(this.buttonRemoveSet_Click);
//
// FormTrainCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -108,6 +164,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
}
@ -120,5 +178,10 @@
private System.Windows.Forms.Button ButtonRemoveTrain;
private System.Windows.Forms.Button ButtonAddTrain;
private System.Windows.Forms.TextBox maskedTextBoxNumber;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button buttonRemoveSet;
private System.Windows.Forms.ListBox listBoxStorage;
private System.Windows.Forms.Button buttonAddSet;
private System.Windows.Forms.TextBox textBoxSetName;
}
}

View File

@ -7,6 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace Laba1Loco
{
@ -15,15 +16,36 @@ namespace Laba1Loco
/// <summary>
/// Набор объектов
/// </summary>
private readonly TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
private readonly TrainsGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormTrainCollection()
{
InitializeComponent();
_trains = new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new TrainsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorage.SelectedIndex;
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
{
listBoxStorage.SelectedIndex = 0;
}
else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count)
{
listBoxStorage.SelectedIndex = index;
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
@ -31,13 +53,22 @@ namespace Laba1Loco
/// <param name="e"></param>
private void ButtonAddTrain_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormLocomotive form = new FormLocomotive();
if (form.ShowDialog() == DialogResult.OK)
{
if ((_trains + form.SelectedTrain) != -1)
if ((obj + form.SelectedTrain) != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _trains.ShowTrains();
pictureBoxCollection.Image = obj.ShowTrains();
}
else
{
@ -53,6 +84,11 @@ namespace Laba1Loco
/// <param name="e"></param>
private void ButtonRemoveTrain_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
@ -64,11 +100,16 @@ namespace Laba1Loco
MessageBox.Show("Не удалось удалить объект");
return;
}
if (maskedTextBoxNumber.Text.Length == 0)
{
MessageBox.Show("Не удалось удалить объект");
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_trains - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _trains.ShowTrains();
pictureBoxCollection.Image = obj.ShowTrains();
}
else
{
@ -82,7 +123,58 @@ namespace Laba1Loco
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _trains.ShowTrains();
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
pictureBoxCollection.Image = obj.ShowTrains();
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddSet_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxSetName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxSetName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRemoveSet_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект { listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorage.SelectedItem.ToString()?? string.Empty);
ReloadObjects();
}
}
}
}

View File

@ -74,6 +74,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SetGeneric.cs" />
<Compile Include="Status.cs" />
<Compile Include="TrainsGenericStorage.cs" />
<EmbeddedResource Include="FormLocomotive.resx">
<DependentUpon>FormLocomotive.cs</DependentUpon>
</EmbeddedResource>

View File

@ -16,18 +16,23 @@ namespace Laba1Loco
/// <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>
/// Добавление объекта в набор
@ -36,20 +41,10 @@ namespace Laba1Loco
/// <returns></returns>
public int Insert(T train)
{
int i = 0;
for (;i < _places.Length; i++)
{
if (_places[i] == null)
break;
}
if (i == _places.Length)
if (_places.Count >= _maxCount)
return -1;
for (; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[i] = train;
return i;
_places.Insert(0, train);
return 0;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -59,20 +54,17 @@ namespace Laba1Loco
/// <returns></returns>
public bool Insert(T train, int position)
{
if (position < 0 || position >= _places.Length)
if (_places.Count >= _maxCount)
return false;
for (; position < _places.Length; position++)
{
if (_places[position] == null)
break;
}
if (position == _places.Length)
if (position < 0 || position > _places.Count)
return false;
for (; position > 0; position--)
{
_places[position] = _places[position - 1];
}
_places[position] = train;
if (position == _places.Count)
_places.Add(train);
else
_places.Insert(position, train);
return true;
}
/// <summary>
@ -82,9 +74,9 @@ namespace Laba1Loco
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= _places.Length)
if (position < 0 || position >= _places.Count)
return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
/// <summary>
@ -94,10 +86,43 @@ namespace Laba1Loco
/// <returns></returns>
public T Get(int position)
{
if (position < 0 || position >= _places.Length)
if (position < 0 || position >= _places.Count)
return null;
return _places[position];
}
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T this[int position]
{
get
{
if (position < 0 || position >= _places.Count)
return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetCars(int? maxCars = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value)
{
yield break;
}
}
}
}
}

View File

@ -67,13 +67,14 @@ where U : IMoveableObject
/// <returns></returns>
public static T operator -(TrainsGenericCollection<T, U> collect, int pos)
{
T obj = collect._collection.Get(pos);
T obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
@ -81,7 +82,7 @@ where U : IMoveableObject
/// <returns></returns>
public U GetU(int pos)
{
return (U)_collection.Get(pos)?.GetMoveableObject;
return (U)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -118,17 +119,18 @@ where U : IMoveableObject
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var car in _collection.GetCars())
{
T t = _collection.Get(i);
if (t != null)
if (car != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t is DrawingLoco)
(t as DrawingLoco).DrawTransport(g);
car.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (car is DrawingLoco)
(car as DrawingLoco).DrawTransport(g);
else
t.DrawTransport(g);
car.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Laba1Loco
{
internal class TrainsGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>> _carStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _carStorages.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 TrainsGenericStorage(int pictureWidth, int pictureHeight)
{
_carStorages = new Dictionary<string, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_carStorages.ContainsKey(name))
return;
_carStorages[name] = new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_carStorages.ContainsKey(name))
return;
_carStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>
this[string ind]
{
get
{
if (_carStorages.ContainsKey(ind))
return _carStorages[ind];
return null;
}
}
}
}