PIbd-22. Fedorenko G.Y. Lab Work 8 #9

Closed
fedorenko wants to merge 2 commits from lab8 into lab7
9 changed files with 248 additions and 19 deletions
Showing only changes of commit fefc8beabb - Show all commits

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
using Hydroplane.DrawningObjects;
using Hydroplane.Entities;
namespace Hydroplane
{
internal class DrawingPlaneEqutables : IEqualityComparer<DrawningPlane?>
{
public bool Equals(DrawningPlane? x, DrawningPlane? y)
{
if (x == null || x.EntityPlane == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityPlane == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
return false;
if (x.EntityPlane.Speed != y.EntityPlane.Speed)
return false;
if (x.EntityPlane.Weight != y.EntityPlane.Weight)
return false;
if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor)
return false;
// to do logic for "сравнения" additional parameters :)
if (x is DrawningHydroplane && y is DrawningHydroplane)
{
if ((x.EntityPlane as EntityHydroplane).AdditionalColor != (y.EntityPlane as EntityHydroplane).AdditionalColor)
{
return false;
}
if ((x.EntityPlane as EntityHydroplane).Boat != (y.EntityPlane as EntityHydroplane).Boat)
{
return false;
}
if ((x.EntityPlane as EntityHydroplane).Bobber != (y.EntityPlane as EntityHydroplane).Bobber)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningPlane obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -47,6 +47,8 @@
loadToolStripMenuItem = new ToolStripMenuItem();
SaveFileDialog = new SaveFileDialog();
OpenFileDialog = new OpenFileDialog();
buttonSortByType = new Button();
buttonSortByColor = new Button();
panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawPlane).BeginInit();
@ -55,6 +57,8 @@
//
// panel1
//
panel1.Controls.Add(buttonSortByColor);
panel1.Controls.Add(buttonSortByType);
panel1.Controls.Add(panel2);
panel1.Controls.Add(UpdateButton);
panel1.Controls.Add(DeleteButton);
@ -65,7 +69,7 @@
panel1.Location = new Point(646, 24);
panel1.Margin = new Padding(3, 2, 3, 2);
panel1.Name = "panel1";
panel1.Size = new Size(219, 362);
panel1.Size = new Size(219, 432);
panel1.TabIndex = 0;
//
// panel2
@ -133,10 +137,10 @@
//
// UpdateButton
//
UpdateButton.Location = new Point(9, 301);
UpdateButton.Location = new Point(14, 389);
UpdateButton.Margin = new Padding(3, 2, 3, 2);
UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(200, 28);
UpdateButton.Size = new Size(187, 28);
UpdateButton.TabIndex = 4;
UpdateButton.Text = "Обновить коллекцию";
UpdateButton.UseVisualStyleBackColor = true;
@ -144,10 +148,10 @@
//
// DeleteButton
//
DeleteButton.Location = new Point(9, 268);
DeleteButton.Location = new Point(14, 356);
DeleteButton.Margin = new Padding(3, 2, 3, 2);
DeleteButton.Name = "DeleteButton";
DeleteButton.Size = new Size(200, 28);
DeleteButton.Size = new Size(187, 28);
DeleteButton.TabIndex = 3;
DeleteButton.Text = "Удалить самолёт";
DeleteButton.UseVisualStyleBackColor = true;
@ -155,10 +159,10 @@
//
// AddButton
//
AddButton.Location = new Point(9, 212);
AddButton.Location = new Point(14, 300);
AddButton.Margin = new Padding(3, 2, 3, 2);
AddButton.Name = "AddButton";
AddButton.Size = new Size(200, 28);
AddButton.Size = new Size(187, 28);
AddButton.TabIndex = 2;
AddButton.Text = "Добавить самолёт";
AddButton.UseVisualStyleBackColor = true;
@ -166,10 +170,10 @@
//
// PlaneTextBox
//
PlaneTextBox.Location = new Point(9, 244);
PlaneTextBox.Location = new Point(14, 332);
PlaneTextBox.Margin = new Padding(3, 2, 3, 2);
PlaneTextBox.Name = "PlaneTextBox";
PlaneTextBox.Size = new Size(200, 23);
PlaneTextBox.Size = new Size(187, 23);
PlaneTextBox.TabIndex = 1;
//
// label1
@ -187,7 +191,7 @@
DrawPlane.Location = new Point(0, 24);
DrawPlane.Margin = new Padding(3, 2, 3, 2);
DrawPlane.Name = "DrawPlane";
DrawPlane.Size = new Size(646, 362);
DrawPlane.Size = new Size(646, 432);
DrawPlane.TabIndex = 1;
DrawPlane.TabStop = false;
//
@ -230,11 +234,31 @@
OpenFileDialog.FileName = "openFileDialog1";
OpenFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
buttonSortByType.Location = new Point(14, 220);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(187, 27);
buttonSortByType.TabIndex = 6;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(14, 253);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(187, 27);
buttonSortByColor.TabIndex = 7;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// FormHydroplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(865, 386);
ClientSize = new Size(865, 456);
Controls.Add(DrawPlane);
Controls.Add(panel1);
Controls.Add(StripMenu);
@ -274,5 +298,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog SaveFileDialog;
private OpenFileDialog OpenFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -66,7 +66,7 @@ namespace Hydroplane
MessageBox.Show("Input not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(SetTextBox.Text);
_storage.AddSet(SetTextBox.Text.ToString());
ReloadObjects();
_logger.LogInformation($"Added set: {SetTextBox.Text}");
}
@ -75,8 +75,7 @@ namespace Hydroplane
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
DrawPlane.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
@ -228,6 +227,7 @@ namespace Hydroplane
}
}
}
private void LoadToolStripMenu_Click(object sender, EventArgs args)
{
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
@ -246,5 +246,20 @@ namespace Hydroplane
}
}
}
private void buttonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType());
private void buttonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor());
public void ComparePlanes(IComparer<DrawningPlane?> comparer)
{
if (CollectionListBox.SelectedIndex == -1) return;
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
obj.Sort(comparer);
DrawPlane.Image = obj.ShowPlanes();
}
}
}

