lab 8 start

This commit is contained in:
Ivan Kalivan 2024-05-22 14:53:28 +03:00
parent 15b8e2f494
commit 04d7906c68
11 changed files with 255 additions and 29 deletions

View File

@ -56,7 +56,7 @@ public abstract class AbstractCompany
/// <returns></returns>
public static bool operator +(AbstractCompany company, DrawningWarmlyShip warmlyShip)
{
return company._collection?.Insert(warmlyShip) ?? false;
return company._collection?.Insert(warmlyShip, new DrawningWarmlyShipEqutables) ?? false;
}
/// <summary>
@ -112,4 +112,7 @@ public abstract class AbstractCompany
/// Расстановка объектов
/// </summary>
protected abstract void SetObjectsPosition();
//метод выз метода Icollect
public void Sort(IComparer<DrawningWarmlyShip?> comparer) => _collection?.CollectionSort(comparer);
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShipProject.CollectionGenericObjects;
public class CollectionInfo : IEqualityComparer<CollectionInfo>
{
//название кол тип кол опис
///<summary>
/// Название
/// </summary>
public string Name { get; private set; }
/// <summary>
/// тип
/// </summary>
public CollectionType CollectionType {get; private set; }
/// <summary>
/// описание
/// </summary>
public string Description { get; private set; }
private static readonly string _separator = "-";
public CollectionInfo(string name, CollectionType collectionType, string description)
{
Name = name;
CollectionType = collectionType;
Description = description;
}
public static CollectionInfo? GetCollectionInfo(string data)
{
string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
if (strs.Length < 1|| strs.Length > 3) { return null; }
return new CollectionInfo(strs[0],(CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ?strs[2] : string.Empty);
}
public bool Equals(CollectionInfo? x, other)
{
throw new NotImplementedException();
}
public int GetHashCode([DisallowNull] CollectionInfo obj)
{
throw new NotImplementedException();
}
}

View File

@ -1,4 +1,6 @@
namespace WarmlyShipProject.CollectionGenericObjects;
using WarmlyShipProject.Drawnings;
namespace WarmlyShipProject.CollectionGenericObjects;
/// <summary>
/// Интерфейс описания действий для набора хранимых объектов
@ -23,7 +25,7 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj);
bool Insert(T obj, IEqualityComparer<DrawningWarmlyShip?>? comparer = null);
/// <summary>
/// Добавление объекта в коллекцию на конкретную позицию
@ -31,7 +33,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="obj">Добавляемый объект</param>
/// <param name="position">Позиция</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj, int position);
bool Insert(T obj, int position, IEqualityComparer<DrawningWarmlyShip?>? comparer = null);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции
@ -57,4 +59,9 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <returns>Поэлементый вывод элементов коллекции</returns>
IEnumerable<T?> GetItems();
///<summary>
///Сортировка коллекции
/// </summary>
void CollectionSort(IComparer<T?> comparer);
}

View File

@ -4,6 +4,7 @@ using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarmlyShipProject.Drawnings;
using WarmlyShipProject.Exceptions;
namespace WarmlyShipProject.CollectionGenericObjects;
@ -63,7 +64,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position];
}
public bool Insert(T obj)
public bool Insert(T obj, IEqualityComparer<DrawningWarmlyShip?>? comparer = null)
{
if( Count == _maxCount)
{
@ -72,9 +73,10 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
_collection.Add(obj);
//TODO Exception переполнение
return true;
//TODO Есть ли такой объект в коллекции
}
public bool Insert(T obj, int position)
public bool Insert(T obj, int position, IEqualityComparer<DrawningWarmlyShip?>? comparer = null)
{
if( Count == _maxCount)
@ -87,6 +89,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
}
//TODO Exception Переполнение
return true;
//TODO Есть ли такой объект в коллекции
}
public bool Remove(int position)
@ -108,4 +111,9 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
{
throw new NotImplementedException();
}
}

View File

