PIbd-23_Nasyrov_A_G_Lab8 #10

Closed
gaillard wants to merge 4 commits from lab8 into lab7
9 changed files with 243 additions and 34 deletions

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.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 (other != null)
{
return Name == other.Name;
}
else
{
return false;
}
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,33 @@
using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.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.Name != y.EntityAirplane.BodyColor.Name)
{
return x.EntityAirplane.BodyColor.Name.CompareTo(y.EntityAirplane.BodyColor.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,33 @@
using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.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

@ -13,6 +13,7 @@ namespace ProjectAirplaneWithRadar.Generics
private readonly int _placeSizeWidth = 215;
private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public AirplanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -28,7 +29,7 @@ namespace ProjectAirplaneWithRadar.Generics
{
return false;
}
return collect?._collection.Insert(obj)??false;
return collect?._collection.Insert(obj, new DrawningAirplanesEqutables()) ?? false;
}
public static bool operator -(AirplanesGenericCollection<T, U> collect, int pos)
{

View File

@ -11,8 +11,8 @@ namespace ProjectAirplaneWithRadar.Generics
{
internal class AirplanesGenericStorage
{
readonly Dictionary<string, AirplanesGenericCollection<DrawningAirplane,DrawningObjectAirplane>> _airplanesStorages;
public List<string> Keys => _airplanesStorages.Keys.ToList();
readonly Dictionary<AirplaneCollectionInfo, AirplanesGenericCollection<DrawningAirplane,DrawningObjectAirplane>> _airplanesStorages;
public List<AirplaneCollectionInfo> Keys => _airplanesStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
@ -20,7 +20,7 @@ namespace ProjectAirplaneWithRadar.Generics
private static readonly char _separatorForObject = ':';
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
_airplanesStorages = new Dictionary<string,AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_airplanesStorages = new Dictionary<AirplaneCollectionInfo,AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -31,7 +31,7 @@ namespace ProjectAirplaneWithRadar.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<AirplaneCollectionInfo,
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplanesStorages)
{
StringBuilder records = new();
@ -39,7 +39,7 @@ namespace ProjectAirplaneWithRadar.Generics
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
@ -98,7 +98,7 @@ namespace ProjectAirplaneWithRadar.Generics
}
}
}
_airplanesStorages.Add(record[0], collection);
_airplanesStorages.Add(new AirplaneCollectionInfo (record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);
}
@ -106,21 +106,22 @@ namespace ProjectAirplaneWithRadar.Generics
}
public void AddSet(string name)
{
_airplanesStorages.Add(name, new AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
_airplanesStorages.Add(new AirplaneCollectionInfo(name, string.Empty), new AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_airplanesStorages.ContainsKey(name))
if (!_airplanesStorages.ContainsKey(new AirplaneCollectionInfo(name, string.Empty)))
return;
_airplanesStorages.Remove(name);
_airplanesStorages.Remove(new AirplaneCollectionInfo(name, string.Empty));
}
public AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>?this[string ind]
{
get
{
if (_airplanesStorages.ContainsKey(ind))
return _airplanesStorages[ind];
return null;
AirplaneCollectionInfo indObj = new AirplaneCollectionInfo(ind, string.Empty);
if (_airplanesStorages.ContainsKey(indObj))
return _airplanesStorages[indObj];
return null;
}
}
}

View File

@ -0,0 +1,59 @@
using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.Generics
{
internal class DrawningAirplanesEqutables : 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 DrawningAirplaneWithRadar && y is DrawningAirplaneWithRadar)
{
EntityAirplaneWithRadar EntityX = (EntityAirplaneWithRadar)x.EntityAirplane;
EntityAirplaneWithRadar EntityY = (EntityAirplaneWithRadar)y.EntityAirplane;
if (EntityX.Radar != EntityY.Radar)
return false;
if (EntityX.DopBak != EntityY.DopBak)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAirplane obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -30,6 +30,8 @@
{
pictureBoxAirplanesCollection = new PictureBox();
groupBoxAirplaneWithRadar = new GroupBox();
buttonSortByColor = new Button();
buttonSortByType = new Button();
groupBoxCollection = new GroupBox();
buttonRemoveObject = new Button();
listBoxStorages = new ListBox();
@ -59,10 +61,11 @@
pictureBoxAirplanesCollection.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxAirplanesCollection.TabIndex = 0;
pictureBoxAirplanesCollection.TabStop = false;
pictureBoxAirplanesCollection.Click += pictureBoxAirplanesCollection_Click;
//
// groupBoxAirplaneWithRadar
//
groupBoxAirplaneWithRadar.Controls.Add(buttonSortByColor);
groupBoxAirplaneWithRadar.Controls.Add(buttonSortByType);
groupBoxAirplaneWithRadar.Controls.Add(groupBoxCollection);
groupBoxAirplaneWithRadar.Controls.Add(buttonUpdateCollection);
groupBoxAirplaneWithRadar.Controls.Add(buttonDeleteAirplane);
@ -71,11 +74,31 @@
groupBoxAirplaneWithRadar.Dock = DockStyle.Right;
groupBoxAirplaneWithRadar.Location = new Point(650, 28);
groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar";
groupBoxAirplaneWithRadar.Size = new Size(251, 464);
groupBoxAirplaneWithRadar.Size = new Size(251, 522);
groupBoxAirplaneWithRadar.TabIndex = 1;
groupBoxAirplaneWithRadar.TabStop = false;
groupBoxAirplaneWithRadar.Text = "Инструменты";
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(7, 487);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(238, 29);
buttonSortByColor.TabIndex = 6;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(6, 453);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(238, 29);
buttonSortByType.TabIndex = 5;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// groupBoxCollection
//
groupBoxCollection.Controls.Add(buttonRemoveObject);
@ -183,14 +206,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(224, 26);
SaveToolStripMenuItem.Size = new Size(166, 26);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click_1;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26);
LoadToolStripMenuItem.Size = new Size(166, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -202,7 +225,7 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(901, 492);
ClientSize = new Size(901, 550);
Controls.Add(groupBoxAirplaneWithRadar);
Controls.Add(pictureBoxAirplanesCollection);
Controls.Add(menuStrip1);
@ -238,5 +261,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -28,7 +28,7 @@ namespace ProjectAirplaneWithRadar
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))
@ -64,11 +64,16 @@ namespace ProjectAirplaneWithRadar
Log.Information($"Äîáàâëåí îáúåêò â êîëëåêöèþ {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
pictureBoxAirplanesCollection.Image = obj.ShowAirplanes();
}
catch(StorageOverflowException ex)
catch (StorageOverflowException ex)
{
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);
}
@ -89,8 +94,6 @@ namespace ProjectAirplaneWithRadar
{
return;
}
try
{
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
@ -125,7 +128,6 @@ namespace ProjectAirplaneWithRadar
}
pictureBoxAirplanesCollection.Image = obj.ShowAirplanes();
}
private void buttonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
@ -150,17 +152,11 @@ namespace ProjectAirplaneWithRadar
ReloadObjects();
Log.Information($"Óäàëåí íàáîð: {name}");
}
}
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxAirplanesCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes();
}
private void pictureBoxAirplanesCollection_Click(object sender, EventArgs e)
{
}
private void SaveToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
@ -175,7 +171,7 @@ namespace ProjectAirplaneWithRadar
catch (Exception ex)
{
Log.Warning("Íå óäàëîñü ñîõðàíèòü");
MessageBox.Show($"Íå ñîõðàíèëîñü: {ex.Message}","Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Íå ñîõðàíèëîñü: {ex.Message}", "Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
@ -198,9 +194,30 @@ namespace ProjectAirplaneWithRadar
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) => CompareAirplanes(new AirplaneCompareByType());
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);
pictureBoxAirplanesCollection.Image = obj.ShowAirplanes();
}
private void buttonSortByColor_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByColor());
}
}

View File

@ -19,19 +19,25 @@ namespace ProjectAirplaneWithRadar.Generics
_maxCount = count;
_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;
}