готовая 8

This commit is contained in:
Nastya_Kozlova 2023-12-13 01:15:52 +04:00
parent 8ee92ea1d8
commit df36de2377
10 changed files with 265 additions and 29 deletions

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.DrawningObjects;
using AirFighter.Drawnings;
namespace AirFighter.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,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.DrawningObjects;
using AirFighter.Drawnings;
namespace AirFighter.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 AirFighter.Generics
{
internal class AirplanesCollectionInfo : IEquatable<AirplanesCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public AirplanesCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(AirplanesCollectionInfo? other)
{
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -29,6 +29,8 @@ namespace AirFighter.Generics
// Набор объектов
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
// Получение объектов коллекции
public IEnumerable<T?> GetAirplanes => _collection.GetAirplanes();
@ -48,7 +50,7 @@ namespace AirFighter.Generics
{
return -1;
}
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawiningAirplaneEqutables());
}
// Перегрузка оператора вычитания

View File

@ -15,11 +15,11 @@ namespace AirFighter.Generics
internal class AirplanesGenericStorage
{
// Словарь (хранилище)
readonly Dictionary<string, AirplanesGenericCollection<DrawningAirplane,
readonly Dictionary<AirplanesCollectionInfo, AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane>> _AirplaneStorages;
// Возвращение списка названий наборов
public List<string> Keys => _AirplaneStorages.Keys.ToList();
public List<AirplanesCollectionInfo> Keys => _AirplaneStorages.Keys.ToList();
// Ширина окна отрисовки
private readonly int _pictureWidth;
@ -39,7 +39,7 @@ namespace AirFighter.Generics
// Конструктор
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
_AirplaneStorages = new Dictionary<string,
_AirplaneStorages = new Dictionary<AirplanesCollectionInfo,
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
@ -48,16 +48,17 @@ namespace AirFighter.Generics
// Добавление набора
public void AddSet(string name)
{
if (!_AirplaneStorages.ContainsKey(name))
_AirplaneStorages.Add(name, new AirplanesGenericCollection<DrawningAirplane,
if (!_AirplaneStorages.ContainsKey(new AirplanesCollectionInfo(name, string.Empty)))
_AirplaneStorages.Add(new AirplanesCollectionInfo(name, string.Empty),
new AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
// Удаление набора
public void DelSet(string name)
{
if (_AirplaneStorages.ContainsKey(name))
_AirplaneStorages.Remove(name);
if (_AirplaneStorages.ContainsKey(new AirplanesCollectionInfo(name, string.Empty)))
_AirplaneStorages.Remove(new AirplanesCollectionInfo(name, string.Empty));
}
// Доступ к набору
@ -66,8 +67,10 @@ namespace AirFighter.Generics
{
get
{
if (_AirplaneStorages.ContainsKey(ind))
return _AirplaneStorages[ind];
AirplanesCollectionInfo indObj = new AirplanesCollectionInfo(ind, string.Empty);
if (_AirplaneStorages.ContainsKey(indObj))
return _AirplaneStorages[indObj];
return null;
}
}
@ -80,7 +83,7 @@ namespace AirFighter.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<AirplanesCollectionInfo,
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>
record in _AirplaneStorages)
{
@ -89,7 +92,7 @@ namespace AirFighter.Generics
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
@ -138,7 +141,7 @@ namespace AirFighter.Generics
}
}
}
_AirplaneStorages.Add(record[0], collection);
_AirplaneStorages.Add(new AirplanesCollectionInfo(record[0], string.Empty), collection);
}
}
}

View File

@ -0,0 +1,62 @@
using AirFighter.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.Drawnings;
using AirFighter.Entities;
using System.Diagnostics.CodeAnalysis;
using AirFighter.DrawningObject;
namespace AirFighter.Generics
{
internal class DrawiningAirplaneEqutables : 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.Rockets != EntityY.Rockets)
return false;
if (EntityX.Wings != EntityY.Wings)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAirplane obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -47,6 +47,8 @@
loadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
ButtonSortByType = new Button();
ButtonSortByColor = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panelTools.SuspendLayout();
panelSets.SuspendLayout();
@ -64,6 +66,8 @@
// panelTools
//
panelTools.BorderStyle = BorderStyle.FixedSingle;
panelTools.Controls.Add(ButtonSortByColor);
panelTools.Controls.Add(ButtonSortByType);
panelTools.Controls.Add(panelSets);
panelTools.Controls.Add(ButtonAddAirplane);
panelTools.Controls.Add(ButtonRefreshCollection);
@ -134,7 +138,7 @@
//
// ButtonAddAirplane
//
ButtonAddAirplane.Location = new Point(12, 381);
ButtonAddAirplane.Location = new Point(13, 433);
ButtonAddAirplane.Name = "ButtonAddAirplane";
ButtonAddAirplane.Size = new Size(174, 31);
ButtonAddAirplane.TabIndex = 7;
@ -144,7 +148,7 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(12, 532);
ButtonRefreshCollection.Location = new Point(12, 567);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(174, 33);
ButtonRefreshCollection.TabIndex = 6;
@ -154,7 +158,7 @@
//
// buttonRemoveAirplane
//
buttonRemoveAirplane.Location = new Point(12, 480);
buttonRemoveAirplane.Location = new Point(13, 522);
buttonRemoveAirplane.Name = "buttonRemoveAirplane";
buttonRemoveAirplane.Size = new Size(174, 30);
buttonRemoveAirplane.TabIndex = 5;
@ -164,7 +168,7 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(12, 432);
maskedTextBoxNumber.Location = new Point(13, 480);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(174, 27);
maskedTextBoxNumber.TabIndex = 4;
@ -199,14 +203,14 @@
// saveToolStripMenuItem
//
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
saveToolStripMenuItem.Size = new Size(224, 26);
saveToolStripMenuItem.Size = new Size(177, 26);
saveToolStripMenuItem.Text = "Сохранение";
saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// loadToolStripMenuItem
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.Size = new Size(224, 26);
loadToolStripMenuItem.Size = new Size(177, 26);
loadToolStripMenuItem.Text = "Загрузка";
loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -219,6 +223,26 @@
//
saveFileDialog.Filter = "txt file | *.txt";
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(12, 343);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(171, 29);
ButtonSortByType.TabIndex = 9;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(12, 388);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(174, 29);
ButtonSortByColor.TabIndex = 10;
ButtonSortByColor.Text = "сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// FormAirPlaneCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
@ -263,5 +287,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -39,7 +39,7 @@ namespace AirFighter
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
listBoxStorage.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index
>= listBoxStorage.Items.Count))
@ -86,7 +86,7 @@ namespace AirFighter
}
string name = listBoxStorage.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show($"удалить набор {name}?", "удаление",
if (MessageBox.Show($"удалить набор {name}?", "удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(name);
@ -124,11 +124,17 @@ namespace AirFighter
}
}
catch (ApplicationException ex)
catch (StorageOverflowException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
_logger.LogWarning($"Неудачная попытка добавить объект: {ex.Message}");
}
catch (ArgumentException ex)
{
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
_logger.LogWarning($"Добавляемый объект уже существует в коллекции {ex.Message}");
}
}
// Добавление объекта в набор
private void ButtonAddAirplane_Click(object sender, EventArgs e)
@ -164,7 +170,7 @@ namespace AirFighter
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
try
{
if (obj - pos)
@ -206,7 +212,7 @@ namespace AirFighter
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
{
_storage.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно",
"результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
@ -241,5 +247,25 @@ namespace AirFighter
}
}
}
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 (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowAirplanes();
}
}
}

View File

@ -126,4 +126,7 @@
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>315, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>33</value>
</metadata>
</root>

View File

@ -26,20 +26,29 @@ namespace AirFighter.Generics
_places = new List<T?>(count);
}
// сортировка набора
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
// Добавление объекта в набор
public int Insert(T airplane)
public int Insert(T airplane, IEqualityComparer<T?>? equal = null)
{
return Insert(airplane, 0);
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
return Insert(airplane, 0, equal);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T airplane, int position)
public int Insert(T airplane, int position, IEqualityComparer<T?>? equal= null)
{
if (position < 0 || position > Count)
throw new AirplaneNotFoundException("Вставка невозможна");
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (equal != null)
{
if (_places.Contains(airplane, equal))
throw new ArgumentException(nameof(airplane));
}
_places.Insert(position, airplane);
return position;
}