Боровков М В ПИбд-22 8 лабораторная работа #9

Closed
bekodeg wants to merge 2 commits from labWork8 into labWork7
11 changed files with 299 additions and 53 deletions
Showing only changes of commit 732526724d - Show all commits

View File

@ -29,8 +29,9 @@
private void InitializeComponent()
{
tableLayoutPanel1 = new TableLayoutPanel();
pictureBoxCollection = new PictureBox();
groupBox1 = new GroupBox();
button2 = new Button();
buttonSortByType = new Button();
groupBox2 = new GroupBox();
buttonDelObject = new Button();
listBoxStorages = new ListBox();
@ -40,14 +41,15 @@
buttonRefreshCollection = new Button();
buttonRemoveLocomotive = new Button();
buttonAddLocomotive = new Button();
pictureBoxCollection = new PictureBox();
menuStrip1 = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
saveToolStripMenuItem = new ToolStripMenuItem();
loadToolStripMenuItem = new ToolStripMenuItem();
tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout();
//
@ -57,29 +59,22 @@
tableLayoutPanel1.ColumnCount = 2;
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle());
tableLayoutPanel1.Controls.Add(pictureBoxCollection, 0, 0);
tableLayoutPanel1.Controls.Add(groupBox1, 1, 0);
tableLayoutPanel1.Controls.Add(pictureBoxCollection, 0, 0);
tableLayoutPanel1.Dock = DockStyle.Fill;
tableLayoutPanel1.Location = new Point(0, 28);
tableLayoutPanel1.Name = "tableLayoutPanel1";
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.RowStyles.Add(new RowStyle());
tableLayoutPanel1.Size = new Size(885, 453);
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F));
tableLayoutPanel1.Size = new Size(885, 499);
tableLayoutPanel1.TabIndex = 0;
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(3, 3);
pictureBoxCollection.MinimumSize = new Size(100, 100);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(673, 447);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
groupBox1.Controls.Add(button2);
groupBox1.Controls.Add(buttonSortByType);
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(buttonRefreshCollection);
@ -89,11 +84,31 @@
groupBox1.MaximumSize = new Size(200, 1000000);
groupBox1.MinimumSize = new Size(200, 200);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(200, 444);
groupBox1.Size = new Size(200, 484);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// button2
//
button2.Location = new Point(12, 311);
button2.Name = "button2";
button2.Size = new Size(176, 29);
button2.TabIndex = 6;
button2.Text = "Сортировка по цвету";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(12, 276);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(176, 29);
buttonSortByType.TabIndex = 2;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// groupBox2
//
groupBox2.Controls.Add(buttonDelObject);
@ -109,7 +124,7 @@
//
// buttonDelObject
//
buttonDelObject.Location = new Point(4, 204);
buttonDelObject.Location = new Point(6, 204);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(176, 29);
buttonDelObject.TabIndex = 3;
@ -129,7 +144,7 @@
//
// buttonAddObject
//
buttonAddObject.Location = new Point(4, 59);
buttonAddObject.Location = new Point(6, 59);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(176, 29);
buttonAddObject.TabIndex = 1;
@ -146,18 +161,18 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(10, 328);
maskedTextBoxNumber.Location = new Point(12, 381);
maskedTextBoxNumber.Mask = "00000";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(184, 27);
maskedTextBoxNumber.Size = new Size(176, 27);
maskedTextBoxNumber.TabIndex = 4;
maskedTextBoxNumber.ValidatingType = typeof(int);
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(10, 396);
buttonRefreshCollection.Location = new Point(12, 449);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(184, 29);
buttonRefreshCollection.Size = new Size(176, 29);
buttonRefreshCollection.TabIndex = 3;
buttonRefreshCollection.Text = "Обновить колекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
@ -165,9 +180,9 @@
//
// buttonRemoveLocomotive
//
buttonRemoveLocomotive.Location = new Point(10, 361);
buttonRemoveLocomotive.Location = new Point(12, 414);
buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
buttonRemoveLocomotive.Size = new Size(184, 29);
buttonRemoveLocomotive.Size = new Size(176, 29);
buttonRemoveLocomotive.TabIndex = 2;
buttonRemoveLocomotive.Text = "Удалить локомотив";
buttonRemoveLocomotive.UseVisualStyleBackColor = true;
@ -175,14 +190,23 @@
//
// buttonAddLocomotive
//
buttonAddLocomotive.Location = new Point(10, 293);
buttonAddLocomotive.Location = new Point(12, 346);
buttonAddLocomotive.Name = "buttonAddLocomotive";
buttonAddLocomotive.Size = new Size(184, 29);
buttonAddLocomotive.Size = new Size(176, 29);
buttonAddLocomotive.TabIndex = 0;
buttonAddLocomotive.Text = "Добавить локомотив";
buttonAddLocomotive.UseVisualStyleBackColor = true;
buttonAddLocomotive.Click += buttonAddLocomotive_Click;
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(3, 3);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(673, 493);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// menuStrip1
//
menuStrip1.ImageScalingSize = new Size(20, 20);
@ -203,14 +227,14 @@
// saveToolStripMenuItem
//
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
saveToolStripMenuItem.Size = new Size(224, 26);
saveToolStripMenuItem.Size = new Size(177, 26);
saveToolStripMenuItem.Text = "Сохранение";
saveToolStripMenuItem.Click += saveToolStripMenuItem_Click;
//
// loadToolStripMenuItem
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.Size = new Size(224, 26);
loadToolStripMenuItem.Size = new Size(177, 26);
loadToolStripMenuItem.Text = "Загрузка";
loadToolStripMenuItem.Click += loadToolStripMenuItem_Click;
//
@ -218,7 +242,7 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(885, 481);
ClientSize = new Size(885, 527);
Controls.Add(tableLayoutPanel1);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
@ -226,11 +250,11 @@
Name = "FormLocomotiveCollection";
Text = "FormLocomotiveCollection";
tableLayoutPanel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false);
@ -242,7 +266,6 @@
private TableLayoutPanel tableLayoutPanel1;
private GroupBox groupBox1;
private Button buttonAddLocomotive;
private PictureBox pictureBoxCollection;
private Button buttonRemoveLocomotive;
private Button buttonRefreshCollection;
private MaskedTextBox maskedTextBoxNumber;
@ -255,5 +278,8 @@
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem saveToolStripMenuItem;
private ToolStripMenuItem loadToolStripMenuItem;
private Button button2;
private Button buttonSortByType;
private PictureBox pictureBoxCollection;
}
}

