WIP: ПИбд-23 Волков Никита Андреевич Лабораторная работа №8 #10

Closed
bocchanskyy wants to merge 3 commits from Lab8 into Lab7
12 changed files with 335 additions and 31 deletions

View File

@ -2,7 +2,7 @@
namespace Bulldozer.DrawingObjects
{
public static class ExtentionDrawingBulldozer
public static class ExtensionDrawingBulldozer
{
public static DrawingBulldozer? CreateDrawingBulldozer(this string info, char separatorForObject, int width, int height)
{

View File

@ -45,6 +45,8 @@
this.toolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.ButtonSortByType = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panelTools.SuspendLayout();
this.groupListBulldozersBox.SuspendLayout();
@ -65,6 +67,8 @@
// panelTools
//
this.panelTools.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.panelTools.Controls.Add(this.ButtonSortByType);
this.panelTools.Controls.Add(this.ButtonSortByColor);
this.panelTools.Controls.Add(this.groupListBulldozersBox);
this.panelTools.Controls.Add(this.textBoxDeletingBulldozer);
this.panelTools.Controls.Add(this.buttonUpdateColletion);
@ -132,7 +136,7 @@
//
// textBoxDeletingBulldozer
//
this.textBoxDeletingBulldozer.Location = new System.Drawing.Point(25, 525);
this.textBoxDeletingBulldozer.Location = new System.Drawing.Point(22, 525);
this.textBoxDeletingBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBoxDeletingBulldozer.Name = "textBoxDeletingBulldozer";
this.textBoxDeletingBulldozer.Size = new System.Drawing.Size(220, 27);
@ -140,7 +144,7 @@
//
// buttonUpdateColletion
//
this.buttonUpdateColletion.Location = new System.Drawing.Point(25, 692);
this.buttonUpdateColletion.Location = new System.Drawing.Point(22, 693);
this.buttonUpdateColletion.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonUpdateColletion.Name = "buttonUpdateColletion";
this.buttonUpdateColletion.Size = new System.Drawing.Size(221, 53);
@ -150,7 +154,7 @@
//
// buttonDeleteBulldozer
//
this.buttonDeleteBulldozer.Location = new System.Drawing.Point(25, 563);
this.buttonDeleteBulldozer.Location = new System.Drawing.Point(22, 560);
this.buttonDeleteBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonDeleteBulldozer.Name = "buttonDeleteBulldozer";
this.buttonDeleteBulldozer.Size = new System.Drawing.Size(221, 53);
@ -161,7 +165,7 @@
//
// buttonAddBulldozer
//
this.buttonAddBulldozer.Location = new System.Drawing.Point(24, 464);
this.buttonAddBulldozer.Location = new System.Drawing.Point(22, 464);
this.buttonAddBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonAddBulldozer.Name = "buttonAddBulldozer";
this.buttonAddBulldozer.Size = new System.Drawing.Size(221, 53);
@ -202,17 +206,39 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(166, 26);
this.SaveToolStripMenuItem.Text = "Сохранить";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(166, 26);
this.LoadToolStripMenuItem.Text = "Загрузить";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(22, 322);
this.ButtonSortByColor.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(221, 53);
this.ButtonSortByColor.TabIndex = 9;
this.ButtonSortByColor.Text = "Сортировка по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(22, 383);
this.ButtonSortByType.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(221, 53);
this.ButtonSortByType.TabIndex = 10;
this.ButtonSortByType.Text = "Сортировка по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// FormBulldozerCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
@ -255,5 +281,7 @@
private ToolStripMenuItem toolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -127,10 +127,17 @@ namespace Bulldozer
MessageBox.Show("Объект добавлен");
_logger.LogInformation("Объект добавлен");
pictureBoxCollection.Image = obj.ShowBulldozers();
} catch(Exception ex) {
}
catch (StorageOverflowException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Объект не добавлен в набор {listBoxBulldozerStorages.SelectedItem.ToString()}");
}
catch (ArgumentException ex)
{
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
_logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxBulldozerStorages.SelectedItem.ToString()}");
}
}
@ -243,5 +250,38 @@ namespace Bulldozer
}
}
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByType());
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByColor());
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void CompareBulldozers(IComparer<DrawingBulldozer?> comparer)
{
if (listBoxBulldozerStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowBulldozers();
}
}
}

