diff --git a/RoadTrain/RoadTrain/DrawingTrainEqutables.cs b/RoadTrain/RoadTrain/DrawingTrainEqutables.cs new file mode 100644 index 0000000..d0bb755 --- /dev/null +++ b/RoadTrain/RoadTrain/DrawingTrainEqutables.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics.CodeAnalysis; +using RoadTrain.Entities; +using RoadTrain.DrawingObjects; + + +namespace RoadTrain.Generics + +{ + internal class DrawingTrainEqutables : IEqualityComparer + { + public bool Equals(DrawingRoadTrain? x, DrawingRoadTrain? y) + { + if (x == null || x.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(y)); + } + + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityRoadTrain.Speed != y.EntityRoadTrain.Speed) + { + return false; + } + if (x.EntityRoadTrain.Weight != y.EntityRoadTrain.Weight) + { + return false; + } + if (x.EntityRoadTrain.BodyColor != y.EntityRoadTrain.BodyColor) + { + return false; + } + if (x is DrawingRoadTrainWithTank && y is DrawingRoadTrainWithTank) + { + EntityRoadTrainWithTank EntityX = (EntityRoadTrainWithTank)x.EntityRoadTrain; + EntityRoadTrainWithTank EntityY = (EntityRoadTrainWithTank)y.EntityRoadTrain; + if (EntityX.Tank != EntityY.Tank) + return false; + if (EntityX.Brush != EntityY.Brush) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingRoadTrain obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/RoadTrain/RoadTrain/SetGeneric.cs b/RoadTrain/RoadTrain/SetGeneric.cs index 9b01a14..f23b01b 100644 --- a/RoadTrain/RoadTrain/SetGeneric.cs +++ b/RoadTrain/RoadTrain/SetGeneric.cs @@ -12,26 +12,32 @@ namespace RoadTrain.Generics { private readonly List _places; public int Count => _places.Count; - private readonly int _maxCount; - + public readonly int _maxCount; public SetGeneric(int count) { _maxCount = count; _places = new List(_maxCount); } - public bool Insert(T train) + public void SortSet(IComparer comparer) => _places.Sort(comparer); + public int Insert(T train, IEqualityComparer? equal = null) { - return Insert(train, 0); + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + return Insert(train, 0, equal); } - public bool Insert(T train, int position) + public int Insert(T train, int position, IEqualityComparer? equal = null) { - if (position < 0 || position >= _maxCount) - throw new StorageOverflowException("Impossible to insert"); - + if (position < 0 || position > _maxCount) + throw new RoadTrainNotFoundException("Вставка невозможна"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - _places.Insert(0, train); - return true; + if (equal != null) + { + if (_places.Contains(train, equal)) + throw new ArgumentException(nameof(train)); + } + _places.Insert(position, train); + return position; } public bool Remove(int position) { @@ -57,7 +63,6 @@ namespace RoadTrain.Generics _places[position] = value; } } - public IEnumerable GetTrains(int? maxTrains = null) { for (int i = 0; i < _places.Count; ++i) diff --git a/RoadTrain/RoadTrain/TrainCollection.Designer.cs b/RoadTrain/RoadTrain/TrainCollection.Designer.cs index 6fb36de..e26e5de 100644 --- a/RoadTrain/RoadTrain/TrainCollection.Designer.cs +++ b/RoadTrain/RoadTrain/TrainCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { panelCollection = new Panel(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); groupBoxSets = new GroupBox(); textBoxStorageName = new TextBox(); buttonDelObject = new Button(); @@ -54,6 +56,8 @@ // // panelCollection // + panelCollection.Controls.Add(ButtonSortByColor); + panelCollection.Controls.Add(ButtonSortByType); panelCollection.Controls.Add(groupBoxSets); panelCollection.Controls.Add(maskedTextBoxNumber); panelCollection.Controls.Add(labelCollection); @@ -63,9 +67,31 @@ panelCollection.Location = new Point(763, -1); panelCollection.Margin = new Padding(3, 2, 3, 2); panelCollection.Name = "panelCollection"; - panelCollection.Size = new Size(191, 402); + panelCollection.Size = new Size(191, 472); panelCollection.TabIndex = 3; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(14, 311); + ButtonSortByColor.Margin = new Padding(3, 2, 3, 2); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(165, 30); + ButtonSortByColor.TabIndex = 14; + ButtonSortByColor.Text = "Cортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(14, 277); + ButtonSortByType.Margin = new Padding(3, 2, 3, 2); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(165, 30); + ButtonSortByType.TabIndex = 13; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // groupBoxSets // groupBoxSets.Controls.Add(textBoxStorageName); @@ -155,7 +181,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(14, 301); + maskedTextBoxNumber.Location = new Point(14, 379); maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(164, 23); @@ -172,7 +198,7 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(14, 362); + buttonRefreshCollection.Location = new Point(14, 440); buttonRefreshCollection.Margin = new Padding(3, 2, 3, 2); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(164, 30); @@ -183,7 +209,7 @@ // // buttonRemoveTrain // - buttonRemoveTrain.Location = new Point(14, 328); + buttonRemoveTrain.Location = new Point(13, 406); buttonRemoveTrain.Margin = new Padding(3, 2, 3, 2); buttonRemoveTrain.Name = "buttonRemoveTrain"; buttonRemoveTrain.Size = new Size(164, 30); @@ -194,7 +220,7 @@ // // buttonAddTrain // - buttonAddTrain.Location = new Point(14, 267); + buttonAddTrain.Location = new Point(14, 345); buttonAddTrain.Margin = new Padding(3, 2, 3, 2); buttonAddTrain.Name = "buttonAddTrain"; buttonAddTrain.Size = new Size(164, 30); @@ -205,10 +231,10 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(2, -1); + pictureBoxCollection.Location = new Point(12, 6); pictureBoxCollection.Margin = new Padding(3, 2, 3, 2); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(756, 402); + pictureBoxCollection.Size = new Size(746, 465); pictureBoxCollection.TabIndex = 2; pictureBoxCollection.TabStop = false; // @@ -225,7 +251,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(956, 400); + ClientSize = new Size(956, 482); Controls.Add(panelCollection); Controls.Add(pictureBoxCollection); MainMenuStrip = menuStrip1; @@ -260,5 +286,7 @@ private ToolStripMenuItem загрузкаToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/RoadTrain/RoadTrain/TrainCollection.cs b/RoadTrain/RoadTrain/TrainCollection.cs index 516376a..f6cb344 100644 --- a/RoadTrain/RoadTrain/TrainCollection.cs +++ b/RoadTrain/RoadTrain/TrainCollection.cs @@ -34,7 +34,7 @@ namespace RoadTrain listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i]); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) @@ -111,25 +111,28 @@ namespace RoadTrain train.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); try { - bool isAddedSuccessfully = obj + train; - - if (isAddedSuccessfully) + if (obj + train != -1) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = obj.ShowTrains(); - _logger.LogInformation($"Объект добавлен"); - } + _logger.LogInformation($"Объект {obj.GetType()} добавлен"); + } else - { + { MessageBox.Show("Не удалось добавить объект"); _logger.LogInformation($"Не удалось добавить объект"); + } } - } catch (StorageOverflowException ex) { MessageBox.Show(ex.Message); - _logger.LogWarning(($"Ошибка переполнения памяти")); + _logger.LogWarning(ex.ToString()); + } + catch (ArgumentException ex) + { + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {ex.Message}"); } } @@ -227,5 +230,24 @@ namespace RoadTrain } ReloadObjects(); } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTrains(new TrainCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTrains(new TrainCompareByColor()); + + private void CompareTrains(IComparer comparer) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowTrains(); + } } } \ No newline at end of file diff --git a/RoadTrain/RoadTrain/TrainCollectionInfo.cs b/RoadTrain/RoadTrain/TrainCollectionInfo.cs new file mode 100644 index 0000000..62262aa --- /dev/null +++ b/RoadTrain/RoadTrain/TrainCollectionInfo.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RoadTrain.Generics +{ + internal class TrainCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public TrainCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(TrainCollectionInfo? other) + { + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/RoadTrain/RoadTrain/TrainCompareByColor.cs b/RoadTrain/RoadTrain/TrainCompareByColor.cs new file mode 100644 index 0000000..b2bb02d --- /dev/null +++ b/RoadTrain/RoadTrain/TrainCompareByColor.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RoadTrain.DrawingObjects; + + +namespace RoadTrain.Generics +{ + internal class TrainCompareByColor : IComparer + { + public int Compare(DrawingRoadTrain? x, DrawingRoadTrain? y) + { + if (x == null || x.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityRoadTrain.BodyColor.Name != y.EntityRoadTrain.BodyColor.Name) + { + return x.EntityRoadTrain.BodyColor.Name.CompareTo(y.EntityRoadTrain.BodyColor.Name); + } + var speedCompare = x.EntityRoadTrain.Speed.CompareTo(y.EntityRoadTrain.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityRoadTrain.Weight.CompareTo(y.EntityRoadTrain.Weight); + } + } +} \ No newline at end of file diff --git a/RoadTrain/RoadTrain/TrainCompareByType.cs b/RoadTrain/RoadTrain/TrainCompareByType.cs new file mode 100644 index 0000000..3b55861 --- /dev/null +++ b/RoadTrain/RoadTrain/TrainCompareByType.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RoadTrain.DrawingObjects; + + +namespace RoadTrain.Generics +{ + internal class TrainCompareByType : IComparer + { + public int Compare(DrawingRoadTrain? x, DrawingRoadTrain? y) + { + if (x == null || x.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityRoadTrain.Speed.CompareTo(y.EntityRoadTrain.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityRoadTrain.Weight.CompareTo(y.EntityRoadTrain.Weight); + } + } +} \ No newline at end of file diff --git a/RoadTrain/RoadTrain/TrainsGenericCollection.cs b/RoadTrain/RoadTrain/TrainsGenericCollection.cs index d0ae52e..c43994f 100644 --- a/RoadTrain/RoadTrain/TrainsGenericCollection.cs +++ b/RoadTrain/RoadTrain/TrainsGenericCollection.cs @@ -3,10 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; - using RoadTrain.DrawingObjects; using RoadTrain.MovementStrategy; -using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace RoadTrain.Generics { @@ -20,6 +18,7 @@ namespace RoadTrain.Generics private readonly int _placeSizeWidth = 200; private readonly int _placeSizeHeight = 100; private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public TrainsGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -28,13 +27,13 @@ namespace RoadTrain.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static bool operator +(TrainsGenericCollection collect, T? obj) + public static int operator +(TrainsGenericCollection collect, T? obj) { if (obj == null) { - return false; + return -1; } - return (bool)collect?._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawingTrainEqutables()); } public static T? operator -(TrainsGenericCollection collect, int pos) { @@ -88,4 +87,4 @@ namespace RoadTrain.Generics } } } -} +} \ No newline at end of file diff --git a/RoadTrain/RoadTrain/TrainsGenericStorage.cs b/RoadTrain/RoadTrain/TrainsGenericStorage.cs index 1c8b42a..fee7a92 100644 --- a/RoadTrain/RoadTrain/TrainsGenericStorage.cs +++ b/RoadTrain/RoadTrain/TrainsGenericStorage.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.DirectoryServices.ActiveDirectory; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -12,48 +11,50 @@ namespace RoadTrain.Generics { internal class TrainsGenericStorage { - readonly Dictionary> _trainStorages; - public List Keys => _trainStorages.Keys.ToList(); + readonly Dictionary> _trainStorages; + public List Keys => _trainStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; public TrainsGenericStorage(int pictureWidth, int pictureHeight) { - _trainStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { - if (_trainStorages.ContainsKey(name)) + if (_trainStorages.ContainsKey(new TrainCollectionInfo(name, string.Empty))) { return; } - _trainStorages[name] = new TrainsGenericCollection(_pictureWidth, _pictureHeight); + _trainStorages.Add(new TrainCollectionInfo(name, string.Empty), new TrainsGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (!_trainStorages.ContainsKey(name)) + if (!_trainStorages.ContainsKey(new TrainCollectionInfo(name, string.Empty))) { return; } - _trainStorages.Remove(name); + _trainStorages.Remove(new TrainCollectionInfo(name, string.Empty)); } public TrainsGenericCollection? this[string ind] { get { - if (_trainStorages.ContainsKey(ind)) + TrainCollectionInfo indObj = new TrainCollectionInfo(ind, string.Empty); + + if (_trainStorages.ContainsKey(indObj)) { - return _trainStorages[ind]; + return _trainStorages[indObj]; } return null; } } - private static readonly char _separatorForKeyValue = '|'; - private readonly char _separatorRecords = ';'; - private static readonly char _separatorForObject = ':'; public void SaveData(string filename) { if (File.Exists(filename)) @@ -61,14 +62,14 @@ namespace RoadTrain.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _trainStorages) + foreach (KeyValuePair> record in _trainStorages) { StringBuilder records = new(); foreach (DrawingRoadTrain? elem in record.Value.GetTrain) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -92,7 +93,6 @@ namespace RoadTrain.Generics string line; if (!(((line = sr.ReadLine()) != null) && line.StartsWith("TrainStorage"))) { - //если нет такой записи, то это не те данные throw new ArgumentException("Неверный формат данных"); } _trainStorages.Clear(); @@ -115,16 +115,16 @@ namespace RoadTrain.Generics _pictureWidth, _pictureHeight); if (train != null) { - if (!(collection + train)) + if (collection + train == -1) { throw new InvalidOperationException("Ошибка добавления в коллекцию"); } } } - _trainStorages.Add(record[0], collection); + _trainStorages.Add(new TrainCollectionInfo(record[0], string.Empty), collection); } return true; } } } -} +} \ No newline at end of file