правки 8 лаб

This commit is contained in:
platoff aeeee 2023-12-24 14:57:53 +04:00
parent 4b3d7d9694
commit a8bc30a079
10 changed files with 252 additions and 20 deletions

View File

@ -0,0 +1,76 @@
using global::Tank.Entites;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.DrawingObjects;
using Tank.Entites;
namespace Tank.Drawings
{
internal class DrawiningTankEqutables : IEqualityComparer<DrawingArmoredCar?>
{
public bool Equals(DrawingArmoredCar? x, DrawingArmoredCar? y)
{
if (x == null || x.Tank == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.Tank == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.Tank.Speed != y.Tank.Speed)
{
return false;
}
if (x.Tank.Weight != y.Tank.Weight)
{
return false;
}
if (x.Tank.BodyColor != y.Tank.BodyColor)
{
return false;
}
if (x is DrawingTank && y is DrawingTank)
{
if (x.Tank as EntityTank == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y.Tank as EntityTank == null)
{
throw new ArgumentNullException(nameof(y));
}
if ((x.Tank as EntityTank).AdditionalColor != (y.Tank as EntityTank).AdditionalColor)
{
return false;
}
if ((x.Tank as EntityTank).BodyKit != (y.Tank as EntityTank).BodyKit)
{
return false;
}
if ((x.Tank as EntityTank).Trunk != (y.Tank as EntityTank).Trunk)
{
return false;
}
if ((x.Tank as EntityTank).Line != (y.Tank as EntityTank).Line)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawingArmoredCar obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.listBoxStorages = new System.Windows.Forms.ListBox(); this.listBoxStorages = new System.Windows.Forms.ListBox();
@ -55,6 +57,8 @@
// //
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.ButtonSortByColor);
this.groupBox1.Controls.Add(this.ButtonSortByType);
this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.maskedTextBoxNumber); this.groupBox1.Controls.Add(this.maskedTextBoxNumber);
this.groupBox1.Controls.Add(this.ButtonRefreshCollection); this.groupBox1.Controls.Add(this.ButtonRefreshCollection);
@ -62,11 +66,31 @@
this.groupBox1.Controls.Add(this.ButtonAddArmoredCar); this.groupBox1.Controls.Add(this.ButtonAddArmoredCar);
this.groupBox1.Location = new System.Drawing.Point(579, 0); this.groupBox1.Location = new System.Drawing.Point(579, 0);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(194, 343); this.groupBox1.Size = new System.Drawing.Size(194, 400);
this.groupBox1.TabIndex = 0; this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты"; this.groupBox1.Text = "Инструменты";
// //
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(18, 211);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(144, 23);
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(18, 182);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(144, 23);
this.ButtonSortByType.TabIndex = 9;
this.ButtonSortByType.Text = "Сортировка по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBox2 // groupBox2
// //
this.groupBox2.Controls.Add(this.textBoxStorageName); this.groupBox2.Controls.Add(this.textBoxStorageName);
@ -119,14 +143,14 @@
// //
// maskedTextBoxNumber // maskedTextBoxNumber
// //
this.maskedTextBoxNumber.Location = new System.Drawing.Point(18, 235); this.maskedTextBoxNumber.Location = new System.Drawing.Point(18, 292);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(129, 23); this.maskedTextBoxNumber.Size = new System.Drawing.Size(129, 23);
this.maskedTextBoxNumber.TabIndex = 3; this.maskedTextBoxNumber.TabIndex = 3;
// //
// ButtonRefreshCollection // ButtonRefreshCollection
// //
this.ButtonRefreshCollection.Location = new System.Drawing.Point(6, 310); this.ButtonRefreshCollection.Location = new System.Drawing.Point(6, 367);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(172, 27); this.ButtonRefreshCollection.Size = new System.Drawing.Size(172, 27);
this.ButtonRefreshCollection.TabIndex = 2; this.ButtonRefreshCollection.TabIndex = 2;
@ -136,7 +160,7 @@
// //
// ButtonRemoveArmoredCar // ButtonRemoveArmoredCar
// //
this.ButtonRemoveArmoredCar.Location = new System.Drawing.Point(6, 264); this.ButtonRemoveArmoredCar.Location = new System.Drawing.Point(6, 321);
this.ButtonRemoveArmoredCar.Name = "ButtonRemoveArmoredCar"; this.ButtonRemoveArmoredCar.Name = "ButtonRemoveArmoredCar";
this.ButtonRemoveArmoredCar.Size = new System.Drawing.Size(172, 40); this.ButtonRemoveArmoredCar.Size = new System.Drawing.Size(172, 40);
this.ButtonRemoveArmoredCar.TabIndex = 1; this.ButtonRemoveArmoredCar.TabIndex = 1;
@ -146,7 +170,7 @@
// //
// ButtonAddArmoredCar // ButtonAddArmoredCar
// //
this.ButtonAddArmoredCar.Location = new System.Drawing.Point(6, 190); this.ButtonAddArmoredCar.Location = new System.Drawing.Point(6, 247);
this.ButtonAddArmoredCar.Name = "ButtonAddArmoredCar"; this.ButtonAddArmoredCar.Name = "ButtonAddArmoredCar";
this.ButtonAddArmoredCar.Size = new System.Drawing.Size(172, 39); this.ButtonAddArmoredCar.Size = new System.Drawing.Size(172, 39);
this.ButtonAddArmoredCar.TabIndex = 0; this.ButtonAddArmoredCar.TabIndex = 0;
@ -165,9 +189,9 @@
// panelStrip // panelStrip
// //
this.panelStrip.Controls.Add(this.StripMenu); this.panelStrip.Controls.Add(this.StripMenu);
this.panelStrip.Location = new System.Drawing.Point(585, 349); this.panelStrip.Location = new System.Drawing.Point(585, 406);
this.panelStrip.Name = "panelStrip"; this.panelStrip.Name = "panelStrip";
this.panelStrip.Size = new System.Drawing.Size(172, 89); this.panelStrip.Size = new System.Drawing.Size(172, 44);
this.panelStrip.TabIndex = 2; this.panelStrip.TabIndex = 2;
// //
// StripMenu // StripMenu
@ -255,5 +279,7 @@
private ToolStripMenuItem openFileDialog; private ToolStripMenuItem openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog1; private OpenFileDialog openFileDialog1;
private Button ButtonSortByColor;
private Button ButtonSortByType;
} }
} }

