PIbd-22. Stroev V.M. Lab Work 08 #10

Closed
StroevVladimir wants to merge 4 commits from Lab08 into Lab07
9 changed files with 270 additions and 63 deletions
Showing only changes of commit edd3626677 - Show all commits

View File

@ -30,6 +30,8 @@
{
pictureBoxCollection = new PictureBox();
panel1 = new Panel();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
panel2 = new Panel();
label2 = new Label();
listBoxStorages = new ListBox();
@ -55,26 +57,52 @@
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(5, 31);
pictureBoxCollection.Location = new Point(6, 39);
pictureBoxCollection.Margin = new Padding(4);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(1017, 485);
pictureBoxCollection.Size = new Size(1271, 606);
pictureBoxCollection.TabIndex = 5;
pictureBoxCollection.TabStop = false;
//
// panel1
//
panel1.BackColor = SystemColors.Info;
panel1.Controls.Add(ButtonSortByColor);
panel1.Controls.Add(ButtonSortByType);
panel1.Controls.Add(panel2);
panel1.Controls.Add(maskedTextBoxNumber);
panel1.Controls.Add(buttonRefreshCol);
panel1.Controls.Add(buttonDelMonorail);
panel1.Controls.Add(buttonAddMonorail);
panel1.Controls.Add(label1);
panel1.Location = new Point(1025, -1);
panel1.Location = new Point(1281, -1);
panel1.Margin = new Padding(4);
panel1.Name = "panel1";
panel1.Size = new Size(204, 517);
panel1.Size = new Size(255, 646);
panel1.TabIndex = 4;
//
// ButtonSortByColor
//
ButtonSortByColor.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point);
ButtonSortByColor.Location = new Point(15, 366);
ButtonSortByColor.Margin = new Padding(4);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(224, 51);
ButtonSortByColor.TabIndex = 9;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
//
// ButtonSortByType
//
ButtonSortByType.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point);
ButtonSortByType.Location = new Point(15, 307);
ButtonSortByType.Margin = new Padding(4);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(224, 51);
ButtonSortByType.TabIndex = 8;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
//
// panel2
//
panel2.Controls.Add(label2);
@ -82,43 +110,48 @@
panel2.Controls.Add(textBoxStorageName);
panel2.Controls.Add(button1);
panel2.Controls.Add(ButtonAddObject);
panel2.Location = new Point(12, 20);
panel2.Location = new Point(15, 25);
panel2.Margin = new Padding(4);
panel2.Name = "panel2";
panel2.Size = new Size(179, 263);
panel2.Size = new Size(224, 261);
panel2.TabIndex = 7;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(3, -3);
label2.Location = new Point(4, -4);
label2.Margin = new Padding(4, 0, 4, 0);
label2.Name = "label2";
label2.Size = new Size(66, 20);
label2.Size = new Size(79, 25);
label2.TabIndex = 8;
label2.Text = "Наборы";
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(17, 109);
listBoxStorages.ItemHeight = 25;
listBoxStorages.Location = new Point(21, 106);
listBoxStorages.Margin = new Padding(4);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(144, 104);
listBoxStorages.Size = new Size(179, 104);
listBoxStorages.TabIndex = 9;
listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(18, 29);
textBoxStorageName.Location = new Point(21, 25);
textBoxStorageName.Margin = new Padding(4);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(143, 27);
textBoxStorageName.Size = new Size(178, 31);
textBoxStorageName.TabIndex = 8;
//
// button1
//
button1.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point);
button1.Location = new Point(18, 219);
button1.Location = new Point(20, 218);
button1.Margin = new Padding(4);
button1.Name = "button1";
button1.Size = new Size(143, 41);
button1.Size = new Size(179, 33);
button1.TabIndex = 5;
button1.Text = "Удалить набор";
button1.UseVisualStyleBackColor = true;
@ -127,9 +160,10 @@
// ButtonAddObject
//
ButtonAddObject.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point);
ButtonAddObject.Location = new Point(18, 62);
ButtonAddObject.Location = new Point(22, 64);
ButtonAddObject.Margin = new Padding(4);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(143, 41);
ButtonAddObject.Size = new Size(179, 34);
ButtonAddObject.TabIndex = 6;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
@ -137,17 +171,19 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(30, 336);
maskedTextBoxNumber.Location = new Point(37, 484);
maskedTextBoxNumber.Margin = new Padding(4);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(125, 27);
maskedTextBoxNumber.Size = new Size(155, 31);
maskedTextBoxNumber.TabIndex = 4;
//
// buttonRefreshCol
//
buttonRefreshCol.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point);
buttonRefreshCol.Location = new Point(12, 466);
buttonRefreshCol.Location = new Point(15, 582);
buttonRefreshCol.Margin = new Padding(4);
buttonRefreshCol.Name = "buttonRefreshCol";
buttonRefreshCol.Size = new Size(179, 41);
buttonRefreshCol.Size = new Size(224, 51);
buttonRefreshCol.TabIndex = 3;
buttonRefreshCol.Text = "Обновить коллекцию";
buttonRefreshCol.UseVisualStyleBackColor = true;
@ -156,9 +192,10 @@
// buttonDelMonorail
//
buttonDelMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point);
buttonDelMonorail.Location = new Point(12, 369);
buttonDelMonorail.Location = new Point(15, 523);
buttonDelMonorail.Margin = new Padding(4);
buttonDelMonorail.Name = "buttonDelMonorail";
buttonDelMonorail.Size = new Size(179, 41);
buttonDelMonorail.Size = new Size(224, 51);
buttonDelMonorail.TabIndex = 2;
buttonDelMonorail.Text = "Удалить монорельс";
buttonDelMonorail.UseVisualStyleBackColor = true;
@ -167,9 +204,10 @@
// buttonAddMonorail
//
buttonAddMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point);
buttonAddMonorail.Location = new Point(12, 289);
buttonAddMonorail.Location = new Point(15, 425);
buttonAddMonorail.Margin = new Padding(4);
buttonAddMonorail.Name = "buttonAddMonorail";
buttonAddMonorail.Size = new Size(179, 41);
buttonAddMonorail.Size = new Size(224, 51);
buttonAddMonorail.TabIndex = 1;
buttonAddMonorail.Text = "Добавить монорельс";
buttonAddMonorail.UseVisualStyleBackColor = true;
@ -178,9 +216,10 @@
// label1
//
label1.AutoSize = true;
label1.Location = new Point(3, -3);
label1.Location = new Point(4, -4);
label1.Margin = new Padding(4, 0, 4, 0);
label1.Name = "label1";
label1.Size = new Size(103, 20);
label1.Size = new Size(122, 25);
label1.TabIndex = 0;
label1.Text = "Инструменты";
//
@ -190,7 +229,8 @@
menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(1228, 28);
menuStrip1.Padding = new Padding(8, 2, 0, 2);
menuStrip1.Size = new Size(1535, 33);
menuStrip1.TabIndex = 6;
menuStrip1.Text = "menuStrip1";
//
@ -198,20 +238,20 @@
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(59, 24);
файлToolStripMenuItem.Size = new Size(69, 29);
файлToolStripMenuItem.Text = "Файл";
//
// сохранитьToolStripMenuItem
//
сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem";
сохранитьToolStripMenuItem.Size = new Size(166, 26);
сохранитьToolStripMenuItem.Size = new Size(200, 34);
сохранитьToolStripMenuItem.Text = "Сохранить";
сохранитьToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// загрузитьToolStripMenuItem
//
загрузитьToolStripMenuItem.Name = агрузитьToolStripMenuItem";
загрузитьToolStripMenuItem.Size = new Size(166, 26);
загрузитьToolStripMenuItem.Size = new Size(200, 34);
загрузитьToolStripMenuItem.Text = "Загрузить";
загрузитьToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -227,14 +267,15 @@
//
// FormMonorailCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ControlDarkDark;
ClientSize = new Size(1228, 518);
ClientSize = new Size(1535, 648);
Controls.Add(pictureBoxCollection);
Controls.Add(panel1);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Margin = new Padding(4);
Name = "FormMonorailCollection";
Text = "Набор Монорельсов";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
@ -269,5 +310,7 @@
private ToolStripMenuItem загрузитьToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -25,6 +25,11 @@ namespace Monorail
private readonly ILogger _logger;
readonly int countPlace = 22;
private void ButtonSortByType_Click(object sender, EventArgs e) =>
CompareMonorails(new MonorailCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) =>
CompareMonorails(new MonorailCompareByColor());
public FormMonorailCollection(ILogger<FormMonorailCollection> logger)
{
InitializeComponent();
@ -71,6 +76,22 @@ EventArgs e)
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowMonorails();
}
private void CompareMonorails(IComparer<DrawningMonorail?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowMonorails();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
@ -165,12 +186,14 @@ EventArgs e)
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try {
try
{
var curObj = obj - pos;
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowMonorails();
_logger.LogInformation($"Объект удален по позиции: {pos}");
}catch(MonorailNotFoundException ex)
}
catch (MonorailNotFoundException ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);

