"Философия есть познание того, что есть" - Гегель

This commit is contained in:
Arkadiy Radaev 2023-12-13 11:58:02 +04:00
parent b2c527eccd
commit 737406f412
9 changed files with 264 additions and 36 deletions

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class CatamaranCollectionInfo : IEquatable<CatamaranCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public CatamaranCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(CatamaranCollectionInfo? other)
{
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class CatamaranCompareByColor: IComparer<DrawningCatamaran?>
{
public int Compare(DrawningCatamaran? x, DrawningCatamaran? y)
{
if (x == null || x.EntityCatamaran == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityCatamaran == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityCatamaran.BodyColor.Name != y.EntityCatamaran.BodyColor.Name)
{
return x.EntityCatamaran.BodyColor.Name.CompareTo(y.EntityCatamaran.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is DrawningCatamaran)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawningCatamaranPro)
{
EntityCatamaranPro EntityX = (EntityCatamaranPro)x.EntityCatamaran;
EntityCatamaranPro EntityY = (EntityCatamaranPro)y.EntityCatamaran;
if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name)
{
return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name);
}
}
var speedCompare = x.EntityCatamaran.Speed.CompareTo(y.EntityCatamaran.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityCatamaran.Weight.CompareTo(y.EntityCatamaran.Weight);
}
}
}

View File

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

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.Logging;
namespace Catamaran
{
@ -21,7 +22,9 @@ namespace Catamaran
private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public CatamaransGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -35,7 +38,7 @@ namespace Catamaran
{
if (obj == null)
return false;
return collect?._collection.Insert(obj) ?? false;
return collect?._collection.Insert(obj, new DrawningCatamaranEqutables()) ?? false;
}
public static T? operator -(CatamaransGenericCollection<T, U> collect, int pos)

View File

@ -8,10 +8,9 @@ namespace Catamaran
{
internal class CatamaransGenericStorage
{
readonly Dictionary<string, CatamaransGenericCollection<DrawningCatamaran,
DrawningObjectCatamaran>> _catStorages;
public List<string> Keys => _catStorages.Keys.ToList();
readonly Dictionary<CatamaranCollectionInfo, CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>> _catStorages;
public List<CatamaranCollectionInfo> Keys => _catStorages.Keys.ToList();
private readonly int _pictureWidth;
@ -25,8 +24,8 @@ namespace Catamaran
public CatamaransGenericStorage(int pictureWidth, int pictureHeight)
{
_catStorages = new Dictionary<string,
CatamaransGenericCollection<DrawningCatamaran,DrawningObjectCatamaran>>();
_catStorages = new Dictionary<CatamaranCollectionInfo,
CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -38,7 +37,7 @@ namespace Catamaran
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<CatamaranCollectionInfo,
CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>> record in _catStorages)
{
StringBuilder records = new();
@ -46,7 +45,7 @@ namespace Catamaran
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
@ -108,7 +107,7 @@ namespace Catamaran
}
}
}
_catStorages.Add(record[0], collection);
_catStorages.Add(new CatamaranCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);
@ -118,22 +117,23 @@ namespace Catamaran
public void AddSet(string name)
{
_catStorages.Add(name, new CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>(_pictureWidth, _pictureHeight));
_catStorages.Add(new CatamaranCollectionInfo(name, string.Empty), new CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_catStorages.ContainsKey(name))
if (!_catStorages.ContainsKey(new CatamaranCollectionInfo(name, string.Empty)))
return;
_catStorages.Remove(name);
_catStorages.Remove(new CatamaranCollectionInfo(name, string.Empty));
}
public CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>? this[string ind]
{
get
{
if (_catStorages.ContainsKey(ind))
return _catStorages[ind];
CatamaranCollectionInfo indObj = new CatamaranCollectionInfo(ind, string.Empty);
if (_catStorages.ContainsKey(indObj))
return _catStorages[indObj];
return null;
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class DrawningCatamaranEqutables: IEqualityComparer<DrawningCatamaran?>
{
public bool Equals(DrawningCatamaran? x, DrawningCatamaran? y)
{
if (x == null || x.EntityCatamaran == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCatamaran == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityCatamaran.Speed != y.EntityCatamaran.Speed)
{
return false;
}
if (x.EntityCatamaran.Weight != y.EntityCatamaran.Weight)
{
return false;
}
if (x.EntityCatamaran.BodyColor != y.EntityCatamaran.BodyColor)
{
return false;
}
if (x is DrawningCatamaranPro && y is DrawningCatamaranPro)
{
EntityCatamaranPro EntityX = (EntityCatamaranPro)x.EntityCatamaran;
EntityCatamaranPro EntityY = (EntityCatamaranPro)y.EntityCatamaran;
if (EntityX.Motor != EntityY.Motor)
return false;
if (EntityX.BodyKit != EntityY.BodyKit)
return false;
if (EntityX.Sail != EntityY.Sail)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningCatamaran obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -21,6 +21,8 @@
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormCatamaranCollection));
groupBoxCollection=new GroupBox();
buttonSortByColor=new Button();
buttonSortByType=new Button();
setsBox=new GroupBox();
DeleteSetButton=new Button();
SetslistBox=new ListBox();
@ -95,6 +97,8 @@
//
// groupBoxCollection
//
groupBoxCollection.Controls.Add(buttonSortByColor);
groupBoxCollection.Controls.Add(buttonSortByType);
groupBoxCollection.Controls.Add(setsBox);
groupBoxCollection.Controls.Add(maskedTextBox);
groupBoxCollection.Controls.Add(buttonUpdateCollection);
@ -107,13 +111,33 @@
groupBoxCollection.TabStop=false;
groupBoxCollection.Text="Инструменты";
//
// buttonSortByColor
//
buttonSortByColor.Location=new Point(21, 410);
buttonSortByColor.Name="buttonSortByColor";
buttonSortByColor.Size=new Size(204, 34);
buttonSortByColor.TabIndex=5;
buttonSortByColor.Text="Сортировать по цвету";
buttonSortByColor.UseVisualStyleBackColor=true;
buttonSortByColor.Click+=ButtonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Location=new Point(21, 361);
buttonSortByType.Name="buttonSortByType";
buttonSortByType.Size=new Size(204, 34);
buttonSortByType.TabIndex=4;
buttonSortByType.Text="Сортировать по типу";
buttonSortByType.UseVisualStyleBackColor=true;
buttonSortByType.Click+=ButtonSortByType_Click;
//
// setsBox
//
setsBox.Controls.Add(DeleteSetButton);
setsBox.Controls.Add(SetslistBox);
setsBox.Controls.Add(setAddBox);
setsBox.Controls.Add(AddSetButton);
setsBox.Location=new Point(6, 41);
setsBox.Location=new Point(6, 30);
setsBox.Name="setsBox";
setsBox.RightToLeft=RightToLeft.No;
setsBox.Size=new Size(219, 305);
@ -160,16 +184,16 @@
//
// maskedTextBox
//
maskedTextBox.Location=new Point(21, 457);
maskedTextBox.Location=new Point(21, 525);
maskedTextBox.Name="maskedTextBox";
maskedTextBox.Size=new Size(204, 31);
maskedTextBox.TabIndex=2;
//
// buttonUpdateCollection
//
buttonUpdateCollection.Location=new Point(21, 577);
buttonUpdateCollection.Location=new Point(21, 606);
buttonUpdateCollection.Name="buttonUpdateCollection";
buttonUpdateCollection.Size=new Size(204, 54);
buttonUpdateCollection.Size=new Size(204, 39);
buttonUpdateCollection.TabIndex=2;
buttonUpdateCollection.Text="Обновить коллекцию";
buttonUpdateCollection.UseVisualStyleBackColor=true;
@ -177,9 +201,9 @@
//
// buttonDeleteCat
//
buttonDeleteCat.Location=new Point(21, 504);
buttonDeleteCat.Location=new Point(21, 562);
buttonDeleteCat.Name="buttonDeleteCat";
buttonDeleteCat.Size=new Size(204, 54);
buttonDeleteCat.Size=new Size(204, 38);
buttonDeleteCat.TabIndex=1;
buttonDeleteCat.Text="Удалить катамаран";
buttonDeleteCat.UseVisualStyleBackColor=true;
@ -187,9 +211,9 @@
//
// buttonAddCat
//
buttonAddCat.Location=new Point(21, 385);
buttonAddCat.Location=new Point(21, 481);
buttonAddCat.Name="buttonAddCat";
buttonAddCat.Size=new Size(204, 54);
buttonAddCat.Size=new Size(204, 38);
buttonAddCat.TabIndex=0;
buttonAddCat.Text="Добавить катамаран";
buttonAddCat.UseVisualStyleBackColor=true;
@ -661,5 +685,7 @@
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранитьToolStripMenuItem2;
private ToolStripMenuItem загрузитьToolStripMenuItem;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -31,7 +31,7 @@ namespace Catamaran
SetslistBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
SetslistBox.Items.Add(_storage.Keys[i]);
SetslistBox.Items.Add(_storage.Keys[i].Name);
}
if (SetslistBox.Items.Count > 0 && (index == -1 || index
>= SetslistBox.Items.Count))
@ -43,11 +43,11 @@ namespace Catamaran
{
SetslistBox.SelectedIndex = index;
}
}
private void ButtonAddStorage_Click(object sender, EventArgs e)
{
string storname = setAddBox.Text;
if (string.IsNullOrEmpty(setAddBox.Text) || _storage.Keys.Contains(storname))
if (string.IsNullOrEmpty(setAddBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
@ -55,6 +55,7 @@ namespace Catamaran
}
_storage.AddSet(setAddBox.Text);
ReloadObjects();
Log.Information($"Добавлен набор: {setAddBox.Text}");
}
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
@ -71,9 +72,11 @@ namespace Catamaran
}
if (MessageBox.Show($"Удалить набор {SetslistBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(SetslistBox.SelectedItem.ToString()
?? string.Empty);
string name = SetslistBox.SelectedItem.ToString()
?? string.Empty;
_storage.DelSet(name);
ReloadObjects();
Log.Information($"Удален набор: {name}");
}
}
@ -106,9 +109,13 @@ namespace Catamaran
Log.Warning($"Коллекция {SetslistBox.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
}
catch (ArgumentException)
{
Log.Warning($"Добавляемый объект уже существует в коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(catamaranDelegate);
}
private void ButtonRemoveCatamaran_Click(object sender, EventArgs e)
@ -141,7 +148,7 @@ namespace Catamaran
Log.Warning($"Не получилось удалить объект из коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show(ex.Message);
}
catch (FormatException ex)
catch (FormatException)
{
Log.Warning($"Было введено не число");
MessageBox.Show("Введите число");
@ -177,7 +184,6 @@ namespace Catamaran
{
Log.Warning("Не удалось сохранить");
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
@ -200,10 +206,29 @@ namespace Catamaran
catch (Exception ex)
{
Log.Warning("Не удалось загрузить");
MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByColor());
private void CompareCatamarans(IComparer<DrawningCatamaran?> comparer)
{
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowCats();
}
}
}

View File

@ -18,21 +18,27 @@ namespace Catamaran
_maxCount = count;
_places = new List<T?>(count);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public bool Insert(T catamaran)
public bool Insert(T catamaran, IEqualityComparer<T?>? equal = null)
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(catamaran, 0);
Insert(catamaran, 0, equal);
return true;
}
public bool Insert(T catamaran, int position)
public bool Insert(T catamaran, int position, IEqualityComparer<T?>? equal = null)
{
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
return false;
if (equal != null)
{
if (_places.Contains(catamaran, equal))
throw new ArgumentException(nameof(catamaran));
}
_places.Insert(position, catamaran);
return true;
}