View File

@ -132,8 +132,7 @@ namespace Tank
{ {
return; return;
} }
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
string.Empty];
if (obj == null) if (obj == null)
{ {
return; return;
@ -213,5 +212,18 @@ namespace Tank
} }
} }
} }
private void CompareTank(IComparer<DrawingArmoredCar?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
return;
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowTanks();
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTank(new TankCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTank(new TankCompareByColor());
} }
} }

View File

@ -67,6 +67,6 @@
<value>294, 7</value> <value>294, 7</value>
</metadata> </metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>67</value> <value>39</value>
</metadata> </metadata>
</root> </root>

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Tank.Exceptions;
namespace Tank.Generics namespace Tank.Generics
{ {
@ -17,30 +18,37 @@ namespace Tank.Generics
public int Count => _places.Count; public int Count => _places.Count;
private readonly int _maxCount; private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public SetGeneric(int count) public SetGeneric(int count)
{ {
_maxCount = count; _maxCount = count;
_places = new List<T?>(_maxCount); _places = new List<T?>(_maxCount);
} }
public bool Insert(T tank) public bool Insert(T tank, IEqualityComparer<T?>? equal = null)
{ {
return Insert(tank, 0); return Insert(tank, 0, equal);
} }
public bool Insert(T tank, int position) public bool Insert(T tank, int position, IEqualityComparer<T?>? equal = null)
{ {
if (position < 0 || position > _maxCount) if (position < 0 || position >= _maxCount)
return false; throw new TankNotFoundException(position);
if (Count >= _maxCount) if (Count >= _maxCount)
return false; throw new TankStorageOverflowException(_maxCount);
if (equal != null && _places.Contains<T>(tank, equal))
throw new ApplicationException("Уже есть");
_places.Insert(0, tank); _places.Insert(0, tank);
return true; return true;
} }
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position > _maxCount) if (position < 0 || position > _maxCount || position >= Count)
return false; throw new TankNotFoundException(position);
_places.RemoveAt(position); _places.RemoveAt(position);
return true; return true;
} }

