From 7c4ed6a16864e68ac3391f6940dff9eca2b580d4 Mon Sep 17 00:00:00 2001 From: frog24 Date: Sat, 16 Dec 2023 04:25:32 +0400 Subject: [PATCH] Lab8 WarmlyShip Barsukov --- .../ProjectWarmlyShip/DrawingShipEqutables.cs | 64 +++++++++++++++++++ .../FormShipCollection.Designer.cs | 44 ++++++++++--- .../ProjectWarmlyShip/FormShipCollection.cs | 18 +++++- .../ProjectWarmlyShip/SetGeneric.cs | 11 +++- .../ProjectWarmlyShip/ShipCompareByColor.cs | 56 ++++++++++++++++ .../ProjectWarmlyShip/ShipCompareByType.cs | 34 ++++++++++ .../ProjectWarmlyShip/ShipsCollectionInfo.cs | 27 ++++++++ .../ShipsGenericCollection.cs | 3 +- .../ProjectWarmlyShip/ShipsGenericStorage.cs | 25 ++++---- 9 files changed, 257 insertions(+), 25 deletions(-) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/DrawingShipEqutables.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShipEqutables.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShipEqutables.cs new file mode 100644 index 0000000..730d9e7 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShipEqutables.cs @@ -0,0 +1,64 @@ +using ProjectWarmlyShip.DrawingObjects; +using ProjectWarmlyShip.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class DrawingShipEqutables : IEqualityComparer + { + public bool Equals(DrawingShip? x, DrawingShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityShip.Speed != y.EntityShip.Speed) + { + return false; + } + if (x.EntityShip.Weight != y.EntityShip.Weight) + { + return false; + } + if (x.EntityShip.MainColor != y.EntityShip.MainColor) + { + return false; + } + if (x is DrawingWarmlyShip && y is DrawingWarmlyShip) + { + EntityWarmlyShip _shipX = (EntityWarmlyShip)x.EntityShip; + EntityWarmlyShip _shipY = (EntityWarmlyShip)y.EntityShip; + if (_shipX.Pipes != _shipY.Pipes) + { + return false; + } + if (_shipX.FuelCompartment != _shipY.FuelCompartment) + { + return false; + } + if (_shipX.OptionalColor != _shipY.OptionalColor) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawingShip obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs index 5641a7b..c9f8b27 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -45,6 +45,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); SaveFileDialog = new SaveFileDialog(); OpenFileDialog = new OpenFileDialog(); + ButtonSortByType = new Button(); + ButtonSortByColor = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); Tools.SuspendLayout(); Sets.SuspendLayout(); @@ -61,6 +63,8 @@ // // Tools // + Tools.Controls.Add(ButtonSortByColor); + Tools.Controls.Add(ButtonSortByType); Tools.Controls.Add(Sets); Tools.Controls.Add(maskedTextBoxNumber); Tools.Controls.Add(buttonRefresh); @@ -125,16 +129,16 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(10, 320); + maskedTextBoxNumber.Location = new Point(10, 340); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(165, 23); maskedTextBoxNumber.TabIndex = 4; // // buttonRefresh // - buttonRefresh.Location = new Point(10, 405); + buttonRefresh.Location = new Point(10, 415); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(165, 50); + buttonRefresh.Size = new Size(165, 40); buttonRefresh.TabIndex = 2; buttonRefresh.Text = "Refresh"; buttonRefresh.UseVisualStyleBackColor = true; @@ -142,9 +146,9 @@ // // buttonDeleteShip // - buttonDeleteShip.Location = new Point(10, 350); + buttonDeleteShip.Location = new Point(10, 370); buttonDeleteShip.Name = "buttonDeleteShip"; - buttonDeleteShip.Size = new Size(165, 50); + buttonDeleteShip.Size = new Size(165, 40); buttonDeleteShip.TabIndex = 1; buttonDeleteShip.Text = "Delete Ship"; buttonDeleteShip.UseVisualStyleBackColor = true; @@ -152,9 +156,9 @@ // // buttonAddShip // - buttonAddShip.Location = new Point(10, 260); + buttonAddShip.Location = new Point(10, 290); buttonAddShip.Name = "buttonAddShip"; - buttonAddShip.Size = new Size(165, 50); + buttonAddShip.Size = new Size(165, 40); buttonAddShip.TabIndex = 0; buttonAddShip.Text = "Add Ship"; buttonAddShip.UseVisualStyleBackColor = true; @@ -179,14 +183,14 @@ // saveToolStripMenuItem // saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - saveToolStripMenuItem.Size = new Size(180, 22); + saveToolStripMenuItem.Size = new Size(100, 22); saveToolStripMenuItem.Text = "Save"; saveToolStripMenuItem.Click += SaveToolStripMenu_Click; // // loadToolStripMenuItem // loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - loadToolStripMenuItem.Size = new Size(180, 22); + loadToolStripMenuItem.Size = new Size(100, 22); loadToolStripMenuItem.Text = "Load"; loadToolStripMenuItem.Click += LoadToolStripMenu_Click; // @@ -199,6 +203,26 @@ OpenFileDialog.FileName = "openFileDialog1"; OpenFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(10, 257); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(82, 30); + ButtonSortByType.TabIndex = 6; + ButtonSortByType.Text = "Type Sort"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(93, 257); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(82, 30); + ButtonSortByColor.TabIndex = 7; + ButtonSortByColor.Text = "Color Sort"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // // FormShipCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -240,5 +264,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private SaveFileDialog SaveFileDialog; private OpenFileDialog OpenFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index 2e00fb2..9592786 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -44,7 +44,7 @@ namespace ProjectWarmlyShip 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)) @@ -216,5 +216,21 @@ namespace ProjectWarmlyShip } } } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByColor()); + private void CompareShips(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.ShowShips(); + } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs index e94836e..3aff3e8 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs @@ -17,11 +17,11 @@ namespace ProjectWarmlyShip.Generics _maxCount = count; _places = new List(count); } - public bool Insert(T ship) + public bool Insert(T ship, IEqualityComparer? equal = null) { - return Insert(ship, 0); + return Insert(ship, 0, equal); } - public bool Insert(T ship, int position) + public bool Insert(T ship, int position, IEqualityComparer? equal = null) { if (Count >= _maxCount) { @@ -31,6 +31,10 @@ namespace ProjectWarmlyShip.Generics { throw new StorageOverflowException("Impossible to insert"); } + if (equal != null && _places.Contains(ship, equal)) + { + throw new ArgumentException("That objact already exist in collection"); + } _places.Insert(position, ship); return true; } @@ -77,5 +81,6 @@ namespace ProjectWarmlyShip.Generics } } } + public void SortSet(IComparer comparer) => _places.Sort(comparer); } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs new file mode 100644 index 0000000..8d8b7e7 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs @@ -0,0 +1,56 @@ +using ProjectWarmlyShip.DrawingObjects; +using ProjectWarmlyShip.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipCompareByColor : IComparer + { + public int Compare(DrawingShip? x, DrawingShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityShip.MainColor.Name != y.EntityShip.MainColor.Name) + { + return x.EntityShip.MainColor.Name.CompareTo(y.EntityShip.MainColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawingShip) + { + return -1; + } + else + { + return 1; + } + } + if (x.GetType().Name == y.GetType().Name && x is DrawingWarmlyShip) + { + EntityWarmlyShip _X = (EntityWarmlyShip)x.EntityShip; + EntityWarmlyShip _Y = (EntityWarmlyShip)y.EntityShip; + + if (_X.OptionalColor.Name != _Y.OptionalColor.Name) + { + return _X.OptionalColor.Name.CompareTo(_Y.OptionalColor.Name); + } + } + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs new file mode 100644 index 0000000..ad18baf --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs @@ -0,0 +1,34 @@ +using ProjectWarmlyShip.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipCompareByType : IComparer + { + public int Compare(DrawingShip? x, DrawingShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs new file mode 100644 index 0000000..dd94cb8 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public ShipsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(ShipsCollectionInfo? other) + { + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs index aafd3c9..630f023 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs @@ -31,7 +31,7 @@ namespace ProjectWarmlyShip.Generics { return false; } - return (bool)collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj, new DrawingShipEqutables()); } public static T? operator -(ShipsGenericCollection collect, int pos) { @@ -86,5 +86,6 @@ namespace ProjectWarmlyShip.Generics } } public IEnumerable GetShips => _collection.GetShips(); + public void Sort(IComparer comparer) => _collection.SortSet(comparer); } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs index 96ce75b..0779ce7 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs @@ -6,13 +6,14 @@ using System.Threading.Tasks; using ProjectWarmlyShip.MovementStrategy; using ProjectWarmlyShip.DrawingObjects; using ProjectWarmlyShip.Exceptions; +using System.Xml.Linq; namespace ProjectWarmlyShip.Generics { internal class ShipsGenericStorage { - readonly Dictionary> _shipStorages; - public List Keys => _shipStorages.Keys.ToList(); + readonly Dictionary> _shipStorages; + public List Keys => _shipStorages.Keys.ToList(); private readonly int _pictuteWidth; private readonly int _pictuteHeight; private static readonly char _separatorForKeyValue = '|'; @@ -20,32 +21,34 @@ namespace ProjectWarmlyShip.Generics private static readonly char _separatorForObjects = ':'; public ShipsGenericStorage(int pictureWidth, int pictureHeight) { - _shipStorages = new Dictionary>(); + _shipStorages = new Dictionary>(); _pictuteWidth = pictureWidth; _pictuteHeight = pictureHeight; } public void AddSet(string name) { - if (_shipStorages.ContainsKey(name)) + if (_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty))) { + MessageBox.Show("There is that set name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _shipStorages[name] = new ShipsGenericCollection(_pictuteWidth, _pictuteHeight); + _shipStorages.Add(new ShipsCollectionInfo(name, string.Empty), new ShipsGenericCollection(_pictuteWidth, _pictuteHeight)); } public void DelSet(string name) { - if (_shipStorages.ContainsKey(name)) + if (_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty))) { - _shipStorages.Remove(name); + _shipStorages.Remove(new ShipsCollectionInfo(name, string.Empty)); } } public ShipsGenericCollection? this[string ind] { get { - if (_shipStorages.ContainsKey(ind)) + ShipsCollectionInfo indObj = new ShipsCollectionInfo(ind, string.Empty); + if (_shipStorages.ContainsKey(indObj)) { - return _shipStorages[ind]; + return _shipStorages[indObj]; } return null; } @@ -57,7 +60,7 @@ namespace ProjectWarmlyShip.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _shipStorages) + foreach (KeyValuePair> record in _shipStorages) { StringBuilder records = new(); foreach (DrawingShip? elem in record.Value.GetShips) @@ -133,7 +136,7 @@ namespace ProjectWarmlyShip.Generics } } } - _shipStorages.Add(record[0], collection); + _shipStorages.Add(new ShipsCollectionInfo(record[0], string.Empty), collection); } } }