PIbd-21_Tabeev A.P._Lab8 #8

Closed
TabeevAlexander wants to merge 1 commits from Lab8_AntiAircraftGun into Lab7_AntiAircraftGun
9 changed files with 273 additions and 23 deletions

View File

@ -30,6 +30,8 @@
{
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
@ -61,18 +63,40 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.ButtonSortByType);
this.groupBox1.Controls.Add(this.ButtonSortByColor);
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.maskedTextBoxNumber);
this.groupBox1.Controls.Add(this.ButtonAddAircraftGun);
this.groupBox1.Controls.Add(this.ButtonRemoveAircraftGun);
this.groupBox1.Controls.Add(this.ButtonRefreshCollection);
this.groupBox1.Location = new System.Drawing.Point(815, 37);
this.groupBox1.Location = new System.Drawing.Point(815, 27);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(236, 437);
this.groupBox1.Size = new System.Drawing.Size(236, 470);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(49, 236);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(156, 27);
this.ButtonSortByType.TabIndex = 8;
this.ButtonSortByType.Text = "Сортировка по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(49, 269);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(156, 27);
this.ButtonSortByColor.TabIndex = 7;
this.ButtonSortByColor.Text = "Сортировка по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.listBoxStorages);
@ -81,7 +105,7 @@
this.groupBox2.Controls.Add(this.ButtonDelObject);
this.groupBox2.Location = new System.Drawing.Point(21, 22);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(209, 222);
this.groupBox2.Size = new System.Drawing.Size(209, 205);
this.groupBox2.TabIndex = 6;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Наборы";
@ -125,14 +149,14 @@
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 318);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 335);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 23);
this.maskedTextBoxNumber.TabIndex = 5;
//
// ButtonAddAircraftGun
//
this.ButtonAddAircraftGun.Location = new System.Drawing.Point(49, 285);
this.ButtonAddAircraftGun.Location = new System.Drawing.Point(49, 302);
this.ButtonAddAircraftGun.Name = "ButtonAddAircraftGun";
this.ButtonAddAircraftGun.Size = new System.Drawing.Size(156, 27);
this.ButtonAddAircraftGun.TabIndex = 2;
@ -142,7 +166,7 @@
//
// ButtonRemoveAircraftGun
//
this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(49, 347);
this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(49, 364);
this.ButtonRemoveAircraftGun.Name = "ButtonRemoveAircraftGun";
this.ButtonRemoveAircraftGun.Size = new System.Drawing.Size(156, 26);
this.ButtonRemoveAircraftGun.TabIndex = 2;
@ -152,7 +176,7 @@
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(49, 379);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(49, 396);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(156, 27);
this.ButtonRefreshCollection.TabIndex = 2;
@ -244,5 +268,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -136,6 +136,11 @@ namespace AntiAircraftGun
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
}
}
// Добавление объекта в набор
private void ButtonAddAircraftGun_Click(object sender, EventArgs e)
@ -226,6 +231,25 @@ namespace AntiAircraftGun
_logger.LogWarning("Ошибка загрузки");
}
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAntiAircraftGun(new AircraftGunCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAntiAircraftGun(new AircraftGunCompareByColor());
private void CompareAntiAircraftGun(IComparer<DrawningAircraftGun?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowAntiAircraftGuns();
}
}
}

View File

@ -0,0 +1,43 @@
using AntiAircraftGun.DrawningObjects;
using AntiAircraftGun.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntiAircraftGun.Generics
{
internal class AircraftGunCompareByColor : IComparer<DrawningAircraftGun?>
{
public int Compare(DrawningAircraftGun? x, DrawningAircraftGun? y)
{
if (x == null || x.EntityAircraftGun == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAircraftGun == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityAircraftGun.BodyColor != y.EntityAircraftGun.BodyColor)
{
return x.EntityAircraftGun.BodyColor.Name.CompareTo(y.EntityAircraftGun.BodyColor.Name);
}
if (x.EntityAircraftGun is EntityAntiAircraftGun AntiAircraftGunX && y.EntityAircraftGun is EntityAntiAircraftGun AntiAircraftGunY)
{
var colorCompare = AntiAircraftGunX.AdditionalColor.Name.CompareTo(AntiAircraftGunY.AdditionalColor.Name);
if (colorCompare != 0)
{
return colorCompare;
}
}
var speedCompare = x.EntityAircraftGun.Speed.CompareTo(y.EntityAircraftGun.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityAircraftGun.Weight.CompareTo(y.EntityAircraftGun.Weight);
}
}
}

View File

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

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntiAircraftGun.Generics
{
internal class AntiAircraftGunCollectionInfo : IEquatable<AntiAircraftGunCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public AntiAircraftGunCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(AntiAircraftGunCollectionInfo? other)
{
if (other == null || Name == null || other.Name == null)
{
return false;
}
if (Name == other.Name)
{
return true;
}
throw new NotImplementedException();
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
}

View File

@ -40,7 +40,7 @@ namespace AntiAircraftGun.Generics
{
return false;
}
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawningAircraftGunEqutables());
}
// Перегрузка оператора вычитания
@ -100,5 +100,7 @@ namespace AntiAircraftGun.Generics
// Получение объектов коллекции
public IEnumerable<T?> GetAntiAircraftGuns => _collection.GetAntiAircraftGuns();
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
}
}

View File