View File

@ -40,6 +40,20 @@ namespace Monorail.Generics
if (x is DrawningSecondMonorail && y is DrawningSecondMonorail)
{
// TODO
EntitySecondMonorail entityX = (EntitySecondMonorail)x.EntityMonorail;
EntitySecondMonorail entityY = (EntitySecondMonorail)y.EntityMonorail;
if(entityX.AdditionalColor != entityY.AdditionalColor)
{
return false;
}
if(entityX.Monorails != entityY.Monorails)
{
return false;
}
if(entityX.SecondCabin != entityY.SecondCabin)
{
return false;
}
}
return true;
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Monorail.DrawningObjects;
using Monorail.Entities;
namespace Monorail.Generics
{
internal class MonorailCompareByColor : IComparer<DrawningMonorail?>
{
public int Compare(DrawningMonorail? x, DrawningMonorail? y)
{
if (x == null || x.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(y));
}
if(x.EntityMonorail.BodyColor.Name != y.EntityMonorail.BodyColor.Name)
{
return x.EntityMonorail.BodyColor.Name.CompareTo(y.EntityMonorail.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is DrawningMonorail)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawningSecondMonorail)
{
EntitySecondMonorail entityX = (EntitySecondMonorail)x.EntityMonorail;
EntitySecondMonorail entityY = (EntitySecondMonorail)y.EntityMonorail;
if (entityX.AdditionalColor.Name != entityY.AdditionalColor.Name)
{
return entityX.AdditionalColor.Name.CompareTo(entityY.AdditionalColor.Name);
}
}
var speedCompare =
x.EntityMonorail.Speed.CompareTo(y.EntityMonorail.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityMonorail.Weight.CompareTo(y.EntityMonorail.Weight);
}
}
}

