ISE_22.Aparyan.Bulldozer.Base Lab08 #8

Closed
LuizaAparyan wants to merge 1 commits from Laba08 into Laba07
10 changed files with 272 additions and 48 deletions
Showing only changes of commit caf208b305 - Show all commits

View File

@ -25,6 +25,8 @@ namespace ProjectBulldozer
сохранитьToolStripMenuItem = new ToolStripMenuItem();
загрузитьToolStripMenuItem = new ToolStripMenuItem();
toolStripMenuItem1 = new ToolStripMenuItem();
ButtonSortByType = new Button();
ButtonSortByColor = new Button();
((ISupportInitialize)pictureBoxCollections).BeginInit();
groupBox1.SuspendLayout();
Instruments.SuspendLayout();
@ -34,17 +36,17 @@ namespace ProjectBulldozer
// maskedTextBoxNumber
//
maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
maskedTextBoxNumber.Location = new Point(33, 356);
maskedTextBoxNumber.Location = new Point(35, 379);
maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2);
maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(131, 23);
maskedTextBoxNumber.Size = new Size(132, 23);
maskedTextBoxNumber.TabIndex = 4;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
ButtonRefreshCollection.Location = new Point(33, 499);
ButtonRefreshCollection.Location = new Point(33, 441);
ButtonRefreshCollection.Margin = new Padding(3, 2, 3, 2);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(131, 31);
@ -56,7 +58,7 @@ namespace ProjectBulldozer
// ButtonRemoveTractor
//
ButtonRemoveTractor.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
ButtonRemoveTractor.Location = new Point(33, 393);
ButtonRemoveTractor.Location = new Point(35, 406);
ButtonRemoveTractor.Margin = new Padding(3, 2, 3, 2);
ButtonRemoveTractor.Name = "ButtonRemoveTractor";
ButtonRemoveTractor.Size = new Size(131, 31);
@ -68,7 +70,7 @@ namespace ProjectBulldozer
// ButtonAddTractor
//
ButtonAddTractor.Anchor = AnchorStyles.Top;
ButtonAddTractor.Location = new Point(33, 323);
ButtonAddTractor.Location = new Point(35, 337);
ButtonAddTractor.Margin = new Padding(3, 2, 3, 2);
ButtonAddTractor.Name = "ButtonAddTractor";
ButtonAddTractor.Size = new Size(131, 29);
@ -83,14 +85,14 @@ namespace ProjectBulldozer
pictureBoxCollections.Location = new Point(0, 26);
pictureBoxCollections.Margin = new Padding(3, 2, 3, 2);
pictureBoxCollections.Name = "pictureBoxCollections";
pictureBoxCollections.Size = new Size(697, 404);
pictureBoxCollections.Size = new Size(697, 474);
pictureBoxCollections.TabIndex = 1;
pictureBoxCollections.TabStop = false;
pictureBoxCollections.Click += pictureBoxCollections_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(27, 32);
textBoxStorageName.Location = new Point(27, 22);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(131, 23);
textBoxStorageName.TabIndex = 5;
@ -103,7 +105,7 @@ namespace ProjectBulldozer
groupBox1.Controls.Add(textBoxStorageName);
groupBox1.Location = new Point(6, 22);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(189, 296);
groupBox1.Size = new Size(189, 219);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "Наборы";
@ -112,7 +114,7 @@ namespace ProjectBulldozer
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 15;
listBoxStorage.Location = new Point(27, 122);
listBoxStorage.Location = new Point(27, 84);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(131, 94);
listBoxStorage.TabIndex = 9;
@ -121,7 +123,7 @@ namespace ProjectBulldozer
// ButtonAddObject
//
ButtonAddObject.Anchor = AnchorStyles.Top;
ButtonAddObject.Location = new Point(27, 72);
ButtonAddObject.Location = new Point(27, 50);
ButtonAddObject.Margin = new Padding(3, 2, 3, 2);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(131, 29);
@ -133,7 +135,7 @@ namespace ProjectBulldozer
// ButtonRemoveObject
//
ButtonRemoveObject.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
ButtonRemoveObject.Location = new Point(27, 244);
ButtonRemoveObject.Location = new Point(27, 181);
ButtonRemoveObject.Margin = new Padding(3, 2, 3, 2);
ButtonRemoveObject.Name = "ButtonRemoveObject";
ButtonRemoveObject.Size = new Size(131, 33);
@ -144,14 +146,16 @@ namespace ProjectBulldozer
//
// Instruments
//
Instruments.Controls.Add(ButtonSortByColor);
Instruments.Controls.Add(ButtonSortByType);
Instruments.Controls.Add(ButtonRefreshCollection);
Instruments.Controls.Add(groupBox1);
Instruments.Controls.Add(maskedTextBoxNumber);
Instruments.Controls.Add(ButtonAddTractor);
Instruments.Controls.Add(ButtonRemoveTractor);
Instruments.Location = new Point(697, 4);
Instruments.Location = new Point(703, 26);
Instruments.Name = "Instruments";
Instruments.Size = new Size(200, 541);
Instruments.Size = new Size(201, 464);
Instruments.TabIndex = 6;
Instruments.TabStop = false;
Instruments.Text = "Инструменты";
@ -207,11 +211,31 @@ namespace ProjectBulldozer
toolStripMenuItem1.Name = "toolStripMenuItem1";
toolStripMenuItem1.Size = new Size(32, 19);
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(33, 247);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(131, 41);
ButtonSortByType.TabIndex = 7;
ButtonSortByType.Text = "Сортировать по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(33, 294);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(131, 38);
ButtonSortByColor.TabIndex = 8;
ButtonSortByColor.Text = "Сортировать по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// FormTractorCollections
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(904, 439);
ClientSize = new Size(904, 509);
Controls.Add(Instruments);
Controls.Add(pictureBoxCollections);
Controls.Add(menuStrip1);
@ -267,5 +291,7 @@ namespace ProjectBulldozer
private ToolStripMenuItem сохранитьToolStripMenuItem;
private ToolStripMenuItem загрузитьToolStripMenuItem;
private ToolStripMenuItem toolStripMenuItem1;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -3,17 +3,14 @@ using ProjectBulldozer.Generics;
using ProjectBulldozer.Drawning;
using ProjectBulldozer.Exceptions;
using Microsoft.Extensions.Logging;
using System.ComponentModel;
using System.Xml.Linq;
namespace ProjectBulldozer
{
public partial class FormTractorCollections : Form
{
private readonly TractorGenericStorage _storage;
private readonly TractorGenericCollection<DrawingTractor, DrawingObjectTractor> _tractors;
readonly int countPlace = 10;
private readonly ILogger _logger;
private readonly ILogger _logger;
readonly int countPlace = 12;
public FormTractorCollections()
{
InitializeComponent();
@ -25,7 +22,6 @@ namespace ProjectBulldozer
_storage = new TractorGenericStorage(pictureBoxCollections.Width, pictureBoxCollections.Height);
_logger = logger;
}
private void ReloadObjects()
{
int index = listBoxStorage.SelectedIndex;
@ -99,15 +95,19 @@ namespace ProjectBulldozer
tractor._pictureHeight = pictureBoxCollections.Height;
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
string name = listBoxStorage.SelectedItem.ToString() ?? string.Empty;
if (obj == null)
{
return;
}
try
{
if (obj + tractor)
int addedIndex = obj + tractor;
if (addedIndex != -1 && addedIndex < countPlace)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollections.Image = obj.ShowTractors();
@ -124,6 +124,11 @@ namespace ProjectBulldozer
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
}
}
private void ButtonRemoveTractor_Click(object sender, EventArgs e)
{
@ -160,7 +165,6 @@ namespace ProjectBulldozer
_logger.LogWarning($"{ex.Message}. Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1) return;
@ -206,6 +210,24 @@ namespace ProjectBulldozer
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTractors(new TractorCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTractors(new TractorCompareByColor());
private void CompareTractors(IComparer<DrawingTractor?> comparer)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollections.Image = obj.ShowTractors();
}
}
}

View File

@ -126,4 +126,7 @@
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>286, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>

View File

@ -5,8 +5,10 @@ namespace ProjectBulldozer.Generics
{
internal class TractorGenericCollection<T, U> where T : DrawingTractor where U : IMoveableObject
{
//ширина/высота окна
private readonly int _pictureWidth;
private readonly int _pictureHeight;
//ширина/высота занимаемого места
private readonly int _placeSizeWidth = 150;
private readonly int _placeSizeHeight = 130;
private readonly SetGeneric<T> _collection;
@ -19,10 +21,11 @@ namespace ProjectBulldozer.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static bool operator +(TractorGenericCollection<T, U> collect, T? tract)
public static int operator +(TractorGenericCollection<T, U> collect, T? tractor)
{
if (tract == null) return false;
return collect._collection.Insert(tract);
if (tractor == null) return -1;
return collect?._collection.Insert(tractor, new DrawingBulldozerEqutables())??-1;
}
public static T? operator -(TractorGenericCollection<T, U> collect, int pos)
{
@ -33,6 +36,10 @@ namespace ProjectBulldozer.Generics
}
return obj;
}
public U? GetU(int pos)
{
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowTractors()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
@ -48,7 +55,7 @@ namespace ProjectBulldozer.Generics
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
//линия рамзетки места
//линия разметки места
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
@ -60,17 +67,21 @@ namespace ProjectBulldozer.Generics
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
// Получение объекта
var obj = _collection[i];
// Установка позиции
obj?.SetPosition(
(int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)),
(int)((height - 1) * _placeSizeHeight - (i / width * _placeSizeHeight))
);
// Прорисовка объекта
obj?.DrawTransport(g);
}
}
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
}
}
}