@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using WarmlyShipProject.Drawnings;
using WarmlyShipProject.Exceptions;
namespace WarmlyShipProject.CollectionGenericObjects;
@ -65,7 +66,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position];
}
public bool Insert(T obj)
public bool Insert(T obj, IEqualityComparer<DrawningWarmlyShip?>? comparer = null)
{
for(int i = 0; i < _collection.Length; i++)
{
@ -77,9 +78,11 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
//TODO Exception Переполнение
throw new CollectionOverflowException(Count);
//TODO Есть ли такой объект в коллекции
}
public bool Insert(T obj, int position)
public bool Insert(T obj, int position, IEqualityComparer<DrawningWarmlyShip?>? comparer = null)
{
if(position < 0|| position >= _collection.Length)
{
@ -113,6 +116,8 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
//TODO Exception Выход з границы массива
//TODO Exception переполнение
throw new CollectionOverflowException(Count);
//TODO Есть ли такой объект в коллекции
}
public bool Remove(int position)
@ -142,4 +147,10 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
{
throw new NotImplementedException();
}
}

View File

@ -16,19 +16,19 @@ public class StorageCollection<T>
/// <summary>
/// Словарь (хранилище) с коллекциями
/// </summary>
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
readonly Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
/// <summary>
/// Возвращение списка названий коллекций
/// </summary>
public List<string> Keys => _storages.Keys.ToList();
public List<CollectionInfo> Keys => _storages.Keys.ToList();
/// <summary>
/// Конструктор
/// </summary>
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
}
/// <summary>
/// Ключевое слово, с которого должен начинаться файл
@ -100,7 +100,7 @@ public class StorageCollection<T>
}
}
public bool SaveData(string filename)
public bool SaveData(string filename)//переделать
{
if (_storages.Count == 0)
{
@ -113,7 +113,7 @@ public class StorageCollection<T>
using (StreamWriter writer = new(filename))
{
writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
{
writer.Write(Environment.NewLine);
// не сохраняем пустые коллекции
@ -148,7 +148,7 @@ public class StorageCollection<T>
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
public bool LoadData(string filename)
public bool LoadData(string filename) //переделать
{
if (!File.Exists(filename))
{

View File

@ -0,0 +1,22 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShipProject.Drawnings
{
/// <summary>
/// Сравнение по цвету
/// </summary>
public class DarwningWarmlyShipCompareByColor : IComparer<DrawningWarmlyShip?>
{
//TODO
public int Compare(DrawningWarmlyShip? x, DrawningWarmlyShip? y)
{
//TODO По скорости цвету весу
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShipProject.Drawnings
{
public class DrawningWarmlyShipCompareByType : IComparer<DrawningWarmlyShip?>
{
/// <summary>
/// Сравниваем по типу, скорости , весу.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(DrawningWarmlyShip? x, DrawningWarmlyShip? y)
{
if (x == null || x.EntityWarmlyShip == null) { return -1; }
if (y == null || y.EntityWarmlyShip == null) { return -1; }
if (x.GetType().Name != y.GetType().Name) { return x.GetType().Name.CompareTo(y.GetType().Name); }
var speedCompare = x.EntityWarmlyShip.Speed.CompareTo(y.EntityWarmlyShip?.Speed);
if (speedCompare != 0) { return speedCompare; }
return x.EntityWarmlyShip.Weight.CompareTo(y.EntityWarmlyShip.Weight);
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShipProject.Drawnings;
public class DrawningWarmlyShipEqutables : IEqualityComparer <DrawningWarmlyShip?>
{
public bool Equals(DrawningWarmlyShip? x, DrawningWarmlyShip? y)
{
//Объекты не null и ссылки не null
if(x == null || x.EntityWarmlyShip == null) { return false; }
if( y == null || y.EntityWarmlyShip == null) { return false; }
// Типы объектов сравниваем
if(x.GetType().Name != y.GetType().Name) { return false;}
if(x.EntityWarmlyShip.Speed != y.EntityWarmlyShip.Speed) { return false; }
if(x.EntityWarmlyShip.Weight != y.EntityWarmlyShip.Weight) { return false; }
if(x.EntityWarmlyShip.BodyColor != x.EntityWarmlyShip.BodyColor) { return false; }
if(x is DrawningWarmlyShip2 && y is DrawningWarmlyShip2)
{
//TODO логика сравнения дополнительных параметров
}
return true;
}
public int GetHashCode([DisallowNull] DrawningWarmlyShip2 obj)
{
return obj.GetHashCode();
}
}

View File

@ -54,6 +54,8 @@
loadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
buttonSortByColor = new Button();
buttonSortByType = new Button();
groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout();
panelStorage.SuspendLayout();
@ -66,17 +68,19 @@
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(534, 28);
groupBoxTools.Location = new Point(537, 28);
groupBoxTools.Margin = new Padding(3, 4, 3, 4);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Padding = new Padding(3, 4, 3, 4);
groupBoxTools.Size = new Size(240, 649);
groupBoxTools.Size = new Size(240, 724);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonSortByColor);
panelCompanyTools.Controls.Add(buttonSortByType);
panelCompanyTools.Controls.Add(buttonCreateCompany);
panelCompanyTools.Controls.Add(buttonAddWarmlyShip);
panelCompanyTools.Controls.Add(comboBoxSelectorCompany);
@ -87,7 +91,7 @@
panelCompanyTools.Location = new Point(7, 384);
panelCompanyTools.Margin = new Padding(3, 4, 3, 4);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(219, 269);
panelCompanyTools.Size = new Size(219, 340);
panelCompanyTools.TabIndex = 8;
//
// buttonCreateCompany
@ -107,7 +111,7 @@
buttonAddWarmlyShip.Location = new Point(3, 95);
buttonAddWarmlyShip.Margin = new Padding(3, 4, 3, 4);
buttonAddWarmlyShip.Name = "buttonAddWarmlyShip";
buttonAddWarmlyShip.Size = new Size(203, 31);
buttonAddWarmlyShip.Size = new Size(212, 31);
buttonAddWarmlyShip.TabIndex = 1;
buttonAddWarmlyShip.Text = "Добавление теплохода";
buttonAddWarmlyShip.UseVisualStyleBackColor = true;
@ -122,17 +126,17 @@
comboBoxSelectorCompany.Location = new Point(3, 4);
comboBoxSelectorCompany.Margin = new Padding(3, 4, 3, 4);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(203, 28);
comboBoxSelectorCompany.Size = new Size(212, 28);
comboBoxSelectorCompany.TabIndex = 0;
comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged;
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(114, 208);
buttonRefresh.Location = new Point(107, 290);
buttonRefresh.Margin = new Padding(3, 4, 3, 4);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(93, 50);
buttonRefresh.Size = new Size(108, 50);
buttonRefresh.TabIndex = 5;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
@ -144,7 +148,7 @@
buttonAddWarmlyShip2.Location = new Point(3, 134);
buttonAddWarmlyShip2.Margin = new Padding(3, 4, 3, 4);
buttonAddWarmlyShip2.Name = "buttonAddWarmlyShip2";
buttonAddWarmlyShip2.Size = new Size(203, 31);
buttonAddWarmlyShip2.Size = new Size(212, 31);
buttonAddWarmlyShip2.TabIndex = 2;
buttonAddWarmlyShip2.Text = "Добавление крутого теплохода";
buttonAddWarmlyShip2.UseVisualStyleBackColor = true;
@ -152,10 +156,10 @@
// buttonDelWarmlyShip
//
buttonDelWarmlyShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDelWarmlyShip.Location = new Point(3, 208);
buttonDelWarmlyShip.Location = new Point(0, 290);
buttonDelWarmlyShip.Margin = new Padding(3, 4, 3, 4);
buttonDelWarmlyShip.Name = "buttonDelWarmlyShip";
buttonDelWarmlyShip.Size = new Size(105, 50);
buttonDelWarmlyShip.Size = new Size(108, 50);
buttonDelWarmlyShip.TabIndex = 3;
buttonDelWarmlyShip.Text = "Удаление теплохода";
buttonDelWarmlyShip.UseVisualStyleBackColor = true;
@ -168,7 +172,7 @@
maskedTextBox.Margin = new Padding(3, 4, 3, 4);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(203, 27);
maskedTextBox.Size = new Size(212, 27);
maskedTextBox.TabIndex = 2;
maskedTextBox.ValidatingType = typeof(int);
//
@ -267,7 +271,7 @@
buttonTests.Location = new Point(349, 505);
buttonTests.Margin = new Padding(3, 4, 3, 4);
buttonTests.Name = "buttonTests";
buttonTests.Size = new Size(168, 29);
buttonTests.Size = new Size(171, 29);
buttonTests.TabIndex = 4;
buttonTests.Text = "Передать на Тесты";
buttonTests.UseVisualStyleBackColor = true;
@ -279,7 +283,7 @@
pictureBox.Location = new Point(0, 28);
pictureBox.Margin = new Padding(3, 4, 3, 4);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(534, 649);
pictureBox.Size = new Size(537, 724);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
@ -289,7 +293,7 @@
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(774, 28);
menuStrip.Size = new Size(777, 28);
menuStrip.TabIndex = 5;
menuStrip.Text = "menuStrip";
//
@ -324,11 +328,35 @@
//
saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByColor
//
buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonSortByColor.Location = new Point(107, 218);
buttonSortByColor.Margin = new Padding(3, 4, 3, 4);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(108, 50);
buttonSortByColor.TabIndex = 9;
buttonSortByColor.Text = "Сортировать по цвету ";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonSortByType.Location = new Point(-1, 218);
buttonSortByType.Margin = new Padding(3, 4, 3, 4);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(108, 50);
buttonSortByType.TabIndex = 8;
buttonSortByType.Text = "Сортировать по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// FormWarmlyShipCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(774, 677);
ClientSize = new Size(777, 752);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Controls.Add(buttonTests);
@ -377,5 +405,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -324,6 +324,24 @@ public partial class FormWarmlyShipCollection : Form
}
}
private void buttonSortByType_Click(object sender, EventArgs e)
{
CompareWarmlyShip(new DrawningWarmlyShipCompareByType());
}
private void buttonSortByColor_Click(object sender, EventArgs e)
{
CompareWarmlyShip(new DarwningWarmlyShipCompareByColor());
}
private void CompareWarmlyShip(IComparer<DrawningWarmlyShip?> comparer)
{
if(_company == null)
{
return;
}
_company.Sort(comparer);
pictureBox.Image = _company.Show();
}
}