View File

@ -125,6 +125,7 @@
this.labelModifiedObject.TabIndex = 8;
this.labelModifiedObject.Text = "Продвинутый";
this.labelModifiedObject.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.labelModifiedObject.MouseDown += labelObject_MouseDown;
//
// labelSimpleObject
//
@ -137,6 +138,7 @@
this.labelSimpleObject.TabIndex = 7;
this.labelSimpleObject.Text = "Простой";
this.labelSimpleObject.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.labelSimpleObject.MouseDown += labelObject_MouseDown;
//
// groupBoxColors
//
@ -156,6 +158,8 @@
this.groupBoxColors.TabIndex = 6;
this.groupBoxColors.TabStop = false;
this.groupBoxColors.Text = "Цвета";
this.groupBoxColors.DragDrop += PanelObject_DragDrop;
this.groupBoxColors.DragEnter += PanelObject_DragEnter;
//
// panelPurple
//
@ -165,6 +169,7 @@
this.panelPurple.Name = "panelPurple";
this.panelPurple.Size = new System.Drawing.Size(43, 43);
this.panelPurple.TabIndex = 1;
this.panelPurple.MouseDown += panelColor_MouseDown;
//
// panelBlack
//
@ -174,6 +179,7 @@
this.panelBlack.Name = "panelBlack";
this.panelBlack.Size = new System.Drawing.Size(43, 43);
this.panelBlack.TabIndex = 1;
this.panelBlack.MouseDown += panelColor_MouseDown;
//
// panelGray
//
@ -183,6 +189,7 @@
this.panelGray.Name = "panelGray";
this.panelGray.Size = new System.Drawing.Size(43, 43);
this.panelGray.TabIndex = 1;
this.panelGray.MouseDown += panelColor_MouseDown;
//
// panelWhite
//
@ -192,6 +199,7 @@
this.panelWhite.Name = "panelWhite";
this.panelWhite.Size = new System.Drawing.Size(43, 43);
this.panelWhite.TabIndex = 1;
this.panelWhite.MouseDown += panelColor_MouseDown;
//
// panelYellow
//
@ -201,6 +209,7 @@
this.panelYellow.Name = "panelYellow";
this.panelYellow.Size = new System.Drawing.Size(43, 43);
this.panelYellow.TabIndex = 1;
this.panelYellow.MouseDown += panelColor_MouseDown;
//
// panelBlue
//
@ -210,6 +219,7 @@
this.panelBlue.Name = "panelBlue";
this.panelBlue.Size = new System.Drawing.Size(43, 43);
this.panelBlue.TabIndex = 1;
this.panelBlue.MouseDown += panelColor_MouseDown;
//
// panelGreen
//
@ -219,6 +229,7 @@
this.panelGreen.Name = "panelGreen";
this.panelGreen.Size = new System.Drawing.Size(43, 43);
this.panelGreen.TabIndex = 1;
this.panelGreen.MouseDown += panelColor_MouseDown;
//
// panelRed
//
@ -228,6 +239,7 @@
this.panelRed.Name = "panelRed";
this.panelRed.Size = new System.Drawing.Size(43, 43);
this.panelRed.TabIndex = 0;
this.panelRed.MouseDown += panelColor_MouseDown;
//
// numericUpDownWeight
//
@ -315,6 +327,8 @@
this.panelObject.Name = "panelObject";
this.panelObject.Size = new System.Drawing.Size(334, 202);
this.panelObject.TabIndex = 2;
this.panelObject.DragDrop += PanelObject_DragDrop;
this.panelObject.DragEnter += PanelObject_DragEnter;
//
// labelAddColor
//
@ -327,6 +341,8 @@
this.labelAddColor.TabIndex = 3;
this.labelAddColor.Text = "Дополн. цвет";
this.labelAddColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.labelAddColor.DragDrop += labelColor_dragDrop;
this.labelAddColor.DragEnter += labelColor_dragEnter;
//
// labelColor
//
@ -339,6 +355,8 @@
this.labelColor.TabIndex = 2;
this.labelColor.Text = "Основной цвет";
this.labelColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.labelColor.DragDrop += labelColor_dragDrop;
this.labelColor.DragEnter += labelColor_dragEnter;
//
// buttonAdd
//
@ -349,6 +367,7 @@
this.buttonAdd.TabIndex = 3;
this.buttonAdd.Text = "Добавить";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += buttonAdd_Click;
//
// buttonCancel
//

