ИСЭбд-21 Чегодаев Александр лаб 8 простая #9

Closed
yacute wants to merge 1 commits from lab8 into lab7
11 changed files with 270 additions and 71 deletions

View File

@ -32,4 +32,5 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

View File

@ -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<CruiserCollectionInfo>
{
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;
Review

Нет проверки, что other не равен null

Нет проверки, что other не равен null
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
}

View File

@ -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<DrawningCruiser?>
Review

Требовалось сортировать по критериям: цвет, скорость, вес

Требовалось сортировать по критериям: цвет, скорость, вес
{
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);
}
}
}

View File

@ -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<DrawningCruiser?>
{
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);
}
}
}

View File

@ -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<T, U>
where T : DrawningCruiser
@ -25,6 +20,8 @@ namespace ProjectCruiser
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public CruiserGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -34,21 +31,22 @@ namespace ProjectCruiser
_collection = new SetGeneric<T>(width * height);
}
public static int? operator +(CruiserGenericCollection<T, U> collect, T? obj)
public static bool operator +(CruiserGenericCollection<T, U> 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<T, U> 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;
}

View File

@ -8,14 +8,14 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser
namespace ProjectCruiser.Generics
{
internal class CruiserGenericStorage
{
readonly Dictionary<string, CruiserGenericCollection<DrawningCruiser,
readonly Dictionary<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser,
DrawningObjectCruiser>> _CruiserStorages;
public List<string> Keys => _CruiserStorages.Keys.ToList();
public List<CruiserCollectionInfo> Keys => _CruiserStorages.Keys.ToList();
private readonly int _pictureWidth;
@ -29,7 +29,7 @@ DrawningObjectCruiser>> _CruiserStorages;
public CruiserGenericStorage(int pictureWidth, int pictureHeight)
{
_CruiserStorages = new Dictionary<string,
_CruiserStorages = new Dictionary<CruiserCollectionInfo,
CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>>();
_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<DrawningCruiser, DrawningObjectCruiser>(_pictureWidth, _pictureHeight);
_CruiserStorages.Add(name, cruiserCollection);
var CruiserCollection = new CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>(_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<string, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>> record in _CruiserStorages)
foreach (KeyValuePair<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>> 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);
}
}
}

View File

@ -0,0 +1,54 @@
using ProjectCruiser.DrawningObjects;
using ProjectCruiser.Entities;
using System.Diagnostics.CodeAnalysis;
namespace ProjectCruiser.Generics
{
internal class DrawningCruiserEqutables : IEqualityComparer<DrawningCruiser?>
{
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();
}
}
}

View File

@ -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;
}
}

View File

@ -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<DrawningCruiser?> 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();
}
}
}

View File

@ -120,6 +120,9 @@
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>132, 17</value>
</metadata>

View File

@ -23,41 +23,27 @@ namespace ProjectCruiser.Generics
_places = new List<T?>(count);
}
public int Insert(T cruiser)
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public bool Insert(T cruiser, IEqualityComparer<T>? 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<T>? 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;
}