Lab8 WarmlyShip Barsukov #8
64
ProjectWarmlyShip/ProjectWarmlyShip/DrawingShipEqutables.cs
Normal file
64
ProjectWarmlyShip/ProjectWarmlyShip/DrawingShipEqutables.cs
Normal file
@ -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<DrawingShip?>
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<DrawingShip?> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ namespace ProjectWarmlyShip.Generics
|
||||
_maxCount = count;
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
public bool Insert(T ship)
|
||||
public bool Insert(T ship, IEqualityComparer<T?>? 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<T?>? 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<T?> comparer) => _places.Sort(comparer);
|
||||
}
|
||||
}
|
56
ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs
Normal file
56
ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs
Normal file
@ -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<DrawingShip?>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
34
ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs
Normal file
34
ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs
Normal file
@ -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<DrawingShip?>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
27
ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs
Normal file
27
ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs
Normal file
@ -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<ShipsCollectionInfo>
|
||||
{
|
||||
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;
|
||||
eegov
commented
Нет проверки, что other не равен null Нет проверки, что other не равен null
|
||||
}
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.Name.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<T, U> collect, int pos)
|
||||
{
|
||||
@ -86,5 +86,6 @@ namespace ProjectWarmlyShip.Generics
|
||||
}
|
||||
}
|
||||
public IEnumerable<T?> GetShips => _collection.GetShips();
|
||||
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
|
||||
}
|
||||
}
|
||||
|
@ -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<string, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> _shipStorages;
|
||||
public List<string> Keys => _shipStorages.Keys.ToList();
|
||||
readonly Dictionary<ShipsCollectionInfo, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> _shipStorages;
|
||||
public List<ShipsCollectionInfo> 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<string, ShipsGenericCollection<DrawingShip, DrawingObjectShip>>();
|
||||
_shipStorages = new Dictionary<ShipsCollectionInfo, ShipsGenericCollection<DrawingShip, DrawingObjectShip>>();
|
||||
_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<DrawingShip, DrawingObjectShip>(_pictuteWidth, _pictuteHeight);
|
||||
_shipStorages.Add(new ShipsCollectionInfo(name, string.Empty), new ShipsGenericCollection<DrawingShip, DrawingObjectShip>(_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<DrawingShip, DrawingObjectShip>? 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<string, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> record in _shipStorages)
|
||||
foreach (KeyValuePair<ShipsCollectionInfo, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user
Требовалось сортировать по критериям: цвет, скорость, вес