This commit is contained in:
[USERNAME] 2023-12-17 00:11:59 +04:00
parent 9fcdc2cdeb
commit 4eeaa3ae6f
9 changed files with 249 additions and 59 deletions

View File

@ -0,0 +1,49 @@
using Bulldozer.DrawningObjects;
using Bulldozer.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bulldozer.Generics
{
internal class BulldozerCompareByColor : IComparer<DrawningBulldozer?>
{
public int Compare(DrawningBulldozer? x, DrawningBulldozer? y)
{
if (x == null || x.EntityTractor == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityTractor == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityTractor.MainColor.Name != y.EntityTractor.MainColor.Name)
{
return x.EntityTractor.MainColor.Name.CompareTo(y.EntityTractor.MainColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is EntityBulldozer)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawningFastBulldozer)
{
EntityFastBulldozer EntityX = (EntityFastBulldozer)x.EntityTractor;
EntityFastBulldozer EntityY = (EntityFastBulldozer)y.EntityTractor;
if (EntityX.OptionalColor.Name != EntityY.OptionalColor.Name)
{
return EntityX.OptionalColor.Name.CompareTo(EntityY.OptionalColor.Name);
}
}
var speedCompare = x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight);
}
}
}

View File

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

View File

@ -37,6 +37,7 @@ namespace Bulldozer.Generics
/// Набор объектов /// Набор объектов
/// </summary> /// </summary>
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
@ -56,14 +57,14 @@ namespace Bulldozer.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
public static int? operator +(BulldozerGenericCollection<T, U> collect, T? public static bool operator +(BulldozerGenericCollection<T, U> collect, T?
obj) obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect?._collection.Insert(obj); return collect?._collection.Insert(obj, new DrawiningBulldozerEqutables()) ?? false;
} }
/// <summary> /// <summary>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания
@ -71,15 +72,15 @@ namespace Bulldozer.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="pos"></param> /// <param name="pos"></param>
/// <returns></returns> /// <returns></returns>
public static bool operator -(BulldozerGenericCollection<T, U> collect, int public static T? operator -(BulldozerGenericCollection<T, U> collect, int
pos) pos)
{ {
T? obj = collect._collection[pos]; T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
{ {
return collect._collection.Remove(pos); collect._collection.Remove(pos);
} }
return false; return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject

View File

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

View File

@ -16,12 +16,12 @@ namespace Bulldozer.Generics
/// <summary> /// <summary>
/// Словарь (хранилище) /// Словарь (хранилище)
/// </summary> /// </summary>
readonly Dictionary<string, BulldozerGenericCollection<DrawningBulldozer, readonly Dictionary<BulldozersCollectionInfo, BulldozerGenericCollection<DrawningBulldozer,
DrawningObjectBulldozer>> _tractorStorages; DrawningObjectBulldozer>> _tractorStorages;
/// <summary> /// <summary>
/// Возвращение списка названий наборов /// Возвращение списка названий наборов
/// </summary> /// </summary>
public List<string> Keys => _tractorStorages.Keys.ToList(); public List<BulldozersCollectionInfo> Keys => _tractorStorages.Keys.ToList();
/// <summary> /// <summary>
/// Ширина окна отрисовки /// Ширина окна отрисовки
/// </summary> /// </summary>
@ -49,7 +49,7 @@ namespace Bulldozer.Generics
/// <param name="pictureHeight"></param> /// <param name="pictureHeight"></param>
public BulldozersGenericStorage(int pictureWidth, int pictureHeight) public BulldozersGenericStorage(int pictureWidth, int pictureHeight)
{ {
_tractorStorages = new Dictionary<string, _tractorStorages = new Dictionary<BulldozersCollectionInfo,
BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>>(); BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
@ -61,10 +61,10 @@ namespace Bulldozer.Generics
public void AddSet(string name) public void AddSet(string name)
{ {
// TODO Прописать логику для добавления // TODO Прописать логику для добавления
if (!_tractorStorages.ContainsKey(name)) if (!_tractorStorages.ContainsKey(new BulldozersCollectionInfo(name, string.Empty)))
{ {
var tractorCollection = new BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>(_pictureWidth, _pictureHeight); var tractorCollection = new BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>(_pictureWidth, _pictureHeight);
_tractorStorages.Add(name, tractorCollection); _tractorStorages.Add(new BulldozersCollectionInfo(name, string.Empty), tractorCollection);
} }
} }
/// <summary> /// <summary>
@ -74,9 +74,9 @@ namespace Bulldozer.Generics
public void DelSet(string name) public void DelSet(string name)
{ {
// TODO Прописать логику для удаления // TODO Прописать логику для удаления
if (_tractorStorages.ContainsKey(name)) if (_tractorStorages.ContainsKey(new BulldozersCollectionInfo(name, string.Empty)))
{ {
_tractorStorages.Remove(name); _tractorStorages.Remove(new BulldozersCollectionInfo(name, string.Empty));
} }
} }
/// <summary> /// <summary>
@ -88,10 +88,11 @@ namespace Bulldozer.Generics
{ {
get get
{ {
BulldozersCollectionInfo indObj = new BulldozersCollectionInfo(ind, string.Empty);
// TODO Продумать логику получения набора // TODO Продумать логику получения набора
if (_tractorStorages.ContainsKey(ind)) if (_tractorStorages.ContainsKey(indObj))
{ {
return _tractorStorages[ind]; return _tractorStorages[indObj];
} }
return null; return null;
} }
@ -108,7 +109,7 @@ namespace Bulldozer.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>> record in _tractorStorages) foreach (KeyValuePair<BulldozersCollectionInfo, BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>> record in _tractorStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
foreach (DrawningBulldozer? elem in record.Value.GetBulldozer) foreach (DrawningBulldozer? elem in record.Value.GetBulldozer)
@ -181,7 +182,7 @@ namespace Bulldozer.Generics
} }
} }
} }
_tractorStorages.Add(name, collection); _tractorStorages.Add(new BulldozersCollectionInfo(name, string.Empty), collection);
} }
} }
} }

