PIbd-21 Yakovlev M.G. lab work 08 #11

Closed
YakovlevMaxim wants to merge 1 commits from Lab8SailBoat into Lab7SailBoat
9 changed files with 272 additions and 30 deletions
Showing only changes of commit 0fa9af72bf - Show all commits

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
tools = new GroupBox();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
sets = new GroupBox();
ButtonDelObject = new Button();
listBoxStorages = new ListBox();
@ -53,6 +55,8 @@
//
// tools
//
tools.Controls.Add(ButtonSortByColor);
tools.Controls.Add(ButtonSortByType);
tools.Controls.Add(sets);
tools.Controls.Add(maskedTextBoxNumber);
tools.Controls.Add(buttonRefreshCollection);
@ -66,6 +70,26 @@
tools.TabStop = false;
tools.Text = "Инструменты";
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(12, 353);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(176, 45);
ButtonSortByColor.TabIndex = 7;
ButtonSortByColor.Text = "Сортировать по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(12, 305);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(176, 42);
ButtonSortByType.TabIndex = 6;
ButtonSortByType.Text = "Сортировать по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// sets
//
sets.Controls.Add(ButtonDelObject);
@ -74,14 +98,14 @@
sets.Controls.Add(textBoxStorageName);
sets.Location = new Point(6, 22);
sets.Name = "sets";
sets.Size = new Size(188, 344);
sets.Size = new Size(188, 257);
sets.TabIndex = 5;
sets.TabStop = false;
sets.Text = "Наборы";
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(6, 263);
ButtonDelObject.Location = new Point(6, 208);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(176, 41);
ButtonDelObject.TabIndex = 3;
@ -93,7 +117,7 @@
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(6, 148);
listBoxStorages.Location = new Point(6, 93);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(176, 109);
listBoxStorages.TabIndex = 2;
@ -101,7 +125,7 @@
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(6, 39);
ButtonAddObject.Location = new Point(6, 17);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(176, 41);
ButtonAddObject.TabIndex = 1;
@ -111,14 +135,14 @@
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 97);
textBoxStorageName.Location = new Point(6, 64);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(176, 23);
textBoxStorageName.TabIndex = 0;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(45, 454);
maskedTextBoxNumber.Location = new Point(45, 488);
maskedTextBoxNumber.Mask = "00000";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(112, 23);
@ -127,7 +151,7 @@
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(6, 545);
buttonRefreshCollection.Location = new Point(6, 562);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(188, 39);
buttonRefreshCollection.TabIndex = 3;
@ -137,7 +161,7 @@
//
// buttonRemoveBoat
//
buttonRemoveBoat.Location = new Point(6, 483);
buttonRemoveBoat.Location = new Point(6, 517);
buttonRemoveBoat.Name = "buttonRemoveBoat";
buttonRemoveBoat.Size = new Size(188, 39);
buttonRemoveBoat.TabIndex = 1;
@ -147,7 +171,7 @@
//
// buttonAddBoat
//
buttonAddBoat.Location = new Point(6, 372);
buttonAddBoat.Location = new Point(6, 421);
buttonAddBoat.Name = "buttonAddBoat";
buttonAddBoat.Size = new Size(188, 39);
buttonAddBoat.TabIndex = 0;
@ -183,14 +207,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(180, 22);
SaveToolStripMenuItem.Size = new Size(141, 22);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22);
LoadToolStripMenuItem.Size = new Size(141, 22);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -243,5 +267,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -107,7 +107,13 @@ namespace SailBoat
_logger.LogWarning("Не удалось добавить объект");
}
}
catch(StorageOverflowException ex) {
catch (StorageOverflowException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
}
@ -150,7 +156,8 @@ namespace SailBoat
MessageBox.Show("Не удалось удалить объект");
_logger.LogWarning("Не удалось удалить объект");
}
} catch(BoatNotFoundException ex)
}
catch (BoatNotFoundException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message}. Не удалось удалить объект");
@ -175,7 +182,7 @@ namespace SailBoat
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Сохранение");
}
catch(Exception ex)
catch (Exception ex)
{
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("Ошибка сохранения");
@ -185,7 +192,7 @@ namespace SailBoat
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if(openFileDialog.ShowDialog() == DialogResult.OK)
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
@ -194,12 +201,31 @@ namespace SailBoat
MessageBox.Show("Загрузка произошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Загрузка");
}
catch(Exception ex)
catch (Exception ex)
{
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("Ошибка загрузки");
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByType());
private void CompareBoats(IComparer<DrawningBoat?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowBoats();
}
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByColor());
}
}

View File

@ -0,0 +1,43 @@
using SailBoat.DrawningObjects;
using SailBoat.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SailBoat.Generics
{
internal class BoatCompareByColor : IComparer<DrawningBoat?>
{
public int Compare(DrawningBoat? x, DrawningBoat? y)
{
if (x == null || x.EntityBoat == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityBoat == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor)
{
return x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name);
}
if (x.EntityBoat is EntitySailBoat _SailBoatX && y.EntityBoat is EntitySailBoat _SailBoatY)
{
var ElementsColorCompare = _SailBoatX.AdditionalColor.Name.CompareTo(_SailBoatY.AdditionalColor.Name);
if (ElementsColorCompare != 0)
{
return ElementsColorCompare;
}
}
var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight);
}
}
}

