This commit is contained in:
DavidMakarov 2023-12-15 21:47:09 +04:00
parent b53b5951f1
commit 40afc000b4
11 changed files with 248 additions and 36 deletions

View File

@ -0,0 +1,21 @@
namespace AirplaneWithRadar.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)
{
if (this.Name == other?.Name) { return true; }
return false;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,30 @@
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.Entities;
namespace AirplaneWithRadar
{
internal class AirplaneCompareByColor : IComparer<PaintAirplane?>
{
public int Compare(PaintAirplane? x, PaintAirplane? y)
{
if (x == null || x.AirplaneEntity == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.AirplaneEntity == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var colorCompare = x.AirplaneEntity.BodyColor.Name.CompareTo(y.AirplaneEntity.BodyColor.Name);
if (!(x is PaintAirplaneWithRadar && y is PaintAirplaneWithRadar))
{
return colorCompare;
}
return ((AirplaneWithRadarEntity)x.AirplaneEntity).AdditColor.Name.CompareTo(((AirplaneWithRadarEntity)y.AirplaneEntity).AdditColor.Name);
}
}
}

View File

@ -0,0 +1,30 @@
using AirplaneWithRadar.PaintObjects;
namespace AirplaneWithRadar.Generics
{
internal class AirplaneCompareByType : IComparer<PaintAirplane?>
{
public int Compare(PaintAirplane? x, PaintAirplane? y)
{
if (x == null || x.AirplaneEntity == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.AirplaneEntity == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.AirplaneEntity.Speed.CompareTo(y.AirplaneEntity.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.AirplaneEntity.Weight.CompareTo(y.AirplaneEntity.Weight);
}
}
}

View File

@ -8,8 +8,7 @@ namespace AirplaneWithRadar.Exceptions
public AirplaneNotFoundException(int i) : base($"Не найден объект по позиции { i}") { }
public AirplaneNotFoundException() : base() { }
public AirplaneNotFoundException(string message) : base(message) { }
public AirplaneNotFoundException(string message, Exception exception) : base(message, exception)
{ }
public AirplaneNotFoundException(string message, Exception exception) : base(message, exception) { }
protected AirplaneNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}
}

View File

@ -13,6 +13,8 @@ namespace AirplaneWithRadar.Generics
private readonly int placeSizeHeight = 70;
private readonly SetGeneric<T> collection;
public void SortSet(IComparer<T?> comparer) => collection.SortSet(comparer);
public AirplanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / placeSizeWidth;
@ -28,7 +30,7 @@ namespace AirplaneWithRadar.Generics
{
return -1;
}
return collect?.collection.Insert(obj);
return collect?.collection.Insert(obj, new PaintAirplaneEqutables());
}
public static bool operator -(AirplanesGenericCollection<T, U> collect, int pos)
{
@ -84,7 +86,6 @@ namespace AirplaneWithRadar.Generics
j--;
}
}
public IEnumerable<T?> GetAirplanes => collection.GetAirplanes();
}
}

View File

