4 лаба целиком

This commit is contained in:
Timourka 2023-10-23 22:24:05 +04:00
parent 4deea7f26c
commit 036252a578
6 changed files with 312 additions and 54 deletions

View File

@ -34,8 +34,14 @@
this.ButtonAddTrain = new System.Windows.Forms.Button(); this.ButtonAddTrain = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveTrain = 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(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panel1.SuspendLayout(); this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
@ -49,6 +55,8 @@
// //
// panel1 // panel1
// //
this.panel1.AccessibleName = "";
this.panel1.Controls.Add(this.panel2);
this.panel1.Controls.Add(this.maskedTextBoxNumber); this.panel1.Controls.Add(this.maskedTextBoxNumber);
this.panel1.Controls.Add(this.ButtonAddTrain); this.panel1.Controls.Add(this.ButtonAddTrain);
this.panel1.Controls.Add(this.ButtonRefreshCollection); this.panel1.Controls.Add(this.ButtonRefreshCollection);
@ -61,14 +69,14 @@
// //
// maskedTextBoxNumber // 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.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20); this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20);
this.maskedTextBoxNumber.TabIndex = 4; this.maskedTextBoxNumber.TabIndex = 4;
// //
// ButtonAddTrain // 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.Name = "ButtonAddTrain";
this.ButtonAddTrain.Size = new System.Drawing.Size(75, 23); this.ButtonAddTrain.Size = new System.Drawing.Size(75, 23);
this.ButtonAddTrain.TabIndex = 3; this.ButtonAddTrain.TabIndex = 3;
@ -78,9 +86,9 @@
// //
// ButtonRefreshCollection // 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.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.TabIndex = 2;
this.ButtonRefreshCollection.Text = "Refresh Collection"; this.ButtonRefreshCollection.Text = "Refresh Collection";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true; this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -88,14 +96,62 @@
// //
// ButtonRemoveTrain // 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.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.TabIndex = 0;
this.ButtonRemoveTrain.Text = "Remove Train"; this.ButtonRemoveTrain.Text = "Remove Train";
this.ButtonRemoveTrain.UseVisualStyleBackColor = true; this.ButtonRemoveTrain.UseVisualStyleBackColor = true;
this.ButtonRemoveTrain.Click += new System.EventHandler(this.ButtonRemoveTrain_Click); 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 // FormTrainCollection
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -108,6 +164,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.panel1.ResumeLayout(false); this.panel1.ResumeLayout(false);
this.panel1.PerformLayout(); this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
@ -120,5 +178,10 @@
private System.Windows.Forms.Button ButtonRemoveTrain; private System.Windows.Forms.Button ButtonRemoveTrain;
private System.Windows.Forms.Button ButtonAddTrain; private System.Windows.Forms.Button ButtonAddTrain;
private System.Windows.Forms.TextBox maskedTextBoxNumber; 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.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace Laba1Loco namespace Laba1Loco
{ {
@ -15,15 +16,36 @@ namespace Laba1Loco
/// <summary> /// <summary>
/// Набор объектов /// Набор объектов
/// </summary> /// </summary>
private readonly TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains; private readonly TrainsGenericStorage _storage;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormTrainCollection() public FormTrainCollection()
{ {
InitializeComponent(); 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>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
@ -31,13 +53,22 @@ namespace Laba1Loco
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonAddTrain_Click(object sender, EventArgs e) 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(); FormLocomotive form = new FormLocomotive();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if ((_trains + form.SelectedTrain) != -1) if ((obj + form.SelectedTrain) != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _trains.ShowTrains(); pictureBoxCollection.Image = obj.ShowTrains();
} }
else else
{ {
@ -53,6 +84,11 @@ namespace Laba1Loco
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRemoveTrain_Click(object sender, EventArgs e) private void ButtonRemoveTrain_Click(object sender, EventArgs e)
{ {
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (MessageBox.Show("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
@ -64,11 +100,16 @@ namespace Laba1Loco
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
return; return;
} }
if (maskedTextBoxNumber.Text.Length == 0)
{
MessageBox.Show("Не удалось удалить объект");
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text); int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_trains - pos != null) if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _trains.ShowTrains(); pictureBoxCollection.Image = obj.ShowTrains();
} }
else else
{ {
@ -82,7 +123,58 @@ namespace Laba1Loco
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e) 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="Properties\AssemblyInfo.cs" />
<Compile Include="SetGeneric.cs" /> <Compile Include="SetGeneric.cs" />
<Compile Include="Status.cs" /> <Compile Include="Status.cs" />
<Compile Include="TrainsGenericStorage.cs" />
<EmbeddedResource Include="FormLocomotive.resx"> <EmbeddedResource Include="FormLocomotive.resx">
<DependentUpon>FormLocomotive.cs</DependentUpon> <DependentUpon>FormLocomotive.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>

View File

@ -16,18 +16,23 @@ namespace Laba1Loco
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Массив объектов, которые храним
/// </summary> /// </summary>
private readonly T[] _places; private readonly List<T> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в массиве
/// </summary> /// </summary>
public int Count => _places.Length; public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T[count]; _maxCount = count;
_places = new List<T>(count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
@ -36,20 +41,10 @@ namespace Laba1Loco
/// <returns></returns> /// <returns></returns>
public int Insert(T train) public int Insert(T train)
{ {
int i = 0; if (_places.Count >= _maxCount)
for (;i < _places.Length; i++)
{
if (_places[i] == null)
break;
}
if (i == _places.Length)
return -1; return -1;
for (; i > 0; i--) _places.Insert(0, train);
{ return 0;
_places[i] = _places[i - 1];
}
_places[i] = train;
return i;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -59,20 +54,17 @@ namespace Laba1Loco
/// <returns></returns> /// <returns></returns>
public bool Insert(T train, int position) public bool Insert(T train, int position)
{ {
if (position < 0 || position >= _places.Length) if (_places.Count >= _maxCount)
return false; return false;
for (; position < _places.Length; position++)
{ if (position < 0 || position > _places.Count)
if (_places[position] == null)
break;
}
if (position == _places.Length)
return false; return false;
for (; position > 0; position--)
{ if (position == _places.Count)
_places[position] = _places[position - 1]; _places.Add(train);
} else
_places[position] = train; _places.Insert(position, train);
return true; return true;
} }
/// <summary> /// <summary>
@ -82,9 +74,9 @@ namespace Laba1Loco
/// <returns></returns> /// <returns></returns>
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= _places.Length) if (position < 0 || position >= _places.Count)
return false; return false;
_places[position] = null; _places.RemoveAt(position);
return true; return true;
} }
/// <summary> /// <summary>
@ -94,10 +86,43 @@ namespace Laba1Loco
/// <returns></returns> /// <returns></returns>
public T Get(int position) public T Get(int position)
{ {
if (position < 0 || position >= _places.Length) if (position < 0 || position >= _places.Count)
return null; return null;
return _places[position]; 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> /// <returns></returns>
public static T operator -(TrainsGenericCollection<T, U> collect, int pos) public static T operator -(TrainsGenericCollection<T, U> collect, int 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 obj; return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
/// </summary> /// </summary>
@ -81,7 +82,7 @@ where U : IMoveableObject
/// <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>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -118,17 +119,18 @@ where U : IMoveableObject
/// <param name="g"></param> /// <param name="g"></param>
private void DrawObjects(Graphics g) 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 (car != null)
if (t != null)
{ {
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); car.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t is DrawingLoco) if (car is DrawingLoco)
(t as DrawingLoco).DrawTransport(g); (car as DrawingLoco).DrawTransport(g);
else 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;
}
}
}
}