View File

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

View File

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

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

Требовалось сортировать по критериям: цвет, скорость, вес
{
if (x == null && y == null)
{
// Оба объекта равны
return 0;
}
if (x == null && y != null)
{
// Объект x меньше
return 1;
}
if (x != null && y == null)
{
// Объект x больше
return -1;
}
var xBulldozer = x as DrawingBulldozer;
Review

Зачем, они и так нужного типа

Зачем, они и так нужного типа
var yBulldozer = y as DrawingBulldozer;
if (xBulldozer == null && yBulldozer == null)
{
return 0;
}
if (xBulldozer == null && yBulldozer != null)
{
return 1;
}
if (xBulldozer != null && yBulldozer == null)
{
return -1;
}
if (xBulldozer.EntityBulldozer.BodyColor == yBulldozer.EntityBulldozer.BodyColor)
{
// Объекты равны по цвету
return 0;
}
if (xBulldozer.EntityBulldozer.BodyColor.R.CompareTo(yBulldozer.EntityBulldozer.BodyColor.R) == 0)
{
if (xBulldozer.EntityBulldozer.BodyColor.G.CompareTo(yBulldozer.EntityBulldozer.BodyColor.G) == 0)
{
// Сравнение по компоненте B, если R и G совпадают
return xBulldozer.EntityBulldozer.BodyColor.B.CompareTo(yBulldozer.EntityBulldozer.BodyColor.B);
}
else
{
// Сравнение по компоненте G, если R совпадает, но G не совпадает
return xBulldozer.EntityBulldozer.BodyColor.G.CompareTo(yBulldozer.EntityBulldozer.BodyColor.G);
}
}
else
{
// Сравнение по компоненте R, если R не совпадает
return xBulldozer.EntityBulldozer.BodyColor.R.CompareTo(yBulldozer.EntityBulldozer.BodyColor.R);
}
}
}
}

View File

@ -0,0 +1,32 @@
using Bulldozer.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bulldozer.Generics
{
internal class BulldozerCompareByType : IComparer<DrawingBulldozer?>
{
public int Compare(DrawingBulldozer? x, DrawingBulldozer? y)
{
if (x == null || x.EntityBulldozer == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityBulldozer == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityBulldozer.Speed.CompareTo(y.EntityBulldozer.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityBulldozer.Weight.CompareTo(y.EntityBulldozer.Weight);
}
}
}

View File

@ -7,6 +7,12 @@ namespace Bulldozer.Generics
where T : DrawingBulldozer
where U : IMoveableObject
{
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
/// Получение объектов коллекции
/// </summary>
@ -54,7 +60,7 @@ namespace Bulldozer.Generics
{
if (obj == null)
return false;
return collect._collection.Insert(obj);
return collect?._collection.Insert(obj, new DrawingBulldozerEqutables()) ?? false;
}
/// <summary>

View File

@ -3,6 +3,7 @@ using Bulldozer.MovementStrategy;
using System.Text;
using Bulldozer.Exceptions;
using System.Numerics;
using Microsoft.AspNetCore.Http;
namespace Bulldozer.Generics
{
@ -14,11 +15,11 @@ namespace Bulldozer.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>> _bulldozerStorages;
readonly Dictionary<BulldozerCollectionInfo, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>> _bulldozerStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _bulldozerStorages.Keys.ToList();
public List<BulldozerCollectionInfo> Keys => _bulldozerStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -34,7 +35,7 @@ namespace Bulldozer.Generics
/// <param name="pictureHeight"></param>
public BulldozersGenericStorage(int pictureWidth, int pictureHeight)
{
_bulldozerStorages = new Dictionary<string, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>>();
_bulldozerStorages = new Dictionary<BulldozerCollectionInfo, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -44,9 +45,9 @@ namespace Bulldozer.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_bulldozerStorages.ContainsKey(name))
if (!_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(name, string.Empty)))
{
_bulldozerStorages.Add(name, new BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>(_pictureWidth, _pictureHeight));
_bulldozerStorages.Add(new BulldozerCollectionInfo(name, string.Empty), new BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>(_pictureWidth, _pictureHeight));
}
}
@ -58,9 +59,9 @@ namespace Bulldozer.Generics
public void DelSet(string name)
{
// TODO: Прописать логику для удаления набора
if (_bulldozerStorages.ContainsKey(name))
if (_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(name, string.Empty)))
{
_bulldozerStorages.Remove(name);
_bulldozerStorages.Remove(new BulldozerCollectionInfo(name, string.Empty));
}
}
@ -73,14 +74,11 @@ namespace Bulldozer.Generics
{
get
{
if (_bulldozerStorages.ContainsKey(ind))
if (_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(ind, string.Empty)))
{
return _bulldozerStorages[ind];
}
else
{
return null;
return _bulldozerStorages[new BulldozerCollectionInfo(ind, string.Empty)];
}
return null;
}
}
/// Разделитель для записи ключа и значения элемента словаря
@ -107,7 +105,7 @@ namespace Bulldozer.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>> record in _bulldozerStorages)
foreach (KeyValuePair<BulldozerCollectionInfo, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>> record in _bulldozerStorages)
{
StringBuilder records = new();
foreach (DrawingBulldozer? elem in record.Value.GetBulldozers)
@ -189,7 +187,7 @@ namespace Bulldozer.Generics
}
}
}
_bulldozerStorages.Add(record[0], collection);
_bulldozerStorages.Add(new BulldozerCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);

