ПИбд-23 Тихоненков А.Е. Лабораторная №8 #16
@ -30,6 +30,8 @@
|
||||
{
|
||||
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
|
||||
this.panelTools = new System.Windows.Forms.Panel();
|
||||
this.ButtonSortByColor = new System.Windows.Forms.Button();
|
||||
this.ButtonSortByType = new System.Windows.Forms.Button();
|
||||
this.panel1 = new System.Windows.Forms.Panel();
|
||||
this.textBoxStorageName = new System.Windows.Forms.TextBox();
|
||||
this.listBoxStorages = new System.Windows.Forms.ListBox();
|
||||
@ -61,6 +63,8 @@
|
||||
//
|
||||
// panelTools
|
||||
//
|
||||
this.panelTools.Controls.Add(this.ButtonSortByColor);
|
||||
this.panelTools.Controls.Add(this.ButtonSortByType);
|
||||
this.panelTools.Controls.Add(this.panel1);
|
||||
this.panelTools.Controls.Add(this.maskedTextBoxNumber);
|
||||
this.panelTools.Controls.Add(this.buttonUpdateColletion);
|
||||
@ -72,13 +76,33 @@
|
||||
this.panelTools.TabIndex = 1;
|
||||
this.panelTools.Tag = "";
|
||||
//
|
||||
// ButtonSortByColor
|
||||
//
|
||||
this.ButtonSortByColor.Location = new System.Drawing.Point(25, 344);
|
||||
this.ButtonSortByColor.Name = "ButtonSortByColor";
|
||||
this.ButtonSortByColor.Size = new System.Drawing.Size(193, 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(27, 303);
|
||||
this.ButtonSortByType.Name = "ButtonSortByType";
|
||||
this.ButtonSortByType.Size = new System.Drawing.Size(193, 35);
|
||||
this.ButtonSortByType.TabIndex = 9;
|
||||
this.ButtonSortByType.Text = "Сортировка по типу";
|
||||
this.ButtonSortByType.UseVisualStyleBackColor = true;
|
||||
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
|
||||
//
|
||||
// panel1
|
||||
//
|
||||
this.panel1.Controls.Add(this.textBoxStorageName);
|
||||
this.panel1.Controls.Add(this.listBoxStorages);
|
||||
this.panel1.Controls.Add(this.buttonAddCollection);
|
||||
this.panel1.Controls.Add(this.buttonDelCollection);
|
||||
this.panel1.Location = new System.Drawing.Point(15, 52);
|
||||
this.panel1.Location = new System.Drawing.Point(15, 3);
|
||||
this.panel1.Name = "panel1";
|
||||
this.panel1.Size = new System.Drawing.Size(215, 294);
|
||||
this.panel1.TabIndex = 8;
|
||||
@ -242,5 +266,7 @@
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private Button ButtonSortByColor;
|
||||
private Button ButtonSortByType;
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ namespace AntiAircraftGun
|
||||
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))
|
||||
@ -129,6 +129,11 @@ namespace AntiAircraftGun
|
||||
Serilog.Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
Serilog.Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||||
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
|
||||
}
|
||||
});
|
||||
form.AddEvent(zenitDelegate);
|
||||
}
|
||||
@ -226,6 +231,7 @@ namespace AntiAircraftGun
|
||||
{
|
||||
listBoxStorages.Items.Add(collection);
|
||||
}
|
||||
ReloadObjects();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -235,6 +241,25 @@ namespace AntiAircraftGun
|
||||
}
|
||||
}
|
||||
}
|
||||
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAntiAirCraftGun(new AntiAirCraftGunCompareByType());
|
||||
|
||||
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAntiAirCraftGun(new AntiAirCraftGunCompareByColor());
|
||||
|
||||
private void CompareAntiAirCraftGun(IComparer<BaseDrawingAntiAirCraftGun?> comparer)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
obj.Sort(comparer);
|
||||
pictureBoxCollection.Image = obj.ShowZenits();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
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)
|
||||
{
|
||||
return Name == other.Name;
|
||||
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.Name.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
using AntiAircraftGun.DrawingObjects;
|
||||
using AntiAircraftGun.Enitites;
|
||||
using AntiAircraftGun.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AntiAircraftGun.Generics
|
||||
{
|
||||
internal class AntiAirCraftGunCompareByColor : IComparer<BaseDrawingAntiAirCraftGun?>
|
||||
{
|
||||
public int Compare(BaseDrawingAntiAirCraftGun? x, BaseDrawingAntiAirCraftGun? y)
|
||||
eegov
commented
Требовалось сортировать по критериям: цвет, скорость, вес Требовалось сортировать по критериям: цвет, скорость, вес
|
||||
{
|
||||
if (x == null && y == null)
|
||||
{
|
||||
// Оба объекта равны
|
||||
return 0;
|
||||
}
|
||||
if (x == null && y != null)
|
||||
{
|
||||
// Объект x меньше
|
||||
return 1;
|
||||
}
|
||||
if (x != null && y == null)
|
||||
{
|
||||
// Объект x больше
|
||||
return -1;
|
||||
}
|
||||
var xAntiAircraftGun = x as BaseDrawingAntiAirCraftGun;
|
||||
var yAntiAircraftGun = y as BaseDrawingAntiAirCraftGun;
|
||||
|
||||
if (xAntiAircraftGun == null && yAntiAircraftGun == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (xAntiAircraftGun == null && yAntiAircraftGun != null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (xAntiAircraftGun != null && yAntiAircraftGun == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (xAntiAircraftGun.AntiAirСraftGun.BodyColor == yAntiAircraftGun.AntiAirСraftGun.BodyColor)
|
||||
{
|
||||
// Объекты равны по цвету
|
||||
return 0;
|
||||
}
|
||||
if (xAntiAircraftGun.AntiAirСraftGun.BodyColor.R.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.R) == 0)
|
||||
{
|
||||
if (xAntiAircraftGun.AntiAirСraftGun.BodyColor.G.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.G) == 0)
|
||||
{
|
||||
// Сравнение по компоненте B, если R и G совпадают
|
||||
return xAntiAircraftGun.AntiAirСraftGun.BodyColor.B.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.B);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Сравнение по компоненте G, если R совпадает, но G не совпадает
|
||||
return xAntiAircraftGun.AntiAirСraftGun.BodyColor.G.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.G);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Сравнение по компоненте R, если R не совпадает
|
||||
return xAntiAircraftGun.AntiAirСraftGun.BodyColor.R.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.R);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
using AntiAircraftGun.DrawingObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AntiAircraftGun.Generics
|
||||
{
|
||||
internal class AntiAirCraftGunCompareByType : IComparer<BaseDrawingAntiAirCraftGun?>
|
||||
{
|
||||
public int Compare(BaseDrawingAntiAirCraftGun? x, BaseDrawingAntiAirCraftGun? y)
|
||||
{
|
||||
if (x == null || x.AntiAirСraftGun == null)
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
|
||||
if (y == null || y.AntiAirСraftGun == null)
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return x.GetType().Name.CompareTo(y.GetType().Name);
|
||||
}
|
||||
var speedCompare = x.AntiAirСraftGun.Speed.CompareTo(y.AntiAirСraftGun.Speed);
|
||||
|
||||
if (speedCompare != 0)
|
||||
return speedCompare;
|
||||
|
||||
return x.AntiAirСraftGun.Weight.CompareTo(y.AntiAirСraftGun.Weight);
|
||||
}
|
||||
}
|
||||
}
|
@ -36,6 +36,7 @@ namespace AntiAircraftGun.Generics
|
||||
/// Набор объектов
|
||||
/// </summary>
|
||||
private readonly SetGeneric<T> _collection;
|
||||
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -59,7 +60,7 @@ namespace AntiAircraftGun.Generics
|
||||
{
|
||||
if (obj == null)
|
||||
return false;
|
||||
return collect?._collection.Insert(obj) ?? false;
|
||||
return collect?._collection.Insert(obj, new DrawingAntiAirCraftGunEqutables()) ?? false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора вычитания
|
||||
|
@ -5,6 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace AntiAircraftGun.Generics
|
||||
{
|
||||
@ -28,11 +29,11 @@ namespace AntiAircraftGun.Generics
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>> _zenitStorages;
|
||||
readonly Dictionary<AntiAirCraftGunCollectionInfo, AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>> _zenitStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _zenitStorages.Keys.ToList();
|
||||
public List<AntiAirCraftGunCollectionInfo> Keys => _zenitStorages.Keys.ToList();
|
||||
/// <summary>
|
||||
/// Ширина окна отрисовки
|
||||
/// </summary>
|
||||
@ -48,7 +49,7 @@ namespace AntiAircraftGun.Generics
|
||||
/// <param name="pictureHeight"></param>
|
||||
public AntiAirCraftGunGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_zenitStorages = new Dictionary<string, AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>>();
|
||||
_zenitStorages = new Dictionary<AntiAirCraftGunCollectionInfo, AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -59,13 +60,13 @@ namespace AntiAircraftGun.Generics
|
||||
public void AddSet(string name)
|
||||
{
|
||||
// TODO: Прописать логику для добавления набора
|
||||
if (_zenitStorages.ContainsKey(name))
|
||||
if (_zenitStorages.ContainsKey(new AntiAirCraftGunCollectionInfo(name, string.Empty)))
|
||||
{
|
||||
MessageBox.Show("Набор с таким именем уже существует");
|
||||
return;
|
||||
}
|
||||
|
||||
_zenitStorages.Add(name, new AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>(_pictureWidth, _pictureHeight));
|
||||
_zenitStorages.Add(new AntiAirCraftGunCollectionInfo(name,string.Empty), new AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -75,12 +76,12 @@ namespace AntiAircraftGun.Generics
|
||||
public void DelSet(string name)
|
||||
{
|
||||
// TODO: Прописать логику для удаления набора
|
||||
if (!_zenitStorages.ContainsKey(name))
|
||||
if (!_zenitStorages.ContainsKey(new AntiAirCraftGunCollectionInfo(name, string.Empty)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_zenitStorages.Remove(name);
|
||||
_zenitStorages.Remove(new AntiAirCraftGunCollectionInfo(name, string.Empty));
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
@ -92,10 +93,11 @@ namespace AntiAircraftGun.Generics
|
||||
{
|
||||
get
|
||||
{
|
||||
AntiAirCraftGunCollectionInfo indobj = new AntiAirCraftGunCollectionInfo(ind, string.Empty);
|
||||
// TODO: Продумать логику получения набора
|
||||
if (_zenitStorages.ContainsKey(ind))
|
||||
if (_zenitStorages.ContainsKey(indobj))
|
||||
{
|
||||
return _zenitStorages[ind];
|
||||
return _zenitStorages[indobj];
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -115,15 +117,14 @@ namespace AntiAircraftGun.Generics
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string,
|
||||
AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>> record in _zenitStorages)
|
||||
foreach (KeyValuePair<AntiAirCraftGunCollectionInfo, AntiAirCraftGunGenericCollection<BaseDrawingAntiAirCraftGun, DrawingObjectAntiAirCraftGun>> record in _zenitStorages)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
foreach (BaseDrawingAntiAirCraftGun? elem in record.Value.GetCars)
|
||||
{
|
||||
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
||||
}
|
||||
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
||||
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
||||
}
|
||||
if (data.Length == 0)
|
||||
{
|
||||
@ -186,7 +187,7 @@ namespace AntiAircraftGun.Generics
|
||||
}
|
||||
}
|
||||
}
|
||||
_zenitStorages.Add(record[0], collection);
|
||||
_zenitStorages.Add(new AntiAirCraftGunCollectionInfo(record[0],string.Empty), collection);
|
||||
|
||||
str = sr.ReadLine();
|
||||
} while (str != null);
|
||||
|
@ -31,26 +31,32 @@ namespace AntiAircraftGun.Generics
|
||||
_maxCount = count;
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
/// </summary>
|
||||
/// <param name="car">Добавляемый автомобиль</param>
|
||||
/// <returns></returns>
|
||||
public bool Insert(T car)
|
||||
public bool Insert(T car, IEqualityComparer<T>? equal = null)
|
||||
{
|
||||
if (_places.Count == _maxCount)
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
Insert(car, 0);
|
||||
Insert(car, 0, equal);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Insert(T car, int position)
|
||||
public bool Insert(T car, int position, IEqualityComparer<T>? equal = null)
|
||||
{
|
||||
if (_places.Count == _maxCount)
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
if (!(position >= 0 && position <= Count))
|
||||
return false;
|
||||
_places.Insert(position, car);
|
||||
if (equal != null)
|
||||
{
|
||||
if (_places.Contains(car, equal))
|
||||
throw new ArgumentException(nameof(car));
|
||||
}
|
||||
_places.Insert(position, car);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user
Нет проверки, что other не равен null