View File

@ -0,0 +1,55 @@
using System.Diagnostics.CodeAnalysis;
using ProjectBulldozer.Drawning;
using ProjectBulldozer.Entities;
namespace ProjectBulldozer.Generics
{
internal class DrawingBulldozerEqutables : IEqualityComparer<DrawingTractor?>
{
public bool Equals(DrawingTractor? x, DrawingTractor? 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.BodyColor != y.EntityTractor.BodyColor)
{
return false;
}
if (x is DrawingBulldozer && y is DrawingBulldozer)
Review

Нет сравнения по дополнительному цвету

Нет сравнения по дополнительному цвету
{
if ((x.EntityTractor as EntityBulldozer).Otval != (y.EntityTractor as EntityBulldozer).Otval)
{
return false;
}
if ((x.EntityTractor as EntityBulldozer).ThirdWheel != (y.EntityTractor as EntityBulldozer).ThirdWheel)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawingTractor obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -13,11 +13,14 @@ namespace ProjectBulldozer.Generics
_places = new List<T?>(count);
}
/// Добавление объекта в набор
public bool Insert(T tract)
public int Insert(T tractor, IEqualityComparer<T?>? equal = null)
{
return Insert(tract, 0);
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(tractor, 0, equal);
return 0;
}
public bool Insert(T tract, int position)
public bool Insert(T tractor, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount)
{
@ -27,7 +30,17 @@ namespace ProjectBulldozer.Generics
{
throw new StorageOverflowException(_maxCount);
}
_places.Insert(position, tract);
if (equal != null)
{
foreach (var i in _places)
{
if (equal.Equals(i, tractor))
{
throw new ApplicationException($"Объект уже существует");
}
}
}
_places.Insert(position, tractor);
return true;
}
public bool Remove(int position)
@ -67,5 +80,7 @@ namespace ProjectBulldozer.Generics
}
}
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
}
}