View File

@ -0,0 +1,59 @@
using Bulldozer.DrawingObjects;
using Bulldozer.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bulldozer.Generics
{
internal class DrawingBulldozerEqutables : IEqualityComparer<DrawingBulldozer?>
{
public bool Equals(DrawingBulldozer? x, DrawingBulldozer? y)
{
if (x == null || x.EntityBulldozer == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityBulldozer == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityBulldozer.Speed != y.EntityBulldozer.Speed)
{
return false;
}
if (x.EntityBulldozer.Weight != y.EntityBulldozer.Weight)
{
return false;
}
if (x.EntityBulldozer.BodyColor != y.EntityBulldozer.BodyColor)
{
return false;
}
if (x is DrawingBulldozerUpgraded && y is DrawingBulldozerUpgraded)
{
EntityBulldozerUpgraded EntityX = (EntityBulldozerUpgraded)x.EntityBulldozer;
EntityBulldozerUpgraded EntityY = (EntityBulldozerUpgraded)y.EntityBulldozer;
if (EntityX.Blade != EntityY.Blade)
return false;
if (EntityX.Ripper != EntityY.Ripper)
return false;
if (EntityX.DopColor != EntityY.DopColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawingBulldozer obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -23,7 +23,11 @@ namespace Bulldozer.Generics
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Конструктор
/// </summary>
@ -39,9 +43,12 @@ namespace Bulldozer.Generics
/// </summary>
/// <param name="bulldozer">Добавляемый бульдозер</param>
/// <returns></returns>
public bool Insert(T bulldozer)
public bool Insert(T bulldozer, IEqualityComparer<T?>? equal = null)
{
return Insert(bulldozer, 0);
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(bulldozer, 0, equal);
return true;
}
/// <summary>
@ -49,12 +56,21 @@ namespace Bulldozer.Generics
/// </summary>
/// <param name="bulldozer">Добавляемый бульдозер</param>
/// <returns></returns>
public bool Insert(T bulldozer , int position)
public bool Insert(T bulldozer , int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount) {
throw new StorageOverflowException("Вставка невозможна.");
if (_places.Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (position < 0 || position >= _maxCount)
{
return false;
}
if (equal != null)
{
if (_places.Contains(bulldozer, equal))
throw new ArgumentException(nameof(bulldozer));
}
if (Count >= _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(position, bulldozer);
return true;
}