Иногда мы не понимаем, почему что-то происходит, но в конечном итоге убеждаемся, что каждое событие имело смысл. Все происходит по причине, и в конце концов, все будет хорошо. Доверьтесь Божьему плану и идите вперед с верой, даже если не видите всей картины.

This commit is contained in:
goblinrf 2023-12-11 17:54:02 +03:00
parent 53d754014b
commit 34f8f26916
9 changed files with 251 additions and 20 deletions

View File

@ -0,0 +1,49 @@
using ProjectAirFighter.DrawningObjects;
using ProjectAirFighter.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirFighter.Generics
{
internal class AirplaneCompareByColor : IComparer<DrawningAirplane?>
{
public int Compare(DrawningAirplane? x, DrawningAirplane? y)
{
if (x == null || x.EntityAirplane == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityAirplane == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityAirplane.BodyColor.GetBrightness() != y.EntityAirplane.BodyColor.GetBrightness())
{
return x.EntityAirplane.BodyColor.GetBrightness().CompareTo(y.EntityAirplane.BodyColor.GetBrightness());
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is DrawningAirplane)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawningAirFighter)
{
EntityAirFighter EntityX = (EntityAirFighter)x.EntityAirplane;
EntityAirFighter EntityY = (EntityAirFighter)y.EntityAirplane;
if (EntityX.AdditionalColor.GetBrightness() != EntityY.AdditionalColor.GetBrightness())
{
return EntityX.AdditionalColor.GetBrightness().CompareTo(EntityY.AdditionalColor.GetBrightness());
}
}
var speedCompare = x.EntityAirplane.Speed.CompareTo(y.EntityAirplane.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityAirplane.Weight.CompareTo(y.EntityAirplane.Weight);
}
}
}

View File

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

View File

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

View File