View File

@ -0,0 +1,38 @@
using ProjectBulldozer.Drawning;
using ProjectBulldozer.Entities;
namespace ProjectBulldozer.Generics
{
internal class TractorCompareByColor : IComparer<DrawingTractor?>
{
public int Compare(DrawingTractor? x, DrawingTractor? 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.BodyColor != y.EntityTractor.BodyColor)
{
return x.EntityTractor.BodyColor.Name.CompareTo(y.EntityTractor.BodyColor.Name);
}
if (x.EntityTractor is EntityBulldozer _BulldozerX && y.EntityTractor is EntityBulldozer _BulldozertY)
{
var ElementsColorCompare = _BulldozerX.AdditionalColor.Name.CompareTo(_BulldozertY.AdditionalColor.Name);
if (ElementsColorCompare != 0)
{
return ElementsColorCompare;
}
}
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,29 @@
using ProjectBulldozer.Drawning;
namespace ProjectBulldozer.Generics
{
internal class TractorCompareByType : IComparer<DrawingTractor?>
{
public int Compare(DrawingTractor? x, DrawingTractor? 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

@ -9,38 +9,36 @@ namespace ProjectBulldozer.Generics
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
readonly Dictionary<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>> _TractorsStorage;
public List<string> Keys => _TractorsStorage.Keys.ToList();
readonly Dictionary<TractorsCollectionInfo, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>> _TractorsStorage;
public List<TractorsCollectionInfo> Keys => _TractorsStorage.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public TractorGenericStorage(int pictureWidth, int pictureHeight)
{
_TractorsStorage = new Dictionary<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>>();
_TractorsStorage = new Dictionary<TractorsCollectionInfo, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (!_TractorsStorage.ContainsKey(name))
if (!_TractorsStorage.ContainsKey(new TractorsCollectionInfo(name, "")))
{
_TractorsStorage.Add(name, new TractorGenericCollection<DrawingTractor, DrawingObjectTractor>(_pictureWidth, _pictureHeight));
_TractorsStorage.Add(new TractorsCollectionInfo(name, ""), new TractorGenericCollection<DrawingTractor, DrawingObjectTractor>(_pictureWidth, _pictureHeight));
}
}
public void DelSet(string name)
{
if (_TractorsStorage.ContainsKey(name))
if (_TractorsStorage.ContainsKey(new TractorsCollectionInfo(name, "")))
{
_TractorsStorage.Remove(name);
_TractorsStorage.Remove(new TractorsCollectionInfo(name, ""));
}
}
public TractorGenericCollection<DrawingTractor, DrawingObjectTractor>? this[string ind]
{
get
{
if (_TractorsStorage.ContainsKey(ind))
{
return _TractorsStorage[ind];
}
var info = new TractorsCollectionInfo(ind, "");
if (_TractorsStorage.ContainsKey(info)) return _TractorsStorage[info];
return null;
}
}
@ -51,9 +49,7 @@ namespace ProjectBulldozer.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>>
record in _TractorsStorage)
foreach (KeyValuePair<TractorsCollectionInfo, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>> record in _TractorsStorage)
{
StringBuilder records = new();
foreach (DrawingTractor? elem in record.Value.GetTractors)
@ -112,13 +108,13 @@ namespace ProjectBulldozer.Generics
_pictureWidth, _pictureHeight);
if (tractor != null)
{
if ((collection + tractor))
if ((collection + tractor)==-1)
{
throw new ApplicationException("Ошибка добавления в коллекцию");
}
}
}
_TractorsStorage.Add(record[0], collection);
_TractorsStorage.Add(new TractorsCollectionInfo(record[0], string.Empty), collection);
}
return true;
}

View File

@ -0,0 +1,29 @@
namespace ProjectBulldozer.Generics
{
internal class TractorsCollectionInfo : IEquatable<TractorsCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public TractorsCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(TractorsCollectionInfo? other)
{
return Name == other.Name;
Review

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

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