Лаба 8 почти готова

This commit is contained in:
Андрей Байгулов 2023-12-24 15:35:14 +04:00
parent eb6c80a47f
commit e23ed22c72
6 changed files with 124 additions and 40 deletions

View File

@ -30,11 +30,12 @@
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormLocomotiveCollection)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormLocomotiveCollection));
groupBoxSets = new GroupBox(); groupBoxSets = new GroupBox();
ButtonSortByType = new Button();
textBoxSetName = new TextBox(); textBoxSetName = new TextBox();
buttonDeleteSet = new Button(); buttonDeleteSet = new Button();
listBoxStorages = new ListBox(); listBoxStorages = new ListBox();
ButtonAddLocomotive = new Button();
buttonAddSet = new Button(); buttonAddSet = new Button();
ButtonAddLocomotive = new Button();
maskedTextBoxNumber = new MaskedTextBox(); maskedTextBoxNumber = new MaskedTextBox();
ButtonRefreshCollection = new Button(); ButtonRefreshCollection = new Button();
ButtonRemoveLocomotive = new Button(); ButtonRemoveLocomotive = new Button();
@ -47,7 +48,6 @@
openFileDialog = new OpenFileDialog(); openFileDialog = new OpenFileDialog();
groupBoxMenu = new GroupBox(); groupBoxMenu = new GroupBox();
ButtonSortByColor = new Button(); ButtonSortByColor = new Button();
ButtonSortByType = new Button();
groupBoxSets.SuspendLayout(); groupBoxSets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout(); menuStrip.SuspendLayout();
@ -56,18 +56,27 @@
// //
// groupBoxSets // groupBoxSets
// //
groupBoxSets.Controls.Add(ButtonSortByType);
groupBoxSets.Controls.Add(textBoxSetName); groupBoxSets.Controls.Add(textBoxSetName);
groupBoxSets.Controls.Add(buttonDeleteSet); groupBoxSets.Controls.Add(buttonDeleteSet);
groupBoxSets.Controls.Add(listBoxStorages); groupBoxSets.Controls.Add(listBoxStorages);
groupBoxSets.Controls.Add(buttonAddSet); groupBoxSets.Controls.Add(buttonAddSet);
groupBoxSets.Location = new Point(0, 22); groupBoxSets.Location = new Point(0, 22);
groupBoxSets.Name = "groupBoxSets"; groupBoxSets.Name = "groupBoxSets";
groupBoxSets.Size = new Size(242, 314); groupBoxSets.Size = new Size(242, 264);
groupBoxSets.TabIndex = 5; groupBoxSets.TabIndex = 5;
groupBoxSets.TabStop = false; groupBoxSets.TabStop = false;
groupBoxSets.Text = "Наборы"; groupBoxSets.Text = "Наборы";
// //
// ButtonSortByType
//
ButtonSortByType.Location = new Point(6, 295);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(230, 41);
ButtonSortByType.TabIndex = 4;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// textBoxSetName // textBoxSetName
// //
textBoxSetName.Location = new Point(6, 38); textBoxSetName.Location = new Point(6, 38);
@ -95,16 +104,6 @@
listBoxStorages.TabIndex = 1; listBoxStorages.TabIndex = 1;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
// //
// ButtonAddLocomotive
//
ButtonAddLocomotive.Location = new Point(6, 395);
ButtonAddLocomotive.Name = "ButtonAddLocomotive";
ButtonAddLocomotive.Size = new Size(230, 39);
ButtonAddLocomotive.TabIndex = 0;
ButtonAddLocomotive.Text = "Добавить локомотив";
ButtonAddLocomotive.UseVisualStyleBackColor = true;
ButtonAddLocomotive.Click += buttonAddLocomotive_Click;
//
// buttonAddSet // buttonAddSet
// //
buttonAddSet.Location = new Point(6, 67); buttonAddSet.Location = new Point(6, 67);
@ -115,6 +114,16 @@
buttonAddSet.UseVisualStyleBackColor = true; buttonAddSet.UseVisualStyleBackColor = true;
buttonAddSet.Click += ButtonAddObject_Click; buttonAddSet.Click += ButtonAddObject_Click;
// //
// ButtonAddLocomotive
//
ButtonAddLocomotive.Location = new Point(6, 395);
ButtonAddLocomotive.Name = "ButtonAddLocomotive";
ButtonAddLocomotive.Size = new Size(230, 39);
ButtonAddLocomotive.TabIndex = 0;
ButtonAddLocomotive.Text = "Добавить локомотив";
ButtonAddLocomotive.UseVisualStyleBackColor = true;
ButtonAddLocomotive.Click += buttonAddLocomotive_Click;
//
// maskedTextBoxNumber // maskedTextBoxNumber
// //
maskedTextBoxNumber.Location = new Point(6, 440); maskedTextBoxNumber.Location = new Point(6, 440);
@ -197,6 +206,7 @@
// groupBoxMenu // groupBoxMenu
// //
groupBoxMenu.AutoSize = true; groupBoxMenu.AutoSize = true;
groupBoxMenu.Controls.Add(ButtonSortByType);
groupBoxMenu.Controls.Add(ButtonSortByColor); groupBoxMenu.Controls.Add(ButtonSortByColor);
groupBoxMenu.Controls.Add(maskedTextBoxNumber); groupBoxMenu.Controls.Add(maskedTextBoxNumber);
groupBoxMenu.Controls.Add(groupBoxSets); groupBoxMenu.Controls.Add(groupBoxSets);
@ -219,15 +229,7 @@
ButtonSortByColor.TabIndex = 4; ButtonSortByColor.TabIndex = 4;
ButtonSortByColor.Text = "Сортировка по цвету"; ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true; ButtonSortByColor.UseVisualStyleBackColor = true;
// ButtonSortByColor.Click += ButtonSortByColor_Click;
// ButtonSortByType
//
ButtonSortByType.Location = new Point(6, 273);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(230, 41);
ButtonSortByType.TabIndex = 4;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
// //
// FormLocomotiveCollection // FormLocomotiveCollection
// //

View File

@ -157,6 +157,36 @@ namespace ProjectElectricLocomotive
} }
pictureBoxCollection.Image = obj.ShowLocomotives(); pictureBoxCollection.Image = obj.ShowLocomotives();
} }
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType());
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
// TODO продумать логику
}
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void CompareLocomotives(IComparer<DrawingLocomotive?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowLocomotives();
}
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives(); pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives();

View File

@ -4,16 +4,32 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ProjectElectricLocomotive.DrawingObjects; using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.Entities;
namespace ProjectElectricLocomotive.Generics namespace ProjectElectricLocomotive.Generics
{ {
internal interface LocomotiveCompareByColor internal class LocoCompareByColor : IComparer<DrawingLocomotive?>
{ {
public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) public int Compare(DrawingLocomotive? x, DrawingLocomotive? y)
{ {
// TODO реализовать логику сравнения
throw new NotImplementedException();
}
if (x == null || x.EntityLocomotive == null)
throw new NotImplementedException(nameof(x));
if (y == null || y.EntityLocomotive == null)
throw new NotImplementedException(nameof(y));
var bodyColor = x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name);
if (bodyColor != 0) return bodyColor;
if (x.EntityLocomotive is EntityElectricLocomotive &&
y.EntityLocomotive is EntityElectricLocomotive)
{
var addcolor = (x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name.CompareTo((y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name);
if (addcolor != 0) return addcolor;
}
return 1;
}
} }
} }

View File

@ -7,7 +7,7 @@ using ProjectElectricLocomotive.DrawingObjects;
namespace ProjectElectricLocomotive.Generics namespace ProjectElectricLocomotive.Generics
{ {
internal interface LocomotiveCompareByType : IComparer<DrawingLocomotive?> internal class LocomotiveCompareByType : IComparer<DrawingLocomotive?>
{ {
public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) public int Compare(DrawingLocomotive? x, DrawingLocomotive? y)
{ {

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotivesCollectionInfo : IEquatable<LocomotivesCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public LocomotivesCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(LocomotivesCollectionInfo other)
{
if (Name != other?.Name)
return false;
return true;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
}

View File

@ -10,8 +10,8 @@ namespace ProjectElectricLocomotive.Generics
{ {
internal class LocomotivesGenericStorage internal class LocomotivesGenericStorage
{ {
readonly Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage; readonly Dictionary<LocomotivesCollectionInfo, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage;
public List<string> Keys => _locomotivesStorage.Keys.ToList(); public List<LocomotivesCollectionInfo> Keys => _locomotivesStorage.Keys.ToList();
private static readonly char _separatorForKeyValue = '|'; private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';'; private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':'; private static readonly char _separatorForObject = ':';
@ -22,22 +22,23 @@ namespace ProjectElectricLocomotive.Generics
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) public LocomotivesGenericStorage(int pictureWidth, int pictureHeight)
{ {
_locomotivesStorage = new Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>>(); _locomotivesStorage = new Dictionary<LocomotivesCollectionInfo, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
public void AddSet(string name) public void AddSet(string name)
{ {
if (!_locomotivesStorage.ContainsKey(name)) if (!_locomotivesStorage.ContainsKey( new LocomotivesCollectionInfo(name, "")))
{ {
_locomotivesStorage.Add(name, new LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight)); _locomotivesStorage.Add(new LocomotivesCollectionInfo(name, ""),
new LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
} }
} }
public void DelSet(string name) public void DelSet(string name)
{ {
if (_locomotivesStorage.ContainsKey(name)) if (_locomotivesStorage.ContainsKey(new LocomotivesCollectionInfo(name, "")))
{ {
_locomotivesStorage.Remove(name); _locomotivesStorage.Remove(new LocomotivesCollectionInfo(name, ""));
} }
} }
public LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>? public LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>?
@ -45,9 +46,9 @@ namespace ProjectElectricLocomotive.Generics
{ {
get get
{ {
if (_locomotivesStorage.ContainsKey(ind)) if (_locomotivesStorage.ContainsKey(new LocomotivesCollectionInfo(ind, "")))
{ {
return _locomotivesStorage[ind]; return _locomotivesStorage[new LocomotivesCollectionInfo(ind, "")];
} }
return null; return null;
} }
@ -59,7 +60,7 @@ namespace ProjectElectricLocomotive.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage) foreach (KeyValuePair<LocomotivesCollectionInfo, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record in _locomotivesStorage)
{ {
StringBuilder records = new(); StringBuilder records = new();
foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) foreach (DrawingLocomotive? elem in record.Value.GetLocomotives)
@ -126,13 +127,13 @@ namespace ProjectElectricLocomotive.Generics
DrawingLocomotive? loco = elem?.CreateDrawningLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); DrawingLocomotive? loco = elem?.CreateDrawningLocomotive(_separatorForObject, _pictureWidth, _pictureHeight);
if (loco != null) if (loco != null)
{ {
if ((collection + loco) == -1) // for my realization it's -1, for eegov's realization it's boolean if ((collection + loco) == -1)
{ {
throw new Exception("Ошибка добавления "); throw new Exception("Ошибка добавления ");
} }
} }
} }
_locomotivesStorage.Add(record[0], collection); _locomotivesStorage.Add(new LocomotivesCollectionInfo(record[0], string.Empty), collection);
} }
return; return;
} }