lab8 сделано и сдано

This commit is contained in:
sofiaivv 2023-12-29 19:52:22 +04:00
parent c7bd11dd18
commit c9908850f9
9 changed files with 263 additions and 35 deletions

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotorBoat.DrawningObjects;
using MotorBoat.Entities;
namespace MotorBoat.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));
}
var bodyColorCompare = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name);
if (bodyColorCompare != 0)
{
return bodyColorCompare;
}
if (x.EntityBoat is EntityMotorBoat xEntityMotorBoat && y.EntityBoat is EntityMotorBoat yEntityMotorBoat)
{
var BodyColorCompare = xEntityMotorBoat.BodyColor.Name.CompareTo(yEntityMotorBoat.BodyColor.Name);
if (BodyColorCompare != 0)
{
return BodyColorCompare;
}
var AdditionalColorCompare = xEntityMotorBoat.AdditionalColor.Name.CompareTo(yEntityMotorBoat.AdditionalColor.Name);
if (AdditionalColorCompare != 0)
{
return AdditionalColorCompare;
}
}
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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotorBoat.DrawningObjects;
namespace MotorBoat.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

@ -67,12 +67,12 @@ namespace MotorBoat.Generics
/// <returns></returns>
public static bool operator +(BoatsGenericCollection<T, U> collect, T? obj)
{
if (obj != null && collect != null)
if (obj == null)
{
collect._collection.Insert(obj);
return true;
return false;
}
return false;
return collect?._collection.Insert(obj, new DrawningBoatEqutables()) ?? false;
}
/// <summary>
@ -156,5 +156,6 @@ namespace MotorBoat.Generics
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetBoats => _collection.GetBoats();
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MotorBoat.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)
{
if (Name == other?.Name)
return true;
return false;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
}

View File