View File

@ -84,7 +84,8 @@ namespace Tank.Generics
} }
return obj; return obj;
} }
// Сортировка
public void Sort(IComparer<T> comparer) => _collection.SortSet(comparer);
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
/// </summary> /// </summary>

View File

@ -64,6 +64,8 @@ namespace Tank.Generics
/// <param name="name">Название набора</param> /// <param name="name">Название набора</param>
public void AddSet(string name) public void AddSet(string name)
{ {
TankCollectionInfo Info = new TankCollectionInfo(name, string.Empty);
if (_tankStorages.ContainsKey(name)) if (_tankStorages.ContainsKey(name))
{ {
return; return;
@ -79,6 +81,8 @@ namespace Tank.Generics
/// <param name="name">Название набора</param> /// <param name="name">Название набора</param>
public void DelSet(string name) public void DelSet(string name)
{ {
TankCollectionInfo Info = new TankCollectionInfo(name, string.Empty);
if (_tankStorages.ContainsKey(name)) if (_tankStorages.ContainsKey(name))
{ {
_tankStorages.Remove(name); _tankStorages.Remove(name);
@ -98,6 +102,8 @@ namespace Tank.Generics
{ {
get get
{ {
TankCollectionInfo Info = new TankCollectionInfo(ind, string.Empty);
if (_tankStorages.ContainsKey(ind)) if (_tankStorages.ContainsKey(ind))
{ {
return _tankStorages[ind]; return _tankStorages[ind];
@ -125,7 +131,7 @@ namespace Tank.Generics
} }
if (data.Length == 0) if (data.Length == 0)
{ {
throw new Exception("Невалиданя операция, нет данных для сохранения"); throw new Exception("Невалидная операция, нет данных для сохранения");
} }
using (StreamWriter writer = new StreamWriter(filename)) using (StreamWriter writer = new StreamWriter(filename))
{ {

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank
{
internal class TankCollectionInfo : IEquatable<TankCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public TankCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public override int GetHashCode()
{
return Name?.GetHashCode() ?? 0;
}
public bool Equals(TankCollectionInfo? other)
{
if (other == null || Name == null || other.Name == null) return false;
if (Name == other?.Name) return true;
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.DrawingObjects;
using Tank.Entites;
namespace Tank
{
internal class TankCompareByColor : IComparer<DrawingArmoredCar?>
{
public int Compare(DrawingArmoredCar? x, DrawingArmoredCar? y)
{
if (x == null || x.Tank == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.Tank == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.Tank.BodyColor != y.Tank.BodyColor)
{
return x.Tank.BodyColor.Name.CompareTo(y.Tank.BodyColor.Name);
}
if (x.GetType() == y.GetType() && x is DrawingTank)
{
return (x.Tank as EntityTank).AdditionalColor.Name.CompareTo((y.Tank as EntityTank).AdditionalColor.Name);
}
var speedCompare = x.Tank.Speed.CompareTo(y.Tank.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.Tank.Weight.CompareTo(y.Tank.Weight);
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.DrawingObjects;
namespace Tank
{
internal class TankCompareByType : IComparer<DrawingArmoredCar?>
{
public int Compare(DrawingArmoredCar? x, DrawingArmoredCar? y)
{
if (x == null || x.Tank == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.Tank == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.Tank.Speed.CompareTo(y.Tank.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.Tank.Weight.CompareTo(y.Tank.Weight);
}
}
}