@ -8,8 +8,8 @@ namespace AirplaneWithRadar.Generics
internal class AirplanesGenericStorage
{
readonly Dictionary<string, AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>> airplaneStorages;
public List<string> Keys => airplaneStorages.Keys.ToList();
readonly Dictionary<AirplaneCollectionInfo, AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>> airplaneStorages;
public List<AirplaneCollectionInfo> Keys => airplaneStorages.Keys.ToList();
private readonly int pictWidth;
private readonly int pictHeight;
@ -18,7 +18,7 @@ namespace AirplaneWithRadar.Generics
private static readonly char separatorForObject = ':';
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
airplaneStorages = new Dictionary<string, AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>>();
airplaneStorages = new Dictionary<AirplaneCollectionInfo, AirplanesGenericCollection <PaintAirplane, PaintObjectAirplane>>();
pictWidth = pictureWidth;
pictHeight = pictureHeight;
}
@ -29,7 +29,7 @@ namespace AirplaneWithRadar.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>> record in airplaneStorages)
foreach (KeyValuePair<AirplaneCollectionInfo, AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>> record in airplaneStorages)
{
StringBuilder records = new();
foreach (PaintAirplane? elem in record.Value.GetAirplanes)
@ -99,7 +99,7 @@ namespace AirplaneWithRadar.Generics
}
}
}
airplaneStorages.Add(name, collection);
airplaneStorages.Add(new AirplaneCollectionInfo(name, String.Empty), collection);
}
return;
}
@ -107,20 +107,27 @@ namespace AirplaneWithRadar.Generics
public void AddSet(string name)
{
if (airplaneStorages.ContainsKey(name)) { return; }
airplaneStorages.Add(name, new AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>(pictWidth, pictHeight));
var newColl = new AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>(pictWidth, pictHeight);
if (Keys.Contains(new AirplaneCollectionInfo(name, string.Empty)))
{
MessageBox.Show("Набор с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (airplaneStorages.ContainsKey(new AirplaneCollectionInfo(name, String.Empty))) { return; }
airplaneStorages.Add(new AirplaneCollectionInfo(name, String.Empty), newColl);
}
public void DelSet(string name)
{
if (airplaneStorages.ContainsKey(name))
airplaneStorages.Remove(name);
if (airplaneStorages.ContainsKey(new AirplaneCollectionInfo(name, String.Empty)))
airplaneStorages.Remove(new AirplaneCollectionInfo(name, String.Empty));
}
public AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>? this[string ind]
{
get
{
if (airplaneStorages.ContainsKey(ind)) return airplaneStorages[ind];
return null;
if (!airplaneStorages.ContainsKey(new AirplaneCollectionInfo(ind, String.Empty)))
return null;
return airplaneStorages[new AirplaneCollectionInfo(ind, String.Empty)];
}
}
}

View File

@ -48,6 +48,8 @@
сохранитьToolStripMenuItem = new ToolStripMenuItem();
загрузитьToolStripMenuItem = new ToolStripMenuItem();
файлToolStripMenuItem = new ToolStripMenuItem();
buttonSortByColor = new Button();
buttonSortByType = new Button();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
@ -56,6 +58,8 @@
//
// groupBox1
//
groupBox1.Controls.Add(buttonSortByType);
groupBox1.Controls.Add(buttonSortByColor);
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(ButtonRefreshCollection);
@ -76,14 +80,14 @@
groupBox2.Controls.Add(listBoxStorages);
groupBox2.Location = new Point(13, 22);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(204, 209);
groupBox2.Size = new Size(204, 185);
groupBox2.TabIndex = 5;
groupBox2.TabStop = false;
groupBox2.Text = "Наборы";
//
// buttonDelObject
//
buttonDelObject.Location = new Point(17, 178);
buttonDelObject.Location = new Point(17, 150);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(170, 25);
buttonDelObject.TabIndex = 7;
@ -112,22 +116,22 @@
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(10, 93);
listBoxStorages.Location = new Point(10, 80);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(188, 79);
listBoxStorages.Size = new Size(188, 64);
listBoxStorages.TabIndex = 4;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(39, 289);
maskedTextBoxNumber.Location = new Point(39, 319);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(161, 23);
maskedTextBoxNumber.TabIndex = 3;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(20, 370);
ButtonRefreshCollection.Location = new Point(23, 400);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(194, 46);
ButtonRefreshCollection.TabIndex = 2;
@ -137,7 +141,7 @@
//
// ButtonDeleteAirplane
//
ButtonDeleteAirplane.Location = new Point(20, 318);
ButtonDeleteAirplane.Location = new Point(23, 348);
ButtonDeleteAirplane.Name = "ButtonDeleteAirplane";
ButtonDeleteAirplane.Size = new Size(194, 46);
ButtonDeleteAirplane.TabIndex = 1;
@ -147,9 +151,9 @@
//
// ButtonAddAirplane
//
ButtonAddAirplane.Location = new Point(23, 237);
ButtonAddAirplane.Location = new Point(30, 267);
ButtonAddAirplane.Name = "ButtonAddAirplane";
ButtonAddAirplane.Size = new Size(194, 46);
ButtonAddAirplane.Size = new Size(181, 46);
ButtonAddAirplane.TabIndex = 0;
ButtonAddAirplane.Text = "Добавить самолёт";
ButtonAddAirplane.UseVisualStyleBackColor = true;
@ -192,14 +196,14 @@
// сохранитьToolStripMenuItem1
//
сохранитьToolStripMenuItem1.Name = "сохранитьToolStripMenuItem1";
сохранитьToolStripMenuItem1.Size = new Size(180, 22);
сохранитьToolStripMenuItem1.Size = new Size(133, 22);
сохранитьToolStripMenuItem1.Text = "Сохранить";
сохранитьToolStripMenuItem1.Click += SaveToolStripMenuItem_Click;
//
// загрузитьToolStripMenuItem1
//
загрузитьToolStripMenuItem1.Name = агрузитьToolStripMenuItem1";
загрузитьToolStripMenuItem1.Size = new Size(180, 22);
загрузитьToolStripMenuItem1.Size = new Size(133, 22);
загрузитьToolStripMenuItem1.Text = "Загрузить";
загрузитьToolStripMenuItem1.Click += LoadToolStripMenuItem_Click;
//
@ -224,6 +228,26 @@
файлToolStripMenuItem.Size = new Size(48, 20);
файлToolStripMenuItem.Text = "Файл";
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(125, 223);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(86, 38);
buttonSortByColor.TabIndex = 6;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(31, 223);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(88, 38);
buttonSortByType.TabIndex = 7;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// FormAirplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -267,5 +291,7 @@
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранитьToolStripMenuItem1;
private ToolStripMenuItem загрузитьToolStripMenuItem1;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -1,7 +1,7 @@
using AirplaneWithRadar.Generics;
using AirplaneWithRadar.Exceptions;
using Microsoft.Extensions.Logging;
using AirplaneWithRadar.PaintObjects;
namespace AirplaneWithRadar
{
@ -23,7 +23,7 @@ namespace AirplaneWithRadar
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))
{
@ -109,6 +109,11 @@ namespace AirplaneWithRadar
MessageBox.Show($"Ошибка при добавлении: {ex.Message}");
logger.LogWarning($"Ошибка: {ex.Message}");
}
catch (ArgumentException ex)
{
MessageBox.Show($"Ошибка при добавлении: {ex.Message}");
logger.LogWarning($"Ошибка: {ex.Message}");
}
});
formAirplaneConfig.Show();
}
@ -203,9 +208,29 @@ namespace AirplaneWithRadar
MessageBoxButtons.OK, MessageBoxIcon.Error);
logger.LogWarning($"Ошибка при загрузке: {ex.Message}");
}
}
}
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<PaintAirplane?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.SortSet(comparer);
pictureBoxCollection.Image = obj.ShowAirplanes();
}
}
}