View File

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

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.Generics
{
internal class MonorailsCollectionInfo : IEquatable<MonorailsCollectionInfo>
{
public string Name { get; protected set; }
public string Description { get; protected set; }
public MonorailsCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(MonorailsCollectionInfo? other)
{
// TODO прописать логику сравнения по свойству Name
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -18,6 +18,9 @@ namespace Monorail.Generics
private readonly int _placeSizeWidth = 295;
private readonly int _placeSizeHeight = 65;
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) =>
_collection.SortSet(comparer);
public MonorailsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -26,13 +29,13 @@ namespace Monorail.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(MonorailsGenericCollection<T, U> collect, T? obj)
public static int operator +(MonorailsGenericCollection<T, U> collect, T obj)
{
if (obj == null)
{
return -1;
}
return collect._collection.Insert(obj);
return collect?._collection.Insert(obj, new DrawningMonorailEqutables()) ?? -1;
}
public static T? operator -(MonorailsGenericCollection<T, U> collect, int pos)
{

View File

@ -14,14 +14,14 @@ namespace Monorail.Generics
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
readonly Dictionary<string, MonorailsGenericCollection<DrawningMonorail,
readonly Dictionary<MonorailsCollectionInfo, MonorailsGenericCollection<DrawningMonorail,
DrawningObjectMonorail>> _monorailsStorages;
public List<string> Keys => _monorailsStorages.Keys.ToList();
public List<MonorailsCollectionInfo> Keys => _monorailsStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public MonorailsGenericStorage(int pictureWidth, int pictureHeight)
{
_monorailsStorages = new Dictionary<string, MonorailsGenericCollection<DrawningMonorail,
_monorailsStorages = new Dictionary<MonorailsCollectionInfo, MonorailsGenericCollection<DrawningMonorail,
DrawningObjectMonorail>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; ;
@ -29,27 +29,33 @@ namespace Monorail.Generics
public void AddSet(string name)
{
//TO DO
_monorailsStorages.Add(name, new MonorailsGenericCollection<DrawningMonorail,
if(_monorailsStorages.ContainsKey(new MonorailsCollectionInfo(name, string.Empty)))
{
MessageBox.Show("Словарь уже имеет объект с таким названием", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
_monorailsStorages.Add(new MonorailsCollectionInfo(name, string.Empty),
new MonorailsGenericCollection<DrawningMonorail,
DrawningObjectMonorail>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (_monorailsStorages[name] == null)
if (_monorailsStorages[new MonorailsCollectionInfo(name, string.Empty)] == null)
{
return;
}
_monorailsStorages.Remove(name);
_monorailsStorages.Remove(new MonorailsCollectionInfo(name, string.Empty));
}
public MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail>? this[string ind]
public MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail> this[string ind]
{
get
{
//TO DO
if (_monorailsStorages[ind] == null)
MonorailsCollectionInfo index = new MonorailsCollectionInfo(ind, string.Empty);
if (_monorailsStorages.ContainsKey(index))
{
return null;
return _monorailsStorages[index];
}
return _monorailsStorages[ind];
return null;
}
}
public void SaveData(string filename)
@ -60,7 +66,7 @@ namespace Monorail.Generics
}
StringBuilder data = new();
foreach (KeyValuePair<string,MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail>>
foreach (KeyValuePair<MonorailsCollectionInfo,MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail>>
record in _monorailsStorages)
{
StringBuilder records = new();
@ -122,7 +128,7 @@ namespace Monorail.Generics
}
}
}
_monorailsStorages.Add(record[0], collection);
_monorailsStorages.Add(new MonorailsCollectionInfo(record[0], string.Empty), collection);
}
}
}

View File

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
@ -18,11 +19,16 @@ namespace Monorail.Generics
_maxCount = count;
_places = new List<T?>(count);
}
public int Insert(T monorail)
public void SortSet(IComparer<T?> comparer) =>
_places.Sort(comparer);
public int Insert(T monorail, IEqualityComparer<T?>? equal = null)
{
return Insert(monorail, 0);
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(monorail, 0, equal);
return 0;
}
public int Insert(T monorail, int position)
public bool Insert(T monorail, int position, IEqualityComparer<T?>? equal = null)
{
if (Count == _maxCount || position >= _maxCount)
throw new StorageOverflowException(Count);
@ -30,16 +36,13 @@ namespace Monorail.Generics
if (position < 0 || monorail == null)
throw new StorageOverflowException("Ошибка. Объект не найден или введённый " +
"номер позиции = отрицательное число");
if (Count == 0)
if (equal != null)
{
_places.Add(monorail);
if (_places.Contains(monorail, equal))
throw new ArgumentException(nameof(monorail));
}
else
{
_places.Insert(position, monorail);
}
return Count;
return true;
}
public bool Remove(int position)
{