@ -19,12 +19,12 @@ namespace MotorBoat.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BoatsGenericCollection<DrawningBoat,DrawningObjectBoat>> _boatStorages;
readonly Dictionary<BoatsCollectionInfo, BoatsGenericCollection<DrawningBoat,DrawningObjectBoat>> _boatStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _boatStorages.Keys.ToList();
public List<BoatsCollectionInfo> Keys => _boatStorages.Keys.ToList();
/// <summary>
/// Разделитель для записи ключа и значения элемента словаря
@ -58,7 +58,7 @@ namespace MotorBoat.Generics
/// <param name="pictureHeight"></param>
public BoatsGenericStorage(int pictureWidth, int pictureHeight)
{
_boatStorages = new Dictionary<string, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>>();
_boatStorages = new Dictionary<BoatsCollectionInfo, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -69,9 +69,9 @@ namespace MotorBoat.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_boatStorages.ContainsKey(name))
return;
_boatStorages[name] = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(_pictureWidth, _pictureHeight);
if (!_boatStorages.ContainsKey(new BoatsCollectionInfo(name, string.Empty)))
_boatStorages.Add(new BoatsCollectionInfo(name, string.Empty),
new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(_pictureWidth, _pictureHeight));
}
/// <summary>
@ -80,9 +80,8 @@ namespace MotorBoat.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_boatStorages.ContainsKey(name))
return;
_boatStorages.Remove(name);
if (_boatStorages.ContainsKey(new BoatsCollectionInfo(name, string.Empty)))
_boatStorages.Remove(new BoatsCollectionInfo(name, string.Empty));
}
/// <summary>
@ -90,13 +89,13 @@ namespace MotorBoat.Generics
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>?
this[string ind]
public BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>? this[string ind]
{
get
{
if (_boatStorages.ContainsKey(ind))
return _boatStorages[ind];
if (_boatStorages.ContainsKey(new BoatsCollectionInfo(ind, string.Empty)))
return _boatStorages[new BoatsCollectionInfo(ind, string.Empty)];
return null;
}
}
@ -113,16 +112,16 @@ namespace MotorBoat.Generics
File.Delete(filename);
}
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)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0) {
throw new InvalidOperationException("Невалидная операция, нет данных для сохранения");
}
@ -195,7 +194,7 @@ namespace MotorBoat.Generics
}
}
}
_boatStorages.Add(record[0], collection);
_boatStorages.Add(new BoatsCollectionInfo(record[0], string.Empty), collection);
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotorBoat.DrawningObjects;
using MotorBoat.Entities;
using System.Diagnostics.CodeAnalysis;
namespace MotorBoat.Generics
{
internal class DrawningBoatEqutables : 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 DrawningMotorBoat && y is DrawningMotorBoat)
{
EntityMotorBoat EntityX = (EntityMotorBoat)x.EntityBoat;
EntityMotorBoat EntityY = (EntityMotorBoat)y.EntityBoat;
if (EntityX.Glass != EntityY.Glass)
return false;
if (EntityX.Engine != EntityY.Engine)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningBoat obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -34,6 +34,8 @@
ButtonRemoveBoat = new Button();
ButtonRefreshCollection = new Button();
groupBoxTools = new GroupBox();
buttonSortByType = new Button();
buttonSortByColor = new Button();
groupBoxCollections = new GroupBox();
ButtonRemoveObject = new Button();
listBoxStorages = new ListBox();
@ -56,20 +58,20 @@
pictureBoxCollection.Dock = DockStyle.Left;
pictureBoxCollection.Location = new Point(0, 24);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(578, 504);
pictureBoxCollection.Size = new Size(578, 576);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(51, 371);
maskedTextBoxNumber.Location = new Point(51, 454);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 23);
maskedTextBoxNumber.TabIndex = 0;
//
// ButtonAddBoat
//
ButtonAddBoat.Location = new Point(6, 325);
ButtonAddBoat.Location = new Point(6, 408);
ButtonAddBoat.Name = "ButtonAddBoat";
ButtonAddBoat.Size = new Size(188, 40);
ButtonAddBoat.TabIndex = 1;
@ -79,7 +81,7 @@
//
// ButtonRemoveBoat
//
ButtonRemoveBoat.Location = new Point(6, 407);
ButtonRemoveBoat.Location = new Point(6, 490);
ButtonRemoveBoat.Name = "ButtonRemoveBoat";
ButtonRemoveBoat.Size = new Size(188, 41);
ButtonRemoveBoat.TabIndex = 2;
@ -89,7 +91,7 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(6, 454);
ButtonRefreshCollection.Location = new Point(6, 537);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(188, 39);
ButtonRefreshCollection.TabIndex = 3;
@ -99,6 +101,8 @@
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonSortByType);
groupBoxTools.Controls.Add(buttonSortByColor);
groupBoxTools.Controls.Add(groupBoxCollections);
groupBoxTools.Controls.Add(ButtonRefreshCollection);
groupBoxTools.Controls.Add(ButtonRemoveBoat);
@ -107,11 +111,31 @@
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(584, 24);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(200, 504);
groupBoxTools.Size = new Size(200, 576);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// buttonSortByType
//
buttonSortByType.Location = new Point(12, 339);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(176, 29);
buttonSortByType.TabIndex = 6;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(12, 304);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(176, 29);
buttonSortByColor.TabIndex = 5;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// groupBoxCollections
//
groupBoxCollections.Controls.Add(ButtonRemoveObject);
@ -181,14 +205,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;
//
@ -205,7 +229,7 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(784, 528);
ClientSize = new Size(784, 600);
Controls.Add(groupBoxTools);
Controls.Add(pictureBoxCollection);
Controls.Add(menuStrip1);
@ -242,5 +266,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -49,7 +49,7 @@ namespace MotorBoat
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
listBoxStorages.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
@ -163,6 +163,11 @@ namespace MotorBoat
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}");
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
}
}
/// <summary>
@ -281,5 +286,23 @@ namespace MotorBoat
}
ReloadObjects();
}
private void buttonSortByColor_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByType());
private void buttonSortByType_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByColor());
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();
}
}
}

View File

@ -33,6 +33,8 @@ namespace MotorBoat.Generics
/// Конструктор
/// </summary>
/// <param name="count"></param>
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public SetGeneric(int count)
{
_maxCount = count;
@ -44,9 +46,9 @@ namespace MotorBoat.Generics
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <returns></returns>
public bool Insert(T boat)
public bool Insert(T boat, IEqualityComparer<T?>? equal = null)
{
return Insert(boat, 0);
return Insert(boat, 0, equal);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -54,13 +56,17 @@ namespace MotorBoat.Generics
/// <param name="boat">Добавляемая лодка</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T boat, int position)
public bool Insert(T boat, int position, IEqualityComparer<T>? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new BoatNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (equal != null && _places.Contains(boat, equal))
throw new ArgumentException("Данный объект уже есть в коллекции");
_places.Insert(0, boat);
return true;
}
@ -103,7 +109,7 @@ namespace MotorBoat.Generics
/// <summary>
/// Проход по списку
/// </summary>
/// <param name="maxShips"></param>
/// <param name="maxBoats"></param>
/// <returns></returns>
public IEnumerable<T> GetBoats(int? maxBoats = null)
{