View File

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

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SailBoat.Generics
{
internal class BoatsCollectionInfo : IEquatable<BoatsCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public BoatsCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(BoatsCollectionInfo? other)
{
return Name == other.Name;
Review

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

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

View File

@ -35,7 +35,7 @@ namespace SailBoat.Generics
public static bool operator +(BoatsGenericCollection<T,U> collect, T? obj)
{
if (obj == null) return false;
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawingBoatEqutables());
}
public static T? operator -(BoatsGenericCollection<T,U> collect, int pos)
@ -90,5 +90,7 @@ namespace SailBoat.Generics
}
public IEnumerable<T?> GetBoats => _collection.GetBoats();
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
}
}

View File

@ -10,9 +10,9 @@ namespace SailBoat.Generics
{
internal class BoatsGenericStorage
{
readonly Dictionary<string, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>> _boatStorages;
readonly Dictionary<BoatsCollectionInfo, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>> _boatStorages;
public List<string> Keys => _boatStorages.Keys.ToList();
public List<BoatsCollectionInfo> Keys => _boatStorages.Keys.ToList();
private readonly int _pictureWidth;
@ -20,24 +20,25 @@ namespace SailBoat.Generics
public BoatsGenericStorage(int pictureWidth, int pictureHeight)
{
_boatStorages = new Dictionary<string, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>>();
_boatStorages = new Dictionary<BoatsCollectionInfo, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string Name)
{
if (!_boatStorages.ContainsKey(Name))
if (!_boatStorages.ContainsKey(new BoatsCollectionInfo(Name, "")))
{
_boatStorages[Name] = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(_pictureWidth, _pictureHeight);
_boatStorages[new BoatsCollectionInfo(Name, "")] = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(_pictureWidth, _pictureHeight);
}
}
public void DelSet(string Name)
{
if (_boatStorages.ContainsKey(Name))
if (_boatStorages.ContainsKey(new BoatsCollectionInfo(Name, "")))
{
_boatStorages.Remove(Name);
_boatStorages.Remove(new BoatsCollectionInfo(Name, ""));
}
}
@ -45,7 +46,8 @@ namespace SailBoat.Generics
{
get
{
if (_boatStorages.ContainsKey(ind)) return _boatStorages[ind];
var info = new BoatsCollectionInfo(ind, "");
if (_boatStorages.ContainsKey(info)) return _boatStorages[info];
return null;
}
}
@ -63,7 +65,7 @@ namespace SailBoat.Generics
StringBuilder data = new();
foreach (KeyValuePair < string, BoatsGenericCollection < DrawningBoat, DrawningObjectBoat>> record in _boatStorages)
foreach (KeyValuePair <BoatsCollectionInfo, BoatsGenericCollection < DrawningBoat, DrawningObjectBoat>> record in _boatStorages)
{
StringBuilder records = new();
foreach (DrawningBoat? elem in record.Value.GetBoats)
@ -127,7 +129,7 @@ namespace SailBoat.Generics
}
}
_boatStorages.Add(record[0], collection);
_boatStorages.Add(new BoatsCollectionInfo(record[0], ""), collection);
}
return true;

View File

@ -0,0 +1,60 @@
using SailBoat.DrawningObjects;
using SailBoat.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SailBoat.Generics
{
internal class DrawingBoatEqutables : IEqualityComparer<DrawningBoat?>
{
public bool Equals(DrawningBoat? x, DrawningBoat? y)
{
if (x == null || x.EntityBoat == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityBoat == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityBoat.Speed != y.EntityBoat.Speed)
{
return false;
}
if (x.EntityBoat.Weight != y.EntityBoat.Weight)
{
return false;
}
if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor)
{
return false;
}
if (x is DrawningSailBoat && y is DrawningSailBoat)
Review

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

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

View File

@ -22,12 +22,12 @@ namespace SailBoat.Generics
_places = new List<T?>(Count);
}
public bool Insert(T boat)
public bool Insert(T boat, IEqualityComparer<T?>? equal = null)
{
return Insert(boat, 0);
return Insert(boat, 0, equal);
}
public bool Insert(T boat, int position)
public bool Insert(T boat, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount)
{
@ -37,6 +37,18 @@ namespace SailBoat.Generics
{
throw new StorageOverflowException(_maxCount);
}
if(equal != null)
{
foreach(var i in _places)
{
if (equal.Equals(i, boat))
{
throw new ApplicationException($"Объект уже существует");
}
}
}
_places.Insert(position, boat);
return true;
}
@ -81,5 +93,7 @@ namespace SailBoat.Generics
}
}
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
}
}