@ -11,10 +11,10 @@ namespace AntiAircraftGun.Generics
internal class AntiAircraftGunsGenericStorage
{
// Словарь
readonly Dictionary<string, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>> _aircraftgunStorages;
readonly Dictionary<AntiAircraftGunCollectionInfo, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>> _aircraftgunStorages;
// Возвращение списка названий наборов
public List<string> Keys => _aircraftgunStorages.Keys.ToList();
public List<AntiAircraftGunCollectionInfo> Keys => _aircraftgunStorages.Keys.ToList();
// Ширина окна отрисовки
private readonly int _pictureWidth;
// Высота окна отрисовки
@ -29,7 +29,7 @@ namespace AntiAircraftGun.Generics
// Конструктор
public AntiAircraftGunsGenericStorage(int pictureWidth, int pictureHeight)
{
_aircraftgunStorages = new Dictionary<string, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>>();
_aircraftgunStorages = new Dictionary<AntiAircraftGunCollectionInfo, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -37,21 +37,21 @@ namespace AntiAircraftGun.Generics
// Добавление набора
public void AddSet(string name)
{
if(_aircraftgunStorages.ContainsKey(name))
if(_aircraftgunStorages.ContainsKey(new AntiAircraftGunCollectionInfo(name, "")))
{
return;
}
_aircraftgunStorages[name] = new AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>(_pictureWidth, _pictureHeight);
_aircraftgunStorages[new AntiAircraftGunCollectionInfo(name, "")] = new AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>(_pictureWidth, _pictureHeight);
}
// Удаление набора
public void DelSet(string name)
{
if (!_aircraftgunStorages.ContainsKey(name))
if (!_aircraftgunStorages.ContainsKey(new AntiAircraftGunCollectionInfo(name, "")))
{
return;
}
_aircraftgunStorages.Remove(name);
_aircraftgunStorages.Remove(new AntiAircraftGunCollectionInfo(name, ""));
}
// Доступ к набору
@ -59,9 +59,10 @@ namespace AntiAircraftGun.Generics
{
get
{
if(_aircraftgunStorages.ContainsKey(ind))
var info = new AntiAircraftGunCollectionInfo(ind, "");
if(_aircraftgunStorages.ContainsKey(info))
{
return _aircraftgunStorages[ind];
return _aircraftgunStorages[info];
}
return null;
}
@ -75,7 +76,7 @@ namespace AntiAircraftGun.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>> record in _aircraftgunStorages)
foreach (KeyValuePair<AntiAircraftGunCollectionInfo, AntiAircraftGunsGenericCollection<DrawningAircraftGun, DrawningObjectAircraftGun>> record in _aircraftgunStorages)
{
StringBuilder records = new();
foreach (DrawningAircraftGun? elem in record.Value.GetAntiAircraftGuns)
@ -136,7 +137,7 @@ namespace AntiAircraftGun.Generics
}
}
}
_aircraftgunStorages.Add(record[0], collection);
_aircraftgunStorages.Add(new AntiAircraftGunCollectionInfo(record[0], ""), collection);
}
return true;
}

View File

@ -0,0 +1,67 @@
using AntiAircraftGun.DrawningObjects;
using AntiAircraftGun.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntiAircraftGun.Generics
{
internal class DrawningAircraftGunEqutables : IEqualityComparer<DrawningAircraftGun?>
{
public bool Equals(DrawningAircraftGun? x, DrawningAircraftGun? y)
{
if (x == null || x.EntityAircraftGun == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAircraftGun == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityAircraftGun.Speed != y.EntityAircraftGun.Speed)
{
return false;
}
if (x.EntityAircraftGun.Weight != y.EntityAircraftGun.Weight)
{
return false;
}
if (x.EntityAircraftGun.BodyColor != y.EntityAircraftGun.BodyColor)
{
return false;
}
if (x is DrawingAntiAircraftGun && y is DrawingAntiAircraftGun)
{
if ((x.EntityAircraftGun as EntityAntiAircraftGun).Radar != (y.EntityAircraftGun as EntityAntiAircraftGun).Radar)
{
return false;
}
if ((x.EntityAircraftGun as EntityAntiAircraftGun).ZenitkaGun != (y.EntityAircraftGun as EntityAntiAircraftGun).ZenitkaGun)
{
return false;
}
if ((x.EntityAircraftGun as EntityAntiAircraftGun).Luke != (y.EntityAircraftGun as EntityAntiAircraftGun).Luke)
{
return false;
}
if ((x.EntityAircraftGun as EntityAntiAircraftGun).AdditionalColor != (y.EntityAircraftGun as EntityAntiAircraftGun).AdditionalColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAircraftGun obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -30,13 +30,13 @@ namespace AntiAircraftGun.Generics
}
// Добавление объекта в набор
public bool Insert(T antiaircraftgun)
public bool Insert(T antiaircraftgun, IEqualityComparer<T?>? equal = null)
{
return Insert(antiaircraftgun, 0);
return Insert(antiaircraftgun, 0, equal);
}
// Добавление объекта на конкретную позицию
public bool Insert(T antiaircraftgun, int position)
public bool Insert(T antiaircraftgun, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount)
{
@ -46,6 +46,16 @@ namespace AntiAircraftGun.Generics
{
throw new StorageOverflowException(_maxCount);
}
if(equal != null)
{
foreach(var i in _places)
{
if (equal.Equals(i, antiaircraftgun))
{
throw new ApplicationException("Объект уже существует");
}
}
}
_places.Insert(position, antiaircraftgun);
return true;
}
@ -98,5 +108,7 @@ namespace AntiAircraftGun.Generics
}
}
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
}
}