View File

@ -18,7 +18,7 @@
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>

View File

@ -0,0 +1,58 @@
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.Entities;
using System.Diagnostics.CodeAnalysis;
namespace AirplaneWithRadar.Generics;
internal class PaintAirplaneEqutables : IEqualityComparer<PaintAirplane>
{
public bool Equals(PaintAirplane? x, PaintAirplane? y)
{
if (x == null || x.AirplaneEntity == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.AirplaneEntity == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.AirplaneEntity.Speed != y.AirplaneEntity.Speed)
{
return false;
}
if (x.AirplaneEntity.Weight != y.AirplaneEntity.Weight)
{
return false;
}
if (x.AirplaneEntity.BodyColor != y.AirplaneEntity.BodyColor)
{
return false;
}
if (x is PaintAirplaneWithRadar && y is PaintAirplaneWithRadar)
{
AirplaneWithRadarEntity xEntity = (AirplaneWithRadarEntity)x.AirplaneEntity;
AirplaneWithRadarEntity yEntity = (AirplaneWithRadarEntity)x.AirplaneEntity;
if (xEntity.AdditColor != yEntity.AdditColor)
{
return false;
}
if (xEntity.AdditFuelPod != yEntity.AdditFuelPod)
{
return false;
}
if (xEntity.RadarOnBoard != yEntity.AdditFuelPod)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] PaintAirplane obj)
{
return obj.GetHashCode();
}
}

View File

@ -1,4 +1,5 @@
using AirplaneWithRadar.Exceptions;
using System.Collections.Generic;
namespace AirplaneWithRadar.Generics;
@ -14,7 +15,9 @@ internal class SetGeneric<T>
places = new List<T?>(count);
}
public int Insert(T airplane)
public void SortSet(IComparer<T?> comparer) => places.Sort(comparer);
public int Insert(T airplane, IEqualityComparer<T?>? equal = null)
{
if (Count == 0)
{
@ -24,8 +27,11 @@ internal class SetGeneric<T>
int emptyPosition = -1;
for (int i = 0; i < Count; i++)
{
if (places[i] == null)
if (places[i] is null)
{
if (equal is not null && places.Contains(airplane, equal))
throw new ArgumentException("Объект уже существует");
emptyPosition = i;
break;
}
@ -37,13 +43,16 @@ internal class SetGeneric<T>
if (emptyPosition < 0)
{
if (equal is not null && places.Contains(airplane, equal))
throw new ArgumentException("Объект уже существует");
places.Add(airplane);
return 0;
}
Insert(airplane, emptyPosition);
return 0;
}
public int Insert(T airplane, int position)
public int Insert(T airplane, int position, IEqualityComparer<T?>? equal = null)
{
if (position > maxCount)
{
@ -53,8 +62,11 @@ internal class SetGeneric<T>
{
return -1;
}
if (places[position] == null)
if (places[position] is null)
{
if (equal is not null && places.Contains(airplane, equal))
throw new ArgumentException("Объект уже существует");
places[position] = airplane;
return position;
}
@ -62,12 +74,15 @@ internal class SetGeneric<T>
int emptyPosition = -1;
for (int i = position + 1; i < Count; i++)
{
if (places[i]==null)
if (places[i] is null)
{
if (equal is not null && places.Contains(airplane, equal))
throw new ArgumentException("Объект уже существует");
emptyPosition = i;
break;
}
}
}
if (emptyPosition < 0)
{
return -1;