This commit is contained in:
Alenka 2023-12-23 15:06:55 +04:00
parent b6611a468c
commit 0f3e9f9a46
10 changed files with 293 additions and 33 deletions

View File

@ -0,0 +1,24 @@
using Cruiser.Generics;
namespace Cruiser
{
internal class CruiserCollectionInfo : IEquatable<CruiserCollectionInfo?>
{
public string Name { get; private set; }
public string Description { get; private set; }
public CruiserCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(CruiserCollectionInfo? other)
{
if (other == null || other.Name == null)
throw new ArgumentNullException(nameof(other));
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
using Cruiser.Entities;
namespace Cruiser.Generics
{
internal class CruiserCompareByColor : IComparer<DrawningCruiser>
{
public int Compare(DrawningCruiser? x, DrawningCruiser? y)
{
if (x == null || x.EntityCruiser == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityCruiser == null)
throw new ArgumentNullException(nameof(y));
var xCruiser = x.EntityCruiser;
var yCruiser = y.EntityCruiser;
if (xCruiser.BodyColor != yCruiser.BodyColor)
return xCruiser.BodyColor.Name.CompareTo(yCruiser.BodyColor.Name);
var speedCompare = x.EntityCruiser.Speed.CompareTo(y.EntityCruiser.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityCruiser.Weight.CompareTo(y.EntityCruiser.Weight);
}
}
}

View File

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

View File

@ -18,8 +18,9 @@ namespace Cruiser.Generics
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 170;
private readonly int _placeSizeHeight = 124;
private readonly int _placeSizeHeight = 200;
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public CruiserGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -32,7 +33,7 @@ namespace Cruiser.Generics
{
if (obj == null || collect == null)
return false;
collect?._collection.Insert(obj);
collect?._collection.Insert(obj, new DrawningCruiserEqutables());
return true;
}
public static T? operator -(CruiserGenericCollection<T, U> collect, int pos)
@ -77,7 +78,7 @@ namespace Cruiser.Generics
if (cruiser != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
cruiser.SetPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight);
cruiser.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeHeight / 2 - 8, i / inRow * _placeSizeHeight + 20);
cruiser.DrawTransport(g);
}
i++;

View File

@ -10,9 +10,9 @@ namespace Cruiser.Generics
{
internal class CruiserGenericStorage
{
readonly Dictionary<string, CruiserGenericCollection<DrawningCruiser,
readonly Dictionary<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser,
DrawningObjectCruiser>> _cruiserStorages;
public List<string> Keys => _cruiserStorages.Keys.ToList();
public List<CruiserCollectionInfo> Keys => _cruiserStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
@ -20,31 +20,30 @@ namespace Cruiser.Generics
private static readonly char _separatorForObject = ':';
public CruiserGenericStorage(int pictureWidth, int pictureHeight)
{
_cruiserStorages = new Dictionary<string, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>>();
_cruiserStorages = new Dictionary<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_cruiserStorages.Add(name, new CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>(_pictureWidth, _pictureHeight));
_cruiserStorages.Add(new CruiserCollectionInfo(name, string.Empty), new CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_cruiserStorages.ContainsKey(name))
if (!_cruiserStorages.ContainsKey(new CruiserCollectionInfo(name, string.Empty)));
{
return;
}
_cruiserStorages.Remove(name);
_cruiserStorages.Remove(new CruiserCollectionInfo(name, string.Empty));
}
public CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>? this[string ind]
{
get
{
if (_cruiserStorages.ContainsKey(ind))
{
return _cruiserStorages[ind];
}
CruiserCollectionInfo indObj = new CruiserCollectionInfo(ind, string.Empty);
if (_cruiserStorages.ContainsKey(indObj))
return _cruiserStorages[indObj];
return null;
}
}
@ -55,22 +54,22 @@ namespace Cruiser.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>> record in _cruiserStorages)
foreach (KeyValuePair<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>> record in _cruiserStorages)
{
StringBuilder records = new();
foreach (DrawningCruiser? elem in record.Value.GetCruisers)
{
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 IOException("Невалидная операция, нет данных для сохранения");
throw new Exception("Невалидная операция, нет данных для сохранения");
}
using (StreamWriter sw = new(filename))
{
sw.WriteLine($"CruiserStorage{Environment.NewLine}{data}");
sw.WriteLine($"PlaneStorage{Environment.NewLine}{data}");
}
return;
}
@ -88,7 +87,7 @@ namespace Cruiser.Generics
{
throw new IOException("Нет данных для загрузки");
}
if (!strs[0].StartsWith("CruiserStorage"))
if (!strs[0].StartsWith("PlaneStorage"))
{
throw new IOException("Неверный формат данных");
}
@ -115,7 +114,7 @@ namespace Cruiser.Generics
}
}
}
_cruiserStorages.Add(record[0], collection);
_cruiserStorages.Add(new CruiserCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != 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;
using Cruiser.DrawningObjects;
using Cruiser.Entities;
namespace Cruiser
{
internal class DrawningCruiserEqutables : IEqualityComparer<DrawningCruiser?>
{
public bool Equals(DrawningCruiser? x, DrawningCruiser? y)
{
if (x == null || x.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityCruiser.Speed != y.EntityCruiser.Speed)
{
return false;
}
if (x.EntityCruiser.Weight != y.EntityCruiser.Weight)
{
return false;
}
if (x.EntityCruiser.BodyColor != y.EntityCruiser.BodyColor)
{
return false;
}
if (x is DrawningAdvancedCruiser && y is DrawningAdvancedCruiser)
{
EntityAdvancedCruiser EntityX = (EntityAdvancedCruiser)x.EntityCruiser;
EntityAdvancedCruiser EntityY = (EntityAdvancedCruiser)y.EntityCruiser;
if (EntityX.HelicopterPad != EntityY.HelicopterPad)
return false;
if (EntityX.Coating != EntityY.Coating)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningCruiser? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -0,0 +1,57 @@
using Cruiser.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
namespace Cruiser
{
internal class DrawingCruiserEqutables : IEqualityComparer<DrawningCruiser?>
{
public bool Equals(DrawningCruiser? x, DrawningCruiser? y)
{
if (x == null || x.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityCruiser.Speed != y.EntityCruiser.Speed)
{
return false;
}
if (x.EntityCruiser.Weight != y.EntityCruiser.Weight)
{
return false;
}
if (x.EntityCruiser.BodyColor != y.EntityCruiser.BodyColor)
{
return false;
}
if (x is DrawningAdvancedCruiser && y is DrawningAdvancedCruiser)
{
EntityAdvancedCruiser EntityX = (EntityAdvancedCruiser)x.EntityCruiser;
EntityAdvancedCruiser EntityY = (EntityAdvancedCruiser)y.EntityCruiser;
if (EntityX.HelicopterPad != EntityY.HelicopterPad)
return false;
if (EntityX.Coating != EntityY.Coating)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningCruiser? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -32,6 +32,8 @@
this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit();
this.panelCruiser.SuspendLayout();
this.panelSet.SuspendLayout();
@ -43,26 +45,28 @@
this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 33);
this.pictureBoxCruiser.Name = "pictureBoxCruiser";
this.pictureBoxCruiser.Size = new System.Drawing.Size(904, 477);
this.pictureBoxCruiser.Size = new System.Drawing.Size(1083, 556);
this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxCruiser.TabIndex = 0;
this.pictureBoxCruiser.TabStop = false;
//
// panelCruiser
//
this.panelCruiser.Controls.Add(this.buttonSortByColor);
this.panelCruiser.Controls.Add(this.buttonSortByType);
this.panelCruiser.Controls.Add(this.ButtonRefreshCollection);
this.panelCruiser.Controls.Add(this.ButtonRemoveCruiser);
this.panelCruiser.Controls.Add(this.textBoxCruiser);
this.panelCruiser.Controls.Add(this.ButtonAddCruiser);
this.panelCruiser.Controls.Add(this.panelSet);
this.panelCruiser.Location = new System.Drawing.Point(655, 12);
this.panelCruiser.Location = new System.Drawing.Point(786, 36);
this.panelCruiser.Name = "panelCruiser";
this.panelCruiser.Size = new System.Drawing.Size(221, 486);
this.panelCruiser.Size = new System.Drawing.Size(221, 553);
this.panelCruiser.TabIndex = 1;
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 435);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 500);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(138, 41);
this.ButtonRefreshCollection.TabIndex = 2;
@ -72,7 +76,7 @@
//
// ButtonRemoveCruiser
//
this.ButtonRemoveCruiser.Location = new System.Drawing.Point(45, 391);
this.ButtonRemoveCruiser.Location = new System.Drawing.Point(45, 456);
this.ButtonRemoveCruiser.Name = "ButtonRemoveCruiser";
this.ButtonRemoveCruiser.Size = new System.Drawing.Size(138, 38);
this.ButtonRemoveCruiser.TabIndex = 2;
@ -82,7 +86,7 @@
//
// textBoxCruiser
//
this.textBoxCruiser.Location = new System.Drawing.Point(45, 330);
this.textBoxCruiser.Location = new System.Drawing.Point(50, 419);
this.textBoxCruiser.Name = "textBoxCruiser";
this.textBoxCruiser.Size = new System.Drawing.Size(133, 31);
this.textBoxCruiser.TabIndex = 3;
@ -161,7 +165,7 @@
this.FileToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(904, 33);
this.menuStrip.Size = new System.Drawing.Size(1083, 33);
this.menuStrip.TabIndex = 2;
this.menuStrip.Text = "menuStrip";
//
@ -177,22 +181,42 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(200, 34);
this.SaveToolStripMenuItem.Text = "Сохранить";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(200, 34);
this.LoadToolStripMenuItem.Text = "Загрузить";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(50, 320);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(128, 33);
this.buttonSortByType.TabIndex = 4;
this.buttonSortByType.Text = "Сортировка по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(50, 372);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(133, 27);
this.buttonSortByColor.TabIndex = 5;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormCruiserCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(904, 510);
this.ClientSize = new System.Drawing.Size(1083, 589);
this.Controls.Add(this.panelCruiser);
this.Controls.Add(this.pictureBoxCruiser);
this.Controls.Add(this.menuStrip);
@ -229,5 +253,7 @@
private ToolStripMenuItem FileToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -32,7 +32,7 @@ namespace Cruiser
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))
@ -102,6 +102,11 @@ namespace Cruiser
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
}
catch (ArgumentException)
{
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(cruiserDelegate);
}
@ -198,5 +203,24 @@ namespace Cruiser
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new CruiserCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new CruiserCompareByColor());
private void ComparePlanes(IComparer<DrawningCruiser?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCruiser.Image = obj.ShowCruisers();
}
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Exceptions;
using System.Numerics;
namespace Cruiser.Generics
{
@ -20,19 +21,26 @@ namespace Cruiser.Generics
_places = new List<T?>(count);
}
public void Insert(T cruiser)
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public void Insert(T cruiser, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(cruiser, 0);
Insert(cruiser, 0, equal);
}
public void Insert(T cruiser, int position)
public void Insert(T cruiser, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
throw new Exception("Неверная позиция для вставки");
if (equal != null)
{
if (_places.Contains(cruiser, equal))
throw new ArgumentException(nameof(cruiser));
}
_places.Insert(position, cruiser);
}
public void Remove(int position)