View File

@ -0,0 +1,58 @@
using Bulldozer.DrawningObjects;
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 DrawiningBulldozerEqutables : IEqualityComparer<DrawningBulldozer?>
{
public bool Equals(DrawningBulldozer? x, DrawningBulldozer? y)
{
if (x == null || x.EntityTractor == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityTractor == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityTractor.Speed != y.EntityTractor.Speed)
{
return false;
}
if (x.EntityTractor.Weight != y.EntityTractor.Weight)
{
return false;
}
if (x.EntityTractor.MainColor != y.EntityTractor.MainColor)
{
return false;
}
if (x is EntityFastBulldozer && y is EntityFastBulldozer)
{
EntityFastBulldozer EntityX = (EntityFastBulldozer)x.EntityTractor;
EntityFastBulldozer EntityY = (EntityFastBulldozer)y.EntityTractor;
if (EntityX.OptionalColor != EntityY.OptionalColor)
return false;
if (EntityX.Covsh != EntityY.Covsh)
return false;
if (EntityX.Rearbucket != EntityY.Rearbucket)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningBulldozer obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -45,6 +45,8 @@
LoadToolStripMenuItem = new ToolStripMenuItem(); LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog(); openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog(); saveFileDialog = new SaveFileDialog();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout(); groupBox1.SuspendLayout();
groupBox2.SuspendLayout(); groupBox2.SuspendLayout();
@ -53,7 +55,7 @@
// //
// ButtonAddBulldozer // ButtonAddBulldozer
// //
ButtonAddBulldozer.Location = new Point(26, 314); ButtonAddBulldozer.Location = new Point(27, 373);
ButtonAddBulldozer.Name = "ButtonAddBulldozer"; ButtonAddBulldozer.Name = "ButtonAddBulldozer";
ButtonAddBulldozer.Size = new Size(166, 40); ButtonAddBulldozer.Size = new Size(166, 40);
ButtonAddBulldozer.TabIndex = 0; ButtonAddBulldozer.TabIndex = 0;
@ -63,7 +65,7 @@
// //
// ButtonRemoveBulldozer // ButtonRemoveBulldozer
// //
ButtonRemoveBulldozer.Location = new Point(26, 400); ButtonRemoveBulldozer.Location = new Point(27, 459);
ButtonRemoveBulldozer.Name = "ButtonRemoveBulldozer"; ButtonRemoveBulldozer.Name = "ButtonRemoveBulldozer";
ButtonRemoveBulldozer.Size = new Size(166, 39); ButtonRemoveBulldozer.Size = new Size(166, 39);
ButtonRemoveBulldozer.TabIndex = 1; ButtonRemoveBulldozer.TabIndex = 1;
@ -73,7 +75,7 @@
// //
// ButtonRefreshCollection // ButtonRefreshCollection
// //
ButtonRefreshCollection.Location = new Point(26, 478); ButtonRefreshCollection.Location = new Point(26, 505);
ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(166, 40); ButtonRefreshCollection.Size = new Size(166, 40);
ButtonRefreshCollection.TabIndex = 2; ButtonRefreshCollection.TabIndex = 2;
@ -93,7 +95,7 @@
// maskedTextBoxNumber // maskedTextBoxNumber
// //
maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point); maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point);
maskedTextBoxNumber.Location = new Point(56, 371); maskedTextBoxNumber.Location = new Point(57, 430);
maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 22); maskedTextBoxNumber.Size = new Size(100, 22);
@ -102,14 +104,16 @@
// //
// groupBox1 // groupBox1
// //
groupBox1.Controls.Add(ButtonSortByColor);
groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(ButtonSortByType);
groupBox1.Controls.Add(ButtonAddBulldozer); groupBox1.Controls.Add(ButtonAddBulldozer);
groupBox1.Controls.Add(ButtonRefreshCollection); groupBox1.Controls.Add(ButtonRefreshCollection);
groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(ButtonRemoveBulldozer); groupBox1.Controls.Add(ButtonRemoveBulldozer);
groupBox1.Location = new Point(643, 30); groupBox1.Location = new Point(643, 30);
groupBox1.Name = "groupBox1"; groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(209, 533); groupBox1.Size = new Size(209, 552);
groupBox1.TabIndex = 5; groupBox1.TabIndex = 5;
groupBox1.TabStop = false; groupBox1.TabStop = false;
groupBox1.Text = "Инструменты"; groupBox1.Text = "Инструменты";
@ -170,7 +174,7 @@
menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem });
menuStrip.Location = new Point(649, 3); menuStrip.Location = new Point(649, 3);
menuStrip.Name = "menuStrip"; menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(176, 24); menuStrip.Size = new Size(56, 24);
menuStrip.TabIndex = 6; menuStrip.TabIndex = 6;
menuStrip.Text = "menuStrip1"; menuStrip.Text = "menuStrip1";
// //
@ -184,14 +188,14 @@
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(180, 22); SaveToolStripMenuItem.Size = new Size(133, 22);
SaveToolStripMenuItem.Text = "Сохранить"; SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22); LoadToolStripMenuItem.Size = new Size(133, 22);
LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
// //
@ -206,6 +210,26 @@
saveFileDialog.Filter = "txt file | *.txt"; saveFileDialog.Filter = "txt file | *.txt";
saveFileDialog.Title = "Выберите текстовый файл"; saveFileDialog.Title = "Выберите текстовый файл";
// //
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(26, 342);
ButtonSortByColor.Margin = new Padding(3, 2, 3, 2);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(166, 28);
ButtonSortByColor.TabIndex = 10;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(27, 310);
ButtonSortByType.Margin = new Padding(3, 2, 3, 2);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(166, 28);
ButtonSortByType.TabIndex = 9;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
//
// FormBulldozerCollection // FormBulldozerCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
@ -247,5 +271,7 @@
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
} }
} }