View File

@ -50,5 +50,15 @@ namespace ProjectElectricLocomotive
{
LoadToolStripMenuItem_Click(sender, e);
}
private void buttonSortByType_Click(object sender, EventArgs e)
{
ButtonSortByType_Click(sender, e);
}
private void button2_Click(object sender, EventArgs e)
{
ButtonSortByColor_Click(sender, e);
}
}
}

View File

@ -120,4 +120,7 @@
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>

View File

@ -0,0 +1,63 @@
using ProjectElectricLocomotive.DrawningObjects;
using ProjectElectricLocomotive.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class DrawiningLocomotiveEqutables : IEqualityComparer<DrawningLocomotive?>
{
public bool Equals(DrawningLocomotive? x, DrawningLocomotive? y)
{
if (x == null || x.EntityLocomotive == null)
{
throw new ArgumentException(nameof(x));
}
if (y == null || y.EntityLocomotive == null)
{
throw new ArgumentException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityLocomotive.Speed != y.EntityLocomotive.Speed)
{
return false;
}
if (x.EntityLocomotive.Weight != y.EntityLocomotive.Weight)
{
return false;
}
if (x.EntityLocomotive.BodyColor != y.EntityLocomotive.BodyColor)
{
return false;
}
if (x is DrawningElectricLocomotive && y is DrawningElectricLocomotive)
{
if (((EntityElectricLocomotive)x.EntityLocomotive).AdditionalColor != ((EntityElectricLocomotive)y.EntityLocomotive).AdditionalColor)
{
return false;
}
if (((EntityElectricLocomotive)x.EntityLocomotive).Battery != ((EntityElectricLocomotive)y.EntityLocomotive).Battery)
{
return false;
}
if (((EntityElectricLocomotive)x.EntityLocomotive).Horns != ((EntityElectricLocomotive)y.EntityLocomotive).Horns)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningLocomotive? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -0,0 +1,35 @@
using ProjectElectricLocomotive.DrawningObjects;
using ProjectElectricLocomotive.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotiveCompareByColor : IComparer<DrawningLocomotive?>
Review

Требовалось сортировать по критериям: цвет, скорость, вес

Требовалось сортировать по критериям: цвет, скорость, вес
{
public int Compare(DrawningLocomotive? x, DrawningLocomotive? y)
{
if (x == null || x.EntityLocomotive == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityLocomotive == null)
{
throw new ArgumentNullException(nameof(y));
}
if (!(x is DrawningElectricLocomotive && y is DrawningElectricLocomotive))
{
return x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb());
}
if (x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()) != 0)
{
return x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb());
}
return ((EntityElectricLocomotive)x.EntityLocomotive).AdditionalColor.ToArgb().CompareTo(
((EntityElectricLocomotive)y.EntityLocomotive).AdditionalColor.ToArgb());
}
}
}

View File

@ -0,0 +1,34 @@
using ProjectElectricLocomotive.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotiveCompareByType : IComparer<DrawningLocomotive>
{
public int Compare(DrawningLocomotive? x, DrawningLocomotive? y)
{
if (x == null || x.EntityLocomotive == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityLocomotive == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var sreedCompare = x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed);
if (sreedCompare != 0)
{
return sreedCompare;
}
return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight);
}
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotivesCollectionInfo : IEquatable<LocomotivesCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public LocomotivesCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(LocomotivesCollectionInfo? other)
{
if (ReferenceEquals(null, other)) return false;
return Name.Equals(other.Name);
}
public override int GetHashCode() => Name.GetHashCode();
}
}

View File

@ -62,7 +62,7 @@ namespace ProjectElectricLocomotive.Generics
{
return 0;
}
if (collect?._collection.Insert(obj) ?? false)
if (collect?._collection.Insert(obj, new DrawiningLocomotiveEqutables()) ?? false)
{
return collect._collection.Count;
}
@ -150,5 +150,10 @@ namespace ProjectElectricLocomotive.Generics
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetLocomotives => _collection.GetLocomotives();
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer) => _collection.SortSet(comparer);
}
}

View File

@ -17,14 +17,14 @@ namespace ProjectElectricLocomotive.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, LocomotivesGenericCollection<
readonly Dictionary<LocomotivesCollectionInfo, LocomotivesGenericCollection<
DrawningLocomotive, DrawningObjectLocomotive>
> _locomotiveStorage;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _locomotiveStorage.Keys.ToList();
public List<LocomotivesCollectionInfo> Keys => _locomotiveStorage.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
@ -55,7 +55,7 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="pictureHeight"></param>
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight)
{
_locomotiveStorage = new Dictionary<string,
_locomotiveStorage = new Dictionary<LocomotivesCollectionInfo,
LocomotivesGenericCollection<DrawningLocomotive,
DrawningObjectLocomotive>>();
_pictureWidth = pictureWidth;
@ -68,11 +68,12 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (Keys.Contains(name))
LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(name, "");
if (Keys.Contains(locomotivesCollectionInfo))
{
return;
};
_locomotiveStorage.Add(name,
_locomotiveStorage.Add(locomotivesCollectionInfo,
new LocomotivesGenericCollection<
DrawningLocomotive, DrawningObjectLocomotive>(
_pictureWidth, _pictureHeight));
@ -83,9 +84,10 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (Keys.Contains(name))
LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(name, "");
if (Keys.Contains(locomotivesCollectionInfo))
{
_locomotiveStorage.Remove(name);
_locomotiveStorage.Remove(locomotivesCollectionInfo);
}
}
/// <summary>
@ -98,9 +100,10 @@ namespace ProjectElectricLocomotive.Generics
{
get
{
if (Keys.Contains(ind))
LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(ind, "");
if (Keys.Contains(locomotivesCollectionInfo))
{
return _locomotiveStorage[ind];
return _locomotiveStorage[locomotivesCollectionInfo];
}
return null;
}
@ -126,11 +129,11 @@ namespace ProjectElectricLocomotive.Generics
throw new InvalidOperationException("Невалиданя операция, нет данных длясохранения");
}
foreach (
KeyValuePair<string, LocomotivesGenericCollection<
KeyValuePair<LocomotivesCollectionInfo, LocomotivesGenericCollection<
DrawningLocomotive, DrawningObjectLocomotive>>
record in _locomotiveStorage)
{
sw.Write(record.Key);
sw.Write(record.Key.Name);
sw.Write(_separatorForKeyValue);
foreach (DrawningLocomotive? elem in record.Value.GetLocomotives)
{
@ -189,7 +192,8 @@ namespace ProjectElectricLocomotive.Generics
}
}
}
_locomotiveStorage.Add(record[0], collection);
LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(record[0], "");
_locomotiveStorage.Add(locomotivesCollectionInfo, collection);
}
return true;
}

View File

@ -41,13 +41,9 @@ namespace ProjectElectricLocomotive.Generics
/// </summary>
/// <param name="locomotive">Добавляемый локомотив</param>
/// <returns></returns>
public bool Insert(T locomotive)
public bool Insert(T locomotive, IEqualityComparer<T>? equal = null)
{
if (_places.Count >= _maxCount) {
throw new StorageOverflowException(_maxCount);
}
_places.Insert(0, locomotive);
return true;
return Insert(locomotive, 0, equal);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -55,12 +51,17 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="locomotive">Добавляемый поезд</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T locomotive, int position)
public bool Insert(T locomotive, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count >= _maxCount || position >= 0 || position <= _places.Count)
if (_places.Count >= _maxCount || position < 0 || position > _places.Count)
{
return false;
}
if (equal != null && _places.Contains(locomotive, equal))
{
throw new ArgumentException("объект уже находится в коллекции");
}
_places.Insert(position, locomotive);
return true;
}
@ -118,6 +119,10 @@ namespace ProjectElectricLocomotive.Generics
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
}
}

View File

@ -43,7 +43,7 @@ namespace ProjectElectricLocomotive
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
listBoxStorages.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
@ -147,6 +147,10 @@ namespace ProjectElectricLocomotive
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.ToString());
}
catch (ArgumentException ex) {
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.ToString());
}
}
/// <summary>
@ -271,5 +275,37 @@ namespace ProjectElectricLocomotive
}
}
}
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void CompareLocomotives(IComparer<DrawningLocomotive?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowLocomotives();
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType());
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByColor());
}
}