View File

@ -127,6 +127,6 @@
<value>147, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>128</value>
<value>52</value>
</metadata>
</root>

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hydroplane.DrawningObjects;
using Hydroplane.Entities;
namespace Hydroplane
{
internal class PlaneCompareByColor : IComparer<DrawningPlane?>
{
public int Compare(DrawningPlane? x, DrawningPlane? y)
Review

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

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

View File

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

View File

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

View File

@ -33,7 +33,7 @@ namespace Hydroplane.Generics
{
return false;
}
return (bool)collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawingPlaneEqutables());
}
public static T? operator -(PlanesGenericCollection<T, U> collect, int
@ -90,5 +90,7 @@ namespace Hydroplane.Generics
}
public IEnumerable<T?> GetPlanes => _collection.GetPlanes();
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
}
}

View File

@ -14,24 +14,46 @@ namespace Hydroplane.Generics
private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(_maxCount);
}
public bool Insert(T plane)
public bool Insert(T plane, IEqualityComparer<T?>? equal = null)
{
if (equal != null)
{
foreach (var secondPlane in _places)
{
if (equal.Equals(plane, secondPlane))
{
throw new Exception("Такой объект уже есть в коллекции");
}
}
}
return Insert(plane, 0);
}
public bool Insert(T plane, int position)
public bool Insert(T plane, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new StorageOverflowException("Impossible to insert");
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
_places.Insert(0, plane);
if (equal != null)
{
foreach (var secondPlane in _places)
{
if (equal.Equals(plane, secondPlane))
{
throw new ApplicationException("Такой объект уже есть в коллекции");
}
}
}
_places.Insert(position, plane);
return true;
}