View File

@ -250,6 +250,23 @@ namespace Bulldozer
} }
} }
} }
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByColor());
private void CompareBulldozers(IComparer<DrawningBulldozer?> comparer)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowBulldozer();
}
} }
} }

View File

@ -30,36 +30,16 @@ namespace Bulldozer.Generics
_maxCount = count; _maxCount = count;
_places = new List<T?>(count); _places = new List<T?>(count);
} }
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="tractor">Добавляемая установка</param> /// <param name="tractor">Добавляемая установка</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T tractor) public bool Insert(T tractor, IEqualityComparer<T>? equal = null)
{ {
if (_places.Count == 0) Insert(tractor, 0, equal);
{ return true;
_places.Add(tractor);
return 0;
}
else
{
if (_places.Count < _maxCount)
{
_places.Add(tractor);
for (int i = 0; i < _places.Count; i++)
{
T temp = _places[i];
_places[i] = _places[_places.Count - 1];
_places[_places.Count - 1] = temp;
}
return 0;
}
else
{
throw new StorageOverflowException(_places.Count);
}
}
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -67,7 +47,7 @@ namespace Bulldozer.Generics
/// <param name="tractor">Добавляемая установкаь</param> /// <param name="tractor">Добавляемая установкаь</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public bool Insert(T tractor, int position) public bool Insert(T tractor, int position, IEqualityComparer<T>? equal = null)
{ {
// TODO проверка позиции // TODO проверка позиции
if (position < 0 || position >= _maxCount) if (position < 0 || position >= _maxCount)
@ -77,9 +57,13 @@ namespace Bulldozer.Generics
} }
if (Count >= _maxCount) if (Count >= _maxCount)
throw new StorageOverflowException(position); throw new StorageOverflowException(position);
_places.Insert(0, tractor); if (equal != null)
{
if (_places.Contains(tractor, equal))
throw new ArgumentException(nameof(tractor));
}
_places.Insert(position, tractor);
return true; return true;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -90,13 +74,8 @@ namespace Bulldozer.Generics
{ {
// TODO проверка позиции // TODO проверка позиции
// Проверка позиции // Проверка позиции
if (position < 0 || position > _maxCount || position >= Count) if ((position < 0) || (position > _maxCount) || (position >= Count)) throw new BulldozerNotFoundException(position);
throw new BulldozerNotFoundException(); _places.RemoveAt(position);
if (_places[position] == null)
{
throw new BulldozerNotFoundException();
}
_places[position] = null;
return true; return true;
} }
/// <summary> /// <summary>