PIbd - 21 Bakalskaya E.D. LabWork08 Base #10

Closed
ekallin wants to merge 6 commits from LabWork08 into LabWork07
10 changed files with 280 additions and 48 deletions

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectElectricLocomotive.DrawingObjects;
using System.Diagnostics.CodeAnalysis;
using ProjectElectricLocomotive.Entities;
namespace ProjectElectricLocomotive.Generics
{
internal class DrawingLocoEqutables : 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;
// to do logic for "сравнения" additional parameters :)
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).Horns != (y.EntityLocomotive as EntityElectricLocomotive).Horns)
{
return false;
}
if ((x.EntityLocomotive as EntityElectricLocomotive).SeifBatteries != (y.EntityLocomotive as EntityElectricLocomotive).SeifBatteries)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawingLocomotive obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -42,6 +42,8 @@
ButtonAddObject = new Button();
ButtonRemoveObject = new Button();
Instruments = new GroupBox();
buttonSortByType = new Button();
buttonSortByColor = new Button();
menuStrip1 = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
@ -59,17 +61,18 @@
// maskedTextBoxNumber
//
maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
maskedTextBoxNumber.Location = new Point(38, 342);
maskedTextBoxNumber.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point);
maskedTextBoxNumber.Location = new Point(38, 417);
maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(156, 27);
maskedTextBoxNumber.Size = new Size(156, 26);
maskedTextBoxNumber.TabIndex = 4;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
ButtonRefreshCollection.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point);
ButtonRefreshCollection.Location = new Point(38, 412);
ButtonRefreshCollection.Location = new Point(38, 518);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(150, 29);
ButtonRefreshCollection.TabIndex = 2;
@ -80,7 +83,7 @@
//
ButtonRemoveLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
ButtonRemoveLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point);
ButtonRemoveLocomotive.Location = new Point(37, 375);
ButtonRemoveLocomotive.Location = new Point(38, 481);
ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive";
ButtonRemoveLocomotive.Size = new Size(150, 31);
ButtonRemoveLocomotive.TabIndex = 1;
@ -92,7 +95,7 @@
//
ButtonAddLocomotive.Anchor = AnchorStyles.Top;
ButtonAddLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point);
ButtonAddLocomotive.Location = new Point(38, 306);
ButtonAddLocomotive.Location = new Point(38, 381);
ButtonAddLocomotive.Name = "ButtonAddLocomotive";
ButtonAddLocomotive.Size = new Size(150, 30);
ButtonAddLocomotive.TabIndex = 0;
@ -103,32 +106,30 @@
// pictureBoxCollections
//
pictureBoxCollections.Anchor = AnchorStyles.Left;
pictureBoxCollections.Location = new Point(-1, 31);
pictureBoxCollections.Location = new Point(0, 31);
pictureBoxCollections.Name = "pictureBoxCollections";
pictureBoxCollections.Size = new Size(303, 409);
pictureBoxCollections.Size = new Size(303, 523);
pictureBoxCollections.TabIndex = 1;
pictureBoxCollections.TabStop = false;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(31, 28);
textBoxStorageName.Margin = new Padding(3, 4, 3, 4);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(149, 27);
textBoxStorageName.TabIndex = 5;
//
// groupBox1
//
groupBox1.Anchor = AnchorStyles.None;
groupBox1.Controls.Add(listBoxStorage);
groupBox1.Controls.Add(ButtonAddObject);
groupBox1.Controls.Add(ButtonRemoveObject);
groupBox1.Controls.Add(textBoxStorageName);
groupBox1.Location = new Point(7, 29);
groupBox1.Margin = new Padding(3, 4, 3, 4);
groupBox1.Location = new Point(7, 18);
//groupBox1.Margin = new Padding(3, 4, 3, 4);
groupBox1.Name = "groupBox1";
groupBox1.Padding = new Padding(3, 4, 3, 4);
groupBox1.Size = new Size(216, 270);
//groupBox1.Padding = new Padding(3, 4, 3, 4);
groupBox1.Size = new Size(216, 280);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "Наборы";
@ -160,7 +161,7 @@
//
ButtonRemoveObject.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
ButtonRemoveObject.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point);
ButtonRemoveObject.Location = new Point(31, 228);
ButtonRemoveObject.Location = new Point(31, 238);
ButtonRemoveObject.Name = "ButtonRemoveObject";
ButtonRemoveObject.Size = new Size(149, 31);
ButtonRemoveObject.TabIndex = 8;
@ -171,20 +172,46 @@
// Instruments
//
Instruments.Anchor = AnchorStyles.Right;
Instruments.Controls.Add(buttonSortByType);
Instruments.Controls.Add(buttonSortByColor);
Instruments.Controls.Add(ButtonRefreshCollection);
Instruments.Controls.Add(groupBox1);
Instruments.Controls.Add(maskedTextBoxNumber);
Instruments.Controls.Add(ButtonAddLocomotive);
Instruments.Controls.Add(ButtonRemoveLocomotive);
Instruments.Location = new Point(302, -1);
Instruments.Location = new Point(302, 0);
Instruments.Margin = new Padding(3, 4, 3, 4);
Instruments.Name = "Instruments";
Instruments.Padding = new Padding(3, 4, 3, 4);
Instruments.Size = new Size(236, 448);
Instruments.Size = new Size(236, 554);
Instruments.TabIndex = 6;
Instruments.TabStop = false;
Instruments.Text = "Инструменты";
//
// buttonSortByType
//
buttonSortByType.Anchor = AnchorStyles.Top;
buttonSortByType.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point);
buttonSortByType.Location = new Point(44, 305);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(150, 30);
buttonSortByType.TabIndex = 7;
buttonSortByType.Text = "Сортировать по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// buttonSortByColor
//
buttonSortByColor.Anchor = AnchorStyles.Top;
buttonSortByColor.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point);
buttonSortByColor.Location = new Point(25, 341);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(181, 30);
buttonSortByColor.TabIndex = 6;
buttonSortByColor.Text = "Сортировать по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// menuStrip1
//
menuStrip1.ImageScalingSize = new Size(20, 20);
@ -229,7 +256,7 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(538, 443);
ClientSize = new Size(538, 554);
Controls.Add(Instruments);
Controls.Add(pictureBoxCollections);
Controls.Add(menuStrip1);
@ -249,6 +276,11 @@
PerformLayout();
}
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
#endregion
private Button ButtonRefreshCollection;
private Button ButtonRemoveLocomotive;
@ -269,5 +301,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -1,18 +1,7 @@
using Microsoft.Extensions.Logging;
//using ElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.Exceptions;
using ProjectElectricLocomotive.Generics;
/*using ProjectElectricLocomotive.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 ProjectElectricLocomotive
@ -61,7 +50,7 @@ namespace ProjectElectricLocomotive
_logger.LogWarning("Неудачная попытка. Коллекция не добавлена, не все данные заполнены");
return;
}
_storage.AddSet(textBoxStorageName.Text);
_storage.AddSet(textBoxStorageName.Text.ToString());
ReloadObjects();
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
@ -115,16 +104,21 @@ namespace ProjectElectricLocomotive
MessageBox.Show("Объект добавлен");
pictureBoxCollections.Image = obj.ShowLocomotives();
_logger.LogInformation($"Добавлен объект {obj}");
; }
}
else
{
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning("Переполнение коллекции");
}
}
catch (StorageOverflowException ex)
{
MessageBox.Show(ex.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
//проверяем, удалось ли нам загрузить объект
}
@ -219,5 +213,20 @@ namespace ProjectElectricLocomotive
}
}
}
private void buttonSortByType_Click(object sender, EventArgs e) => CompareLocos(new LocoCompareByType());
private void buttonSortByColor_Click(object sender, EventArgs e) => CompareLocos(new LocoCompareByColor());
public void CompareLocos(IComparer<DrawingLocomotive?> comparer)
{
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if(obj == null) return;
obj.Sort(comparer);
pictureBoxCollections.Image = obj.ShowLocomotives();
}
}
}

View File

@ -157,7 +157,7 @@ namespace ProjectElectricLocomotive
}
DrawLoco();
}
private void LabelColor_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))

View File

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

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

Требовалось сортировать по критериям: цвет, скорость, вес
{
if (x == null || x.EntityLocomotive == null)
throw new NotImplementedException(nameof(x));
if (y == null || y.EntityLocomotive == null)
throw new NotImplementedException(nameof(y));
var bodyColor = x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name);
if (bodyColor != 0) return bodyColor;
if(x.EntityLocomotive is EntityElectricLocomotive &&
y.EntityLocomotive is EntityElectricLocomotive)
{
var addcolor = (x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name.CompareTo((y.EntityLocomotive
as EntityElectricLocomotive).AdditionalColor.Name);
if(addcolor != 0) return addcolor;
}
return 1;
}
}
}

View File

@ -0,0 +1,34 @@
using ProjectElectricLocomotive.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive
{
internal class LocoCompareByType : 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

@ -15,6 +15,8 @@ namespace ProjectElectricLocomotive.Generics
/// </summary>
public IEnumerable<T?> GetLocomotives => _collection.GetLocomotives();
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
//ширина/высота окна
private readonly int _pictureWidth;
@ -44,7 +46,7 @@ namespace ProjectElectricLocomotive.Generics
{
return -1;
}
return collect._collection.Insert(loco);
return collect._collection.Insert(loco, new DrawingLocoEqutables());
}
/// Перегрузка оператора вычитания

View File

@ -3,24 +3,24 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.MovementStrategy;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotiveGenericStorage
internal class LocomotiveGenericStorage
{
//create lab 7
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage;
readonly Dictionary<LocosCollectionInfo, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _locomotivesStorage.Keys.ToList();
public List<LocosCollectionInfo> Keys => _locomotivesStorage.Keys.ToList();
private readonly int _pictureWidth;
@ -34,7 +34,7 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="pictureHeight"></param>
public LocomotiveGenericStorage(int pictureWidth, int pictureHeight)
{
_locomotivesStorage = new Dictionary<string, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>>();
_locomotivesStorage = new Dictionary<LocosCollectionInfo, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -44,9 +44,10 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_locomotivesStorage.ContainsKey(name))
if (!_locomotivesStorage.ContainsKey(new LocosCollectionInfo(name, "")))
{
_locomotivesStorage.Add(name, new LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
_locomotivesStorage.Add(new LocosCollectionInfo(name, ""),
new LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
}
}
@ -56,9 +57,9 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_locomotivesStorage.ContainsKey(name))
if (_locomotivesStorage.ContainsKey(new LocosCollectionInfo(name, "")))
{
_locomotivesStorage.Remove(name);
_locomotivesStorage.Remove(new LocosCollectionInfo(name, ""));
}
}
@ -73,9 +74,9 @@ namespace ProjectElectricLocomotive.Generics
{
get
{
if (_locomotivesStorage.ContainsKey(ind))
if (_locomotivesStorage.ContainsKey(new LocosCollectionInfo(ind, "")))
{
return _locomotivesStorage[ind];
return _locomotivesStorage[new LocosCollectionInfo(ind, "")];
}
return null;
}
@ -106,7 +107,7 @@ namespace ProjectElectricLocomotive.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
foreach (KeyValuePair<LocosCollectionInfo, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
{
StringBuilder records = new();
foreach (DrawingLocomotive? elem in record.Value.GetLocomotives)
@ -173,7 +174,7 @@ namespace ProjectElectricLocomotive.Generics
}
LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
foreach (string elem in set/*.Reverse()*/)
{
DrawingLocomotive? loco = elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight);
if (loco != null)
@ -184,7 +185,8 @@ namespace ProjectElectricLocomotive.Generics
}
}
}
_locomotivesStorage.Add(record[0], collection);
_locomotivesStorage.Add(new LocosCollectionInfo(record[0], string.Empty), collection);
}
return;
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive
{
public class LocosCollectionInfo : IEquatable<LocosCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public LocosCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(LocosCollectionInfo other)
{
if (Name != other?.Name)
//throw new NotImplementedException(nameof(Name));
Review

Закомментированного кода быть не должно

Закомментированного кода быть не должно
return false;
return true;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
}

View File

@ -16,6 +16,9 @@ namespace ProjectElectricLocomotive.Generics
/// Максимальное количество объектов в списке
private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) =>_places.Sort(comparer);
public SetGeneric(int count)
{
_maxCount = count;
@ -23,12 +26,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);
@ -38,6 +51,17 @@ 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;
}