PIbd-21. Afanasev S.S. Lab work 08 #9

Closed
Stepan wants to merge 1 commits from Lab8 into Lab7
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)
Review

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

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

Нет проверки, что other не равен null

Нет проверки, что other не равен null
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -16,12 +16,12 @@ namespace Bulldozer.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BulldozerGenericCollection<DrawningBulldozer,
readonly Dictionary<BulldozersCollectionInfo, BulldozerGenericCollection<DrawningBulldozer,
DrawningObjectBulldozer>> _tractorStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _tractorStorages.Keys.ToList();
public List<BulldozersCollectionInfo> Keys => _tractorStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -49,7 +49,7 @@ namespace Bulldozer.Generics
/// <param name="pictureHeight"></param>
public BulldozersGenericStorage(int pictureWidth, int pictureHeight)
{
_tractorStorages = new Dictionary<string,
_tractorStorages = new Dictionary<BulldozersCollectionInfo,
BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
@ -61,10 +61,10 @@ namespace Bulldozer.Generics
public void AddSet(string name)
{
// TODO Прописать логику для добавления
if (!_tractorStorages.ContainsKey(name))
if (!_tractorStorages.ContainsKey(new BulldozersCollectionInfo(name, string.Empty)))
{
var tractorCollection = new BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>(_pictureWidth, _pictureHeight);
_tractorStorages.Add(name, tractorCollection);
_tractorStorages.Add(new BulldozersCollectionInfo(name, string.Empty), tractorCollection);
}
}
/// <summary>
@ -74,9 +74,9 @@ namespace Bulldozer.Generics
public void DelSet(string name)
{
// TODO Прописать логику для удаления
if (_tractorStorages.ContainsKey(name))
if (_tractorStorages.ContainsKey(new BulldozersCollectionInfo(name, string.Empty)))
{
_tractorStorages.Remove(name);
_tractorStorages.Remove(new BulldozersCollectionInfo(name, string.Empty));
}
}
/// <summary>
@ -88,10 +88,11 @@ namespace Bulldozer.Generics
{
get
{
BulldozersCollectionInfo indObj = new BulldozersCollectionInfo(ind, string.Empty);
// TODO Продумать логику получения набора
if (_tractorStorages.ContainsKey(ind))
if (_tractorStorages.ContainsKey(indObj))
{
return _tractorStorages[ind];
return _tractorStorages[indObj];
}
return null;
}
@ -108,7 +109,7 @@ namespace Bulldozer.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>> record in _tractorStorages)
foreach (KeyValuePair<BulldozersCollectionInfo, BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>> record in _tractorStorages)
{
StringBuilder records = new();
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();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
@ -53,7 +55,7 @@
//
// ButtonAddBulldozer
//
ButtonAddBulldozer.Location = new Point(26, 314);
ButtonAddBulldozer.Location = new Point(27, 373);
ButtonAddBulldozer.Name = "ButtonAddBulldozer";
ButtonAddBulldozer.Size = new Size(166, 40);
ButtonAddBulldozer.TabIndex = 0;
@ -63,7 +65,7 @@
//
// ButtonRemoveBulldozer
//
ButtonRemoveBulldozer.Location = new Point(26, 400);
ButtonRemoveBulldozer.Location = new Point(27, 459);
ButtonRemoveBulldozer.Name = "ButtonRemoveBulldozer";
ButtonRemoveBulldozer.Size = new Size(166, 39);
ButtonRemoveBulldozer.TabIndex = 1;
@ -73,7 +75,7 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(26, 478);
ButtonRefreshCollection.Location = new Point(26, 505);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(166, 40);
ButtonRefreshCollection.TabIndex = 2;
@ -93,7 +95,7 @@
// maskedTextBoxNumber
//
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.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 22);
@ -102,14 +104,16 @@
//
// groupBox1
//
groupBox1.Controls.Add(ButtonSortByColor);
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(ButtonSortByType);
groupBox1.Controls.Add(ButtonAddBulldozer);
groupBox1.Controls.Add(ButtonRefreshCollection);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(ButtonRemoveBulldozer);
groupBox1.Location = new Point(643, 30);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(209, 533);
groupBox1.Size = new Size(209, 552);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
@ -170,7 +174,7 @@
menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem });
menuStrip.Location = new Point(649, 3);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(176, 24);
menuStrip.Size = new Size(56, 24);
menuStrip.TabIndex = 6;
menuStrip.Text = "menuStrip1";
//
@ -184,14 +188,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(180, 22);
SaveToolStripMenuItem.Size = new Size(133, 22);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22);
LoadToolStripMenuItem.Size = new Size(133, 22);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -206,6 +210,26 @@
saveFileDialog.Filter = "txt file | *.txt";
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
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -247,5 +271,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
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;
_places = new List<T?>(count);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="tractor">Добавляемая установка</param>
/// <returns></returns>
public int Insert(T tractor)
public bool Insert(T tractor, IEqualityComparer<T>? equal = null)
{
if (_places.Count == 0)
{
_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);
}
}
Insert(tractor, 0, equal);
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -67,7 +47,7 @@ namespace Bulldozer.Generics
/// <param name="tractor">Добавляемая установкаь</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T tractor, int position)
public bool Insert(T tractor, int position, IEqualityComparer<T>? equal = null)
{
// TODO проверка позиции
if (position < 0 || position >= _maxCount)
@ -77,9 +57,13 @@ namespace Bulldozer.Generics
}
if (Count >= _maxCount)
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;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -90,13 +74,8 @@ namespace Bulldozer.Generics
{
// TODO проверка позиции
// Проверка позиции
if (position < 0 || position > _maxCount || position >= Count)
throw new BulldozerNotFoundException();
if (_places[position] == null)
{
throw new BulldozerNotFoundException();
}
_places[position] = null;
if ((position < 0) || (position > _maxCount) || (position >= Count)) throw new BulldozerNotFoundException(position);
_places.RemoveAt(position);
return true;
}
/// <summary>