ISEbd-22. Baygulov A.A. Lab work 08 #9

Closed
tellsense wants to merge 4 commits from lab8 into lab7
9 changed files with 299 additions and 38 deletions

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.Entities;
using System.Diagnostics.CodeAnalysis;
namespace ProjectElectricLocomotive.Generics
{
internal class DrawingLocomotiveEqutables : IEqualityComparer<DrawingLocomotive?>
{
public bool Equals(DrawingLocomotive? x, DrawingLocomotive? 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 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 DrawingElectricLocomotive && y is DrawingElectricLocomotive)
{
if ((x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor != (y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor)
{
return false;
}
if ((x.EntityLocomotive as EntityElectricLocomotive).Pantograph != (y.EntityLocomotive as EntityElectricLocomotive).Pantograph)
{
return false;
}
if ((x.EntityLocomotive as EntityElectricLocomotive).Compartment != (y.EntityLocomotive as EntityElectricLocomotive).Compartment)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawingLocomotive obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -30,11 +30,12 @@
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormLocomotiveCollection));
groupBoxSets = new GroupBox();
ButtonSortByType = new Button();
textBoxSetName = new TextBox();
buttonDeleteSet = new Button();
listBoxStorages = new ListBox();
ButtonAddLocomotive = new Button();
buttonAddSet = new Button();
ButtonAddLocomotive = new Button();
maskedTextBoxNumber = new MaskedTextBox();
ButtonRefreshCollection = new Button();
ButtonRemoveLocomotive = new Button();
@ -46,6 +47,7 @@
saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog();
groupBoxMenu = new GroupBox();
ButtonSortByColor = new Button();
groupBoxSets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout();
@ -57,27 +59,36 @@
groupBoxSets.Controls.Add(textBoxSetName);
groupBoxSets.Controls.Add(buttonDeleteSet);
groupBoxSets.Controls.Add(listBoxStorages);
groupBoxSets.Controls.Add(ButtonAddLocomotive);
groupBoxSets.Controls.Add(buttonAddSet);
groupBoxSets.Location = new Point(0, 22);
groupBoxSets.Name = "groupBoxSets";
groupBoxSets.Size = new Size(242, 314);
groupBoxSets.Size = new Size(242, 264);
groupBoxSets.TabIndex = 5;
groupBoxSets.TabStop = false;
groupBoxSets.Text = "Наборы";
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(6, 295);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(230, 41);
ButtonSortByType.TabIndex = 4;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// textBoxSetName
//
textBoxSetName.Location = new Point(6, 38);
textBoxSetName.Name = "textBoxSetName";
textBoxSetName.Size = new Size(226, 23);
textBoxSetName.Size = new Size(230, 23);
textBoxSetName.TabIndex = 3;
//
// buttonDeleteSet
//
buttonDeleteSet.Location = new Point(6, 216);
buttonDeleteSet.Name = "buttonDeleteSet";
buttonDeleteSet.Size = new Size(226, 45);
buttonDeleteSet.Size = new Size(230, 45);
buttonDeleteSet.TabIndex = 2;
buttonDeleteSet.Text = "Удалить набор";
buttonDeleteSet.UseVisualStyleBackColor = true;
@ -89,40 +100,40 @@
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(6, 116);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(226, 94);
listBoxStorages.Size = new Size(230, 94);
listBoxStorages.TabIndex = 1;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// ButtonAddLocomotive
//
ButtonAddLocomotive.Location = new Point(6, 269);
ButtonAddLocomotive.Name = "ButtonAddLocomotive";
ButtonAddLocomotive.Size = new Size(226, 39);
ButtonAddLocomotive.TabIndex = 0;
ButtonAddLocomotive.Text = "Добавить локомотив";
ButtonAddLocomotive.UseVisualStyleBackColor = true;
ButtonAddLocomotive.Click += buttonAddLocomotive_Click;
//
// buttonAddSet
//
buttonAddSet.Location = new Point(6, 67);
buttonAddSet.Name = "buttonAddSet";
buttonAddSet.Size = new Size(226, 34);
buttonAddSet.Size = new Size(230, 34);
buttonAddSet.TabIndex = 0;
buttonAddSet.Text = "Добавить набор";
buttonAddSet.UseVisualStyleBackColor = true;
buttonAddSet.Click += ButtonAddObject_Click;
//
// ButtonAddLocomotive
//
ButtonAddLocomotive.Location = new Point(6, 395);
ButtonAddLocomotive.Name = "ButtonAddLocomotive";
ButtonAddLocomotive.Size = new Size(230, 39);
ButtonAddLocomotive.TabIndex = 0;
ButtonAddLocomotive.Text = "Добавить локомотив";
ButtonAddLocomotive.UseVisualStyleBackColor = true;
ButtonAddLocomotive.Click += buttonAddLocomotive_Click;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(6, 342);
maskedTextBoxNumber.Location = new Point(6, 440);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(226, 23);
maskedTextBoxNumber.Size = new Size(230, 23);
maskedTextBoxNumber.TabIndex = 3;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(6, 440);
ButtonRefreshCollection.Location = new Point(6, 514);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(230, 43);
ButtonRefreshCollection.TabIndex = 2;
@ -132,7 +143,7 @@
//
// ButtonRemoveLocomotive
//
ButtonRemoveLocomotive.Location = new Point(6, 388);
ButtonRemoveLocomotive.Location = new Point(6, 469);
ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive";
ButtonRemoveLocomotive.Size = new Size(230, 39);
ButtonRemoveLocomotive.TabIndex = 1;
@ -195,10 +206,13 @@
// groupBoxMenu
//
groupBoxMenu.AutoSize = true;
groupBoxMenu.Controls.Add(ButtonSortByType);
groupBoxMenu.Controls.Add(ButtonSortByColor);
groupBoxMenu.Controls.Add(maskedTextBoxNumber);
groupBoxMenu.Controls.Add(groupBoxSets);
groupBoxMenu.Controls.Add(ButtonRemoveLocomotive);
groupBoxMenu.Controls.Add(ButtonRefreshCollection);
groupBoxMenu.Controls.Add(ButtonAddLocomotive);
groupBoxMenu.Dock = DockStyle.Right;
groupBoxMenu.Location = new Point(661, 24);
groupBoxMenu.Name = "groupBoxMenu";
@ -207,6 +221,16 @@
groupBoxMenu.TabStop = false;
groupBoxMenu.Text = "Инструменты";
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(6, 342);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(230, 38);
ButtonSortByColor.TabIndex = 4;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// FormLocomotiveCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -247,5 +271,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private GroupBox groupBoxMenu;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -53,7 +53,7 @@ namespace ProjectElectricLocomotive
_logger.LogWarning("Неудачная попытка. Коллекция не добавлена, не все данные заполнены");
return;
}
_storage.AddSet(textBoxSetName.Text);
_storage.AddSet(textBoxSetName.Text);
ReloadObjects();
_logger.LogInformation($"Добавлен набор: {textBoxSetName.Text}");
@ -94,6 +94,10 @@ namespace ProjectElectricLocomotive
MessageBox.Show(ex.Message);
_logger.LogWarning("Не удалось добавить объект");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ButtonRemoveObject_Click(object sender, EventArgs e)
{
@ -109,7 +113,7 @@ namespace ProjectElectricLocomotive
ReloadObjects();
_logger.LogInformation($"Набор '{name}' удален");
}
_logger.LogWarning("Отмена удаления набора");
_logger.LogWarning("Отмена удаления набора");
}
private void buttonRemoveLocomotive_Click(object sender, EventArgs e)
{
@ -157,6 +161,26 @@ namespace ProjectElectricLocomotive
}
pictureBoxCollection.Image = obj.ShowLocomotives();
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByColor());
private void CompareLocomotives(IComparer<DrawingLocomotive?> 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();
}
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives();

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.Entities;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotiveCompareByColor : IComparer<DrawingLocomotive?>
{
public int Compare(DrawingLocomotive? x, DrawingLocomotive? y)
Review

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

Требовалось сортировать по критериям: цвет, скорость, вес
{
if (x == null || x.EntityLocomotive == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityLocomotive == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityLocomotive.BodyColor.Name != y.EntityLocomotive.BodyColor.Name)
{
return x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is DrawingLocomotive)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawingElectricLocomotive)
{
EntityElectricLocomotive entityX = (EntityElectricLocomotive)x.EntityLocomotive;
EntityElectricLocomotive entityY = (EntityElectricLocomotive)y.EntityLocomotive;
if (entityX.AdditionalColor.Name != entityY.AdditionalColor.Name)
{
return entityX.AdditionalColor.Name.CompareTo(entityY.AdditionalColor.Name);
}
}
var speedCompare =
x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight);
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectElectricLocomotive.DrawingObjects;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotiveCompareByType : IComparer<DrawingLocomotive?>
{
public int Compare(DrawingLocomotive? x, DrawingLocomotive? 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 speedCompare = x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight);
}
}
}

View File

@ -0,0 +1,35 @@
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 (Name != other?.Name)
return false;
return true;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
}

View File

@ -17,6 +17,9 @@ namespace ProjectElectricLocomotive.Generics
private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 130;
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public LocomotivesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -31,7 +34,7 @@ namespace ProjectElectricLocomotive.Generics
{
return -1;
}
return collect._collection.Insert(locomotive);
return collect._collection.Insert(locomotive, new DrawingLocomotiveEqutables());
}
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int pos)
{

View File

@ -10,8 +10,8 @@ namespace ProjectElectricLocomotive.Generics
{
internal class LocomotivesGenericStorage
{
readonly Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage;
public List<string> Keys => _locomotivesStorage.Keys.ToList();
readonly Dictionary<LocomotivesCollectionInfo, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage;
public List<LocomotivesCollectionInfo> Keys => _locomotivesStorage.Keys.ToList();
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
@ -22,22 +22,23 @@ namespace ProjectElectricLocomotive.Generics
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight)
{
_locomotivesStorage = new Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>>();
_locomotivesStorage = new Dictionary<LocomotivesCollectionInfo, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (!_locomotivesStorage.ContainsKey(name))
if (!_locomotivesStorage.ContainsKey( new LocomotivesCollectionInfo(name, "")))
{
_locomotivesStorage.Add(name, new LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
_locomotivesStorage.Add(new LocomotivesCollectionInfo(name, ""),
new LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
}
}
public void DelSet(string name)
{
if (_locomotivesStorage.ContainsKey(name))
if (_locomotivesStorage.ContainsKey(new LocomotivesCollectionInfo(name, "")))
{
_locomotivesStorage.Remove(name);
_locomotivesStorage.Remove(new LocomotivesCollectionInfo(name, ""));
}
}
public LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>?
@ -45,9 +46,9 @@ namespace ProjectElectricLocomotive.Generics
{
get
{
if (_locomotivesStorage.ContainsKey(ind))
if (_locomotivesStorage.ContainsKey(new LocomotivesCollectionInfo(ind, "")))
{
return _locomotivesStorage[ind];
return _locomotivesStorage[new LocomotivesCollectionInfo(ind, "")];
}
return null;
}
@ -59,7 +60,7 @@ namespace ProjectElectricLocomotive.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
foreach (KeyValuePair<LocomotivesCollectionInfo, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
{
StringBuilder records = new();
foreach (DrawingLocomotive? elem in record.Value.GetLocomotives)
@ -126,13 +127,13 @@ namespace ProjectElectricLocomotive.Generics
DrawingLocomotive? loco = elem?.CreateDrawningLocomotive(_separatorForObject, _pictureWidth, _pictureHeight);
if (loco != null)
{
if ((collection + loco) == -1) // for my realization it's -1, for eegov's realization it's boolean
if ((collection + loco) == -1)
{
throw new Exception("Ошибка добавления ");
}
}
}
_locomotivesStorage.Add(record[0], collection);
_locomotivesStorage.Add(new LocomotivesCollectionInfo(record[0], string.Empty), collection);
}
return;
}

View File

@ -16,6 +16,8 @@ namespace ProjectElectricLocomotive.Generics
/// Максимальное количество объектов в списке
private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public SetGeneric(int count)
{
_maxCount = count;
@ -23,12 +25,22 @@ namespace ProjectElectricLocomotive.Generics
}
/// Добавление объекта в набор
public int Insert(T loco)
public int Insert(T loco, IEqualityComparer<T?>? equal = null)
{
if (equal != null)
{
foreach (var secondLoco in _places)
{
if (equal.Equals(loco, secondLoco))
{
throw new Exception("Такой объект уже есть в коллекции");
}
}
}
return Insert(loco, 0);
}
public int Insert(T loco, int position)
public int Insert(T loco, int position, IEqualityComparer<T?>? equal = null)
{
if (_places.Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
@ -37,6 +49,18 @@ namespace ProjectElectricLocomotive.Generics
{
return -1;
}
if (equal != null)
{
foreach (var secondLoco in _places)
{
if (equal.Equals(loco, secondLoco))
{
throw new ApplicationException("Такой объект уже есть в коллекции");
}
}
}
_places.Insert(position, loco);
return position;
}