diff --git a/Cruiser/Cruiser/Cruiser.csproj b/Cruiser/Cruiser/Cruiser.csproj index de13af6..d54ce90 100644 --- a/Cruiser/Cruiser/Cruiser.csproj +++ b/Cruiser/Cruiser/Cruiser.csproj @@ -32,4 +32,5 @@ Resources.Designer.cs + \ No newline at end of file diff --git a/Cruiser/Cruiser/CruiserCollectionInfo.cs b/Cruiser/Cruiser/CruiserCollectionInfo.cs new file mode 100644 index 0000000..68655b3 --- /dev/null +++ b/Cruiser/Cruiser/CruiserCollectionInfo.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser.Generics +{ + internal class CruiserCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public CruiserCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(CruiserCollectionInfo? other) + { + return Name == other.Name; + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} diff --git a/Cruiser/Cruiser/CruiserCompareByColor .cs b/Cruiser/Cruiser/CruiserCompareByColor .cs new file mode 100644 index 0000000..7a4cf92 --- /dev/null +++ b/Cruiser/Cruiser/CruiserCompareByColor .cs @@ -0,0 +1,50 @@ +using ProjectCruiser.DrawningObjects; +using ProjectCruiser.Entities; +using ProjectCruiser.Generics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser +{ + internal class CruiserCompareByColor : IComparer + { + public int Compare(DrawningCruiser? x, DrawningCruiser? y) + { + if (x == null || x.EntityCruiser == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityCruiser == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityCruiser.BodyColor.Name != y.EntityCruiser.BodyColor.Name) + { + return x.EntityCruiser.BodyColor.Name.CompareTo(y.EntityCruiser.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is EntityCruiser) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningCruiserDou) + { + EntityCruiserDou EntityX = (EntityCruiserDou)x.EntityCruiser; + EntityCruiserDou EntityY = (EntityCruiserDou)y.EntityCruiser; + if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name) + { + return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name); + } + } + var speedCompare = x.EntityCruiser.Speed.CompareTo(y.EntityCruiser.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityCruiser.Weight.CompareTo(y.EntityCruiser.Weight); + } + } +} diff --git a/Cruiser/Cruiser/CruiserCompareByType.cs b/Cruiser/Cruiser/CruiserCompareByType.cs new file mode 100644 index 0000000..861df19 --- /dev/null +++ b/Cruiser/Cruiser/CruiserCompareByType.cs @@ -0,0 +1,32 @@ +using ProjectCruiser.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser +{ + internal class CruiserCompareByType : IComparer + { + public int Compare(DrawningCruiser? x, DrawningCruiser? y) + { + if (x == null || x.EntityCruiser == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityCruiser == null) + throw new ArgumentNullException(nameof(y)); + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityCruiser.Speed.CompareTo(y.EntityCruiser.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityCruiser.Weight.CompareTo(y.EntityCruiser.Weight); + } + } +} diff --git a/Cruiser/Cruiser/CruiserGenericCollection.cs b/Cruiser/Cruiser/CruiserGenericCollection.cs index d417c79..1d3f370 100644 --- a/Cruiser/Cruiser/CruiserGenericCollection.cs +++ b/Cruiser/Cruiser/CruiserGenericCollection.cs @@ -1,13 +1,8 @@ -using ProjectCruiser.DrawningObjects; +using ProjectCruiser.Drawnings; using ProjectCruiser.MovementStrategy; -using ProjectCruiser.Generics; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ProjectCruiser.DrawningObjects; -namespace ProjectCruiser +namespace ProjectCruiser.Generics { internal class CruiserGenericCollection where T : DrawningCruiser @@ -25,6 +20,8 @@ namespace ProjectCruiser private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + public CruiserGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -34,21 +31,22 @@ namespace ProjectCruiser _collection = new SetGeneric(width * height); } - public static int? operator +(CruiserGenericCollection collect, T? obj) + public static bool operator +(CruiserGenericCollection collect, T? + obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj); + return collect?._collection.Insert(obj, new DrawningCruiserEqutables()) ?? false; } public static T operator -(CruiserGenericCollection collect, int pos) { - T? obj = collect._collection[pos]; + T obj = collect._collection[pos]; if (obj != null) { - collect._collection.Remove(pos); + collect?._collection.Remove(pos); } return obj; } diff --git a/Cruiser/Cruiser/CruiserGenericStorage.cs b/Cruiser/Cruiser/CruiserGenericStorage.cs index 69be118..bab431f 100644 --- a/Cruiser/Cruiser/CruiserGenericStorage.cs +++ b/Cruiser/Cruiser/CruiserGenericStorage.cs @@ -8,14 +8,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectCruiser +namespace ProjectCruiser.Generics { internal class CruiserGenericStorage { - readonly Dictionary> _CruiserStorages; - public List Keys => _CruiserStorages.Keys.ToList(); + public List Keys => _CruiserStorages.Keys.ToList(); private readonly int _pictureWidth; @@ -29,7 +29,7 @@ DrawningObjectCruiser>> _CruiserStorages; public CruiserGenericStorage(int pictureWidth, int pictureHeight) { - _CruiserStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -37,18 +37,18 @@ DrawningObjectCruiser>> _CruiserStorages; public void AddSet(string name) { - if (!_CruiserStorages.ContainsKey(name)) + if (!_CruiserStorages.ContainsKey(new CruiserCollectionInfo(name, string.Empty))) { - var cruiserCollection = new CruiserGenericCollection(_pictureWidth, _pictureHeight); - _CruiserStorages.Add(name, cruiserCollection); + var CruiserCollection = new CruiserGenericCollection(_pictureWidth, _pictureHeight); + _CruiserStorages.Add(new CruiserCollectionInfo(name, string.Empty), CruiserCollection); } } public void DelSet(string name) { - if (_CruiserStorages.ContainsKey(name)) + if (_CruiserStorages.ContainsKey(new CruiserCollectionInfo(name, string.Empty))) { - _CruiserStorages.Remove(name); + _CruiserStorages.Remove(new CruiserCollectionInfo(name, string.Empty)); } } @@ -56,9 +56,10 @@ DrawningObjectCruiser>> _CruiserStorages; { get { - if (_CruiserStorages.ContainsKey(ind)) + CruiserCollectionInfo indObj = new CruiserCollectionInfo(ind, string.Empty); + if (_CruiserStorages.ContainsKey(indObj)) { - return _CruiserStorages[ind]; + return _CruiserStorages[indObj]; } return null; } @@ -71,7 +72,7 @@ DrawningObjectCruiser>> _CruiserStorages; File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _CruiserStorages) + foreach (KeyValuePair> record in _CruiserStorages) { StringBuilder records = new(); foreach (DrawningCruiser? elem in record.Value.GetCruiser) @@ -87,7 +88,7 @@ DrawningObjectCruiser>> _CruiserStorages; using (StreamWriter writer = new StreamWriter(filename)) { - writer.Write($"CruiserStorage{Environment.NewLine}{data}"); + writer.Write($"lincornStorage{Environment.NewLine}{data}"); } } @@ -142,7 +143,7 @@ DrawningObjectCruiser>> _CruiserStorages; } } } - _CruiserStorages.Add(name, collection); + _CruiserStorages.Add(new CruiserCollectionInfo(name, string.Empty), collection); } } } diff --git a/Cruiser/Cruiser/DrawningCruiserEqutables.cs b/Cruiser/Cruiser/DrawningCruiserEqutables.cs new file mode 100644 index 0000000..b144528 --- /dev/null +++ b/Cruiser/Cruiser/DrawningCruiserEqutables.cs @@ -0,0 +1,54 @@ +using ProjectCruiser.DrawningObjects; +using ProjectCruiser.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectCruiser.Generics +{ + internal class DrawningCruiserEqutables : IEqualityComparer + { + public bool Equals(DrawningCruiser? x, DrawningCruiser? y) + { + if (x == null || x.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityCruiser.Speed != y.EntityCruiser.Speed) + { + return false; + } + if (x.EntityCruiser.Weight != y.EntityCruiser.Weight) + { + return false; + } + if (x.EntityCruiser.BodyColor != y.EntityCruiser.BodyColor) + { + return false; + } + if (x is EntityCruiserDou && y is EntityCruiserDou) + { + EntityCruiserDou EntityX = (EntityCruiserDou)x.EntityCruiser; + EntityCruiserDou EntityY = (EntityCruiserDou)y.EntityCruiser; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + if (EntityX.Vert != EntityY.Vert) + return false; + if (EntityX.Rocket != EntityY.Rocket) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningCruiser obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Cruiser/Cruiser/FormCruiserCollection.Designer.cs b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs index 20f6761..39d9db3 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.Designer.cs +++ b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); groupBox2 = new GroupBox(); listBoxStorage = new ListBox(); textBoxStorageName = new TextBox(); @@ -53,19 +55,41 @@ // // groupBox1 // + groupBox1.Controls.Add(ButtonSortByColor); + groupBox1.Controls.Add(ButtonSortByType); groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(ButtonRefreshCollection); groupBox1.Controls.Add(ButtonRemoveCruiser); groupBox1.Controls.Add(buttonAddCruiser); groupBox1.Controls.Add(menuStrip); - groupBox1.Location = new Point(637, 10); + groupBox1.Location = new Point(643, 12); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(183, 467); + groupBox1.Size = new Size(196, 519); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(14, 359); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(166, 26); + ButtonSortByColor.TabIndex = 11; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(14, 327); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(166, 26); + ButtonSortByType.TabIndex = 10; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // groupBox2 // groupBox2.Controls.Add(listBoxStorage); @@ -85,7 +109,7 @@ listBoxStorage.ItemHeight = 15; listBoxStorage.Location = new Point(6, 93); listBoxStorage.Name = "listBoxStorage"; - listBoxStorage.Size = new Size(154, 94); + listBoxStorage.Size = new Size(167, 109); listBoxStorage.TabIndex = 5; listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; // @@ -94,14 +118,14 @@ textBoxStorageName.Font = new Font("Lucida Sans Unicode", 9F, FontStyle.Regular, GraphicsUnit.Point); textBoxStorageName.Location = new Point(6, 22); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(154, 26); + textBoxStorageName.Size = new Size(167, 26); textBoxStorageName.TabIndex = 4; // // ButtonAddObject // ButtonAddObject.Location = new Point(6, 54); ButtonAddObject.Name = "ButtonAddObject"; - ButtonAddObject.Size = new Size(154, 30); + ButtonAddObject.Size = new Size(167, 33); ButtonAddObject.TabIndex = 3; ButtonAddObject.Text = "Добавить набор"; ButtonAddObject.UseVisualStyleBackColor = true; @@ -111,7 +135,7 @@ // ButtonDelObject.Location = new Point(6, 217); ButtonDelObject.Name = "ButtonDelObject"; - ButtonDelObject.Size = new Size(154, 30); + ButtonDelObject.Size = new Size(167, 33); ButtonDelObject.TabIndex = 2; ButtonDelObject.Text = "Удалить набор"; ButtonDelObject.UseVisualStyleBackColor = true; @@ -120,17 +144,17 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Font = new Font("Lucida Sans Unicode", 9F, FontStyle.Regular, GraphicsUnit.Point); - maskedTextBoxNumber.Location = new Point(34, 359); + maskedTextBoxNumber.Location = new Point(34, 423); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(117, 26); + maskedTextBoxNumber.Size = new Size(130, 26); maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(6, 430); + ButtonRefreshCollection.Location = new Point(16, 487); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - ButtonRefreshCollection.Size = new Size(171, 30); + ButtonRefreshCollection.Size = new Size(166, 26); ButtonRefreshCollection.TabIndex = 2; ButtonRefreshCollection.Text = "Обовить коллекцию"; ButtonRefreshCollection.UseVisualStyleBackColor = true; @@ -138,9 +162,9 @@ // // ButtonRemoveCruiser // - ButtonRemoveCruiser.Location = new Point(6, 391); + ButtonRemoveCruiser.Location = new Point(14, 455); ButtonRemoveCruiser.Name = "ButtonRemoveCruiser"; - ButtonRemoveCruiser.Size = new Size(171, 30); + ButtonRemoveCruiser.Size = new Size(167, 26); ButtonRemoveCruiser.TabIndex = 1; ButtonRemoveCruiser.Text = "Удалить крейсер"; ButtonRemoveCruiser.UseVisualStyleBackColor = true; @@ -148,9 +172,9 @@ // // buttonAddCruiser // - buttonAddCruiser.Location = new Point(6, 320); + buttonAddCruiser.Location = new Point(14, 391); buttonAddCruiser.Name = "buttonAddCruiser"; - buttonAddCruiser.Size = new Size(171, 30); + buttonAddCruiser.Size = new Size(167, 26); buttonAddCruiser.TabIndex = 0; buttonAddCruiser.Text = "Добавить крейсер"; buttonAddCruiser.UseVisualStyleBackColor = true; @@ -163,7 +187,7 @@ menuStrip.Location = new Point(3, 19); menuStrip.Name = "menuStrip"; menuStrip.Padding = new Padding(7, 3, 0, 3); - menuStrip.Size = new Size(177, 25); + menuStrip.Size = new Size(190, 25); menuStrip.TabIndex = 5; menuStrip.Text = "menuStrip1"; // @@ -192,7 +216,7 @@ // pictureBoxCollection.Location = new Point(1, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(630, 266); + pictureBoxCollection.Size = new Size(630, 521); pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; @@ -212,7 +236,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(826, 480); + ClientSize = new Size(853, 545); Controls.Add(pictureBoxCollection); Controls.Add(groupBox1); MainMenuStrip = menuStrip; @@ -248,5 +272,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/Cruiser/Cruiser/FormCruiserCollection.cs b/Cruiser/Cruiser/FormCruiserCollection.cs index 996374c..ce85e92 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.cs +++ b/Cruiser/Cruiser/FormCruiserCollection.cs @@ -27,7 +27,7 @@ namespace ProjectCruiser 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)) @@ -196,5 +196,25 @@ namespace ProjectCruiser } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByColor()); + + private void CompareCruiser(IComparer comparer) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowCruiser(); + } } } diff --git a/Cruiser/Cruiser/FormCruiserCollection.resx b/Cruiser/Cruiser/FormCruiserCollection.resx index c718d14..c06e615 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.resx +++ b/Cruiser/Cruiser/FormCruiserCollection.resx @@ -120,6 +120,9 @@ 17, 17 + + 17, 17 + 132, 17 diff --git a/Cruiser/Cruiser/SetGeneric.cs b/Cruiser/Cruiser/SetGeneric.cs index 46edeaf..ac90819 100644 --- a/Cruiser/Cruiser/SetGeneric.cs +++ b/Cruiser/Cruiser/SetGeneric.cs @@ -23,41 +23,27 @@ namespace ProjectCruiser.Generics _places = new List(count); } - public int Insert(T cruiser) + public void SortSet(IComparer comparer) => _places.Sort(comparer); + + public bool Insert(T cruiser, IEqualityComparer? equal = null) { - if (_places.Count == 0) - { - _places.Add(cruiser); - return 0; - } - else - { - if (_places.Count < _maxCount) - { - _places.Add(cruiser); - for (int i = 0; i < _places.Count; i++) - { - T temp = _places[i]; - _places[i] = _places[_places.Count - 1]; - _places[_places.Count - 1] = temp; - } - return 0; - } - else - { - throw new StorageOverflowException(_places.Count); - } - } + Insert(cruiser, 0, equal); + return true; } - public bool Insert(T cruiser, int position) + public bool Insert(T cruiser, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new CruiserNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(position); - _places.Insert(0, cruiser); + if (equal != null) + { + if (_places.Contains(cruiser, equal)) + throw new ArgumentException(nameof(cruiser)); + } + _places.Insert(position, cruiser); return true; }