@ -1,6 +1,7 @@
using ProjectAirFighter.MovementStrategy;
using ProjectAirFighter.DrawningObjects;
using System.Drawing;
using Microsoft.VisualBasic.Logging;
namespace ProjectAirFighter.Generics
{
@ -19,6 +20,8 @@ namespace ProjectAirFighter.Generics
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public AirplaneslGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -33,7 +36,7 @@ namespace ProjectAirFighter.Generics
{
if (obj == null)
return false;
return collect?._collection.Insert(obj) ?? false;
return collect?._collection.Insert(obj, new DrawningAirplaneEqutables()) ?? false;
}
public static T? operator -(AirplaneslGenericCollection<T, U> collect, int

View File

@ -12,8 +12,9 @@ namespace ProjectAirFighter.Generics
{
internal class AirplanesGenericStorage
{
readonly Dictionary<string, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
public List<string> Keys => _airplaneStorages.Keys.ToList();
readonly Dictionary<AirplaneCollectionInfo, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
public List<AirplaneCollectionInfo> Keys => _airplaneStorages.Keys.ToList();
private readonly int _pictureWidth;
@ -24,7 +25,7 @@ namespace ProjectAirFighter.Generics
private static readonly char _separatorForObject = ':';
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
_airplaneStorages = new Dictionary<string, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_airplaneStorages = new Dictionary<AirplaneCollectionInfo, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -35,7 +36,7 @@ namespace ProjectAirFighter.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<AirplaneCollectionInfo,
AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplaneStorages)
{
StringBuilder records = new();
@ -43,7 +44,7 @@ namespace ProjectAirFighter.Generics
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
@ -111,7 +112,8 @@ StringSplitOptions.RemoveEmptyEntries);
}
}
}
_airplaneStorages.Add(record[0], collection);
_airplaneStorages.Add(new AirplaneCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);
@ -120,22 +122,23 @@ StringSplitOptions.RemoveEmptyEntries);
}
public void AddSet(string name)
{
_airplaneStorages.Add(name, new AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
_airplaneStorages.Add(new AirplaneCollectionInfo(name, string.Empty), new AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_airplaneStorages.ContainsKey(name))
if (!_airplaneStorages.ContainsKey(new AirplaneCollectionInfo(name, string.Empty)))
return;
_airplaneStorages.Remove(name);
_airplaneStorages.Remove(new AirplaneCollectionInfo(name, string.Empty));
}
public AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>? this[string ind]
{
get
{
if (_airplaneStorages.ContainsKey(ind))
return _airplaneStorages[ind];
AirplaneCollectionInfo indObj = new AirplaneCollectionInfo(ind, string.Empty);
if (_airplaneStorages.ContainsKey(indObj))
return _airplaneStorages[indObj];
return null;
}
}

View File

@ -0,0 +1,59 @@
using ProjectAirFighter.DrawningObjects;
using ProjectAirFighter.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirFighter.Generics
{
internal class DrawningAirplaneEqutables : IEqualityComparer<DrawningAirplane?>
{
public bool Equals(DrawningAirplane? x, DrawningAirplane? y)
{
if (x == null || x.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityAirplane.Speed != y.EntityAirplane.Speed)
{
return false;
}
if (x.EntityAirplane.Weight != y.EntityAirplane.Weight)
{
return false;
}
if (x.EntityAirplane.BodyColor != y.EntityAirplane.BodyColor)
{
return false;
}
if (x is DrawningAirFighter && y is DrawningAirFighter)
{
EntityAirFighter EntityX = (EntityAirFighter)x.EntityAirplane;
EntityAirFighter EntityY = (EntityAirFighter)y.EntityAirplane;
if (EntityX.Racket != EntityY.Racket)
return false;
if (EntityX.Wing != EntityY.Wing)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAirplane obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.toolGroupBox = new System.Windows.Forms.GroupBox();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.kitGroupbox = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
@ -53,6 +55,8 @@
//
// toolGroupBox
//
this.toolGroupBox.Controls.Add(this.ButtonSortByColor);
this.toolGroupBox.Controls.Add(this.ButtonSortByType);
this.toolGroupBox.Controls.Add(this.maskedTextBoxNumber);
this.toolGroupBox.Controls.Add(this.kitGroupbox);
this.toolGroupBox.Controls.Add(this.updateCollectionButton);
@ -66,9 +70,29 @@
this.toolGroupBox.TabStop = false;
this.toolGroupBox.Text = "Инструменты";
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(8, 409);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(213, 35);
this.ButtonSortByColor.TabIndex = 10;
this.ButtonSortByColor.Text = "Сортировка по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(8, 370);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(216, 33);
this.ButtonSortByType.TabIndex = 9;
this.ButtonSortByType.Text = "Сортировка по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(7, 406);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(6, 496);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(215, 27);
this.maskedTextBoxNumber.TabIndex = 7;
@ -125,7 +149,7 @@
//
// updateCollectionButton
//
this.updateCollectionButton.Location = new System.Drawing.Point(6, 505);
this.updateCollectionButton.Location = new System.Drawing.Point(7, 575);
this.updateCollectionButton.Name = "updateCollectionButton";
this.updateCollectionButton.Size = new System.Drawing.Size(215, 40);
this.updateCollectionButton.TabIndex = 3;
@ -135,7 +159,7 @@
//
// deleteAirplaneButton
//
this.deleteAirplaneButton.Location = new System.Drawing.Point(6, 449);
this.deleteAirplaneButton.Location = new System.Drawing.Point(6, 529);
this.deleteAirplaneButton.Name = "deleteAirplaneButton";
this.deleteAirplaneButton.Size = new System.Drawing.Size(215, 40);
this.deleteAirplaneButton.TabIndex = 2;
@ -145,7 +169,7 @@
//
// addAirplaneButton
//
this.addAirplaneButton.Location = new System.Drawing.Point(8, 360);
this.addAirplaneButton.Location = new System.Drawing.Point(6, 450);
this.addAirplaneButton.Name = "addAirplaneButton";
this.addAirplaneButton.Size = new System.Drawing.Size(215, 40);
this.addAirplaneButton.TabIndex = 0;
@ -244,5 +268,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -78,7 +78,7 @@ namespace ProjectAirFighter
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))
@ -197,6 +197,11 @@ namespace ProjectAirFighter
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
}
catch (ArgumentException ex)
{
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(airplaneDelegate);
}
@ -224,5 +229,24 @@ MessageBoxIcon.Question) == DialogResult.Yes)
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByColor());
private void CompareAirplanes(IComparer<DrawningAirplane?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowAirplanes();
}
}
}

View File

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