From faa1e41ee054cee1dce59b744fce482baa7a39ef Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Fri, 29 Dec 2023 20:32:17 +0400 Subject: [PATCH 1/3] lab_8 --- .../AirFighter/AirFighterCollectionInfo.cs | 28 +++++++++ .../AirFighter/AirFighterCompareByColor .cs | 50 ++++++++++++++++ .../AirFighter/AirFighterCompareByType.cs | 32 ++++++++++ .../AirFighter/AirFighterGenericCollection.cs | 7 ++- .../AirFighter/AirFighterGenericStorage.cs | 36 +++++++---- .../AirFighter/DrawningAirFighterEqutables.cs | 59 +++++++++++++++++++ .../FormAirFighterCollection.Designer.cs | 36 +++++++++-- .../AirFighter/FormAirFighterCollection.cs | 25 +++++++- AirFighter/AirFighter/SetGeneric.cs | 37 ++++-------- AirFighter/AirFighter/appsettings.json | 2 +- 10 files changed, 263 insertions(+), 49 deletions(-) create mode 100644 AirFighter/AirFighter/AirFighterCollectionInfo.cs create mode 100644 AirFighter/AirFighter/AirFighterCompareByColor .cs create mode 100644 AirFighter/AirFighter/AirFighterCompareByType.cs create mode 100644 AirFighter/AirFighter/DrawningAirFighterEqutables.cs diff --git a/AirFighter/AirFighter/AirFighterCollectionInfo.cs b/AirFighter/AirFighter/AirFighterCollectionInfo.cs new file mode 100644 index 0000000..503f675 --- /dev/null +++ b/AirFighter/AirFighter/AirFighterCollectionInfo.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter.Generics +{ + internal class AirFighterCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public AirFighterCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(AirFighterCollectionInfo? other) + { + return Name == other.Name; + } + + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/AirFighter/AirFighter/AirFighterCompareByColor .cs b/AirFighter/AirFighter/AirFighterCompareByColor .cs new file mode 100644 index 0000000..642f89c --- /dev/null +++ b/AirFighter/AirFighter/AirFighterCompareByColor .cs @@ -0,0 +1,50 @@ +using AirFighter.DrawningObjects; +using AirFighter.Entities; +using AirFighter.Generics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class fighterCompareByColor : IComparer + { + public int Compare(DrawningAirFighter? x, DrawningAirFighter? y) + { + if (x == null || x.EntityAirFighter == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityAirFighter == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityAirFighter.BodyColor.Name != y.EntityAirFighter.BodyColor.Name) + { + return x.EntityAirFighter.BodyColor.Name.CompareTo(y.EntityAirFighter.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is EntityAirFighter) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningAirFighterMilitary) + { + EntityAirFighterMilitary EntityX = (EntityAirFighterMilitary)x.EntityAirFighter; + EntityAirFighterMilitary EntityY = (EntityAirFighterMilitary)y.EntityAirFighter; + if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name) + { + return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name); + } + } + var speedCompare = x.EntityAirFighter.Speed.CompareTo(y.EntityAirFighter.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityAirFighter.Weight.CompareTo(y.EntityAirFighter.Weight); + } + } +} diff --git a/AirFighter/AirFighter/AirFighterCompareByType.cs b/AirFighter/AirFighter/AirFighterCompareByType.cs new file mode 100644 index 0000000..1f735cd --- /dev/null +++ b/AirFighter/AirFighter/AirFighterCompareByType.cs @@ -0,0 +1,32 @@ +using AirFighter.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class AirFighterCompareByType : IComparer + { + public int Compare(DrawningAirFighter? x, DrawningAirFighter? y) + { + if (x == null || x.EntityAirFighter == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityAirFighter == null) + throw new ArgumentNullException(nameof(y)); + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityAirFighter.Speed.CompareTo(y.EntityAirFighter.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityAirFighter.Weight.CompareTo(y.EntityAirFighter.Weight); + } + } +} diff --git a/AirFighter/AirFighter/AirFighterGenericCollection.cs b/AirFighter/AirFighter/AirFighterGenericCollection.cs index 95afa39..4a7427c 100644 --- a/AirFighter/AirFighter/AirFighterGenericCollection.cs +++ b/AirFighter/AirFighter/AirFighterGenericCollection.cs @@ -21,6 +21,7 @@ namespace AirFighter.Generics private readonly int _placeSizeHeight = 180; private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public AirFighterGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -29,13 +30,13 @@ namespace AirFighter.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static int? operator +(AirFighterGenericCollection collect, T? obj) + public static bool operator +(AirFighterGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj); + return collect?._collection.Insert(obj, new DrawningAirFighterEqutables()) ?? false; } public static T operator -(AirFighterGenericCollection collect, int pos) { diff --git a/AirFighter/AirFighter/AirFighterGenericStorage.cs b/AirFighter/AirFighter/AirFighterGenericStorage.cs index 5e2295e..843e080 100644 --- a/AirFighter/AirFighter/AirFighterGenericStorage.cs +++ b/AirFighter/AirFighter/AirFighterGenericStorage.cs @@ -14,37 +14,49 @@ namespace AirFighter.Generics { internal class AirFighterGenericStorage { - readonly Dictionary> _fighterStorages; + readonly Dictionary> _fighterStorages; - public List Keys => _fighterStorages.Keys.ToList(); + public List Keys => _fighterStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; public AirFighterGenericStorage(int pictureWidth, int pictureHeight) { - _fighterStorages = new Dictionary>(); + _fighterStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { - if (_fighterStorages.ContainsKey(name)) return; - _fighterStorages[name] = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); + // TODO Прописать логику для добавления + if (!_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + { + var fighterCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); + _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), fighterCollection); + } } public void DelSet(string name) { - if (!_fighterStorages.ContainsKey(name)) return; - _fighterStorages.Remove(name); + // TODO Прописать логику для удаления + if (_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + { + _fighterStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); + } } - public AirFighterGenericCollection? - this[string ind] + public AirFighterGenericCollection? this[string ind] { get { - if (_fighterStorages.ContainsKey(ind)) return _fighterStorages[ind]; + AirFighterCollectionInfo indObj = new AirFighterCollectionInfo(ind, string.Empty); + // TODO Продумать логику получения набора + if (_fighterStorages.ContainsKey(indObj)) + { + return _fighterStorages[indObj]; + } return null; + } } @@ -60,7 +72,7 @@ namespace AirFighter.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _fighterStorages) + foreach (KeyValuePair> record in _fighterStorages) { StringBuilder records = new(); foreach (DrawningAirFighter? elem in record.Value.GetAirFighter) @@ -129,7 +141,7 @@ namespace AirFighter.Generics } } } - _fighterStorages.Add(name, collection); + _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); } } } diff --git a/AirFighter/AirFighter/DrawningAirFighterEqutables.cs b/AirFighter/AirFighter/DrawningAirFighterEqutables.cs new file mode 100644 index 0000000..9129ccf --- /dev/null +++ b/AirFighter/AirFighter/DrawningAirFighterEqutables.cs @@ -0,0 +1,59 @@ +using AirFighter.DrawningObjects; +using AirFighter.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class DrawningAirFighterEqutables : IEqualityComparer + { + public bool Equals(DrawningAirFighter? x, DrawningAirFighter? y) + { + if (x == null || x.EntityAirFighter == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAirFighter == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityAirFighter.Speed != y.EntityAirFighter.Speed) + { + return false; + } + if (x.EntityAirFighter.Weight != y.EntityAirFighter.Weight) + { + return false; + } + if (x.EntityAirFighter.BodyColor != y.EntityAirFighter.BodyColor) + { + return false; + } + if (x is EntityAirFighterMilitary && y is EntityAirFighterMilitary) + { + EntityAirFighterMilitary EntityX = (EntityAirFighterMilitary)x.EntityAirFighter; + EntityAirFighterMilitary EntityY = (EntityAirFighterMilitary)y.EntityAirFighter; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + if (EntityX.Wing != EntityY.Wing) + return false; + if (EntityX.Rocket != EntityY.Rocket) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningAirFighter obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs b/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs index 918c733..d9837cd 100644 --- a/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs +++ b/AirFighter/AirFighter/FormAirFighterCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + ButtonSortByType = new Button(); + ButtonSortByColor = new Button(); groupBox2 = new GroupBox(); buttonDelObject = new Button(); listBoxStorage = new ListBox(); @@ -53,6 +55,8 @@ // // groupBox1 // + groupBox1.Controls.Add(ButtonSortByType); + groupBox1.Controls.Add(ButtonSortByColor); groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(buttonRemoveFighter); @@ -65,6 +69,26 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(17, 251); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(166, 23); + ButtonSortByType.TabIndex = 3; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(17, 280); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(166, 23); + ButtonSortByColor.TabIndex = 4; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // // groupBox2 // groupBox2.Controls.Add(buttonDelObject); @@ -73,14 +97,14 @@ groupBox2.Controls.Add(textBoxStorageName); groupBox2.Location = new Point(3, 19); groupBox2.Name = "groupBox2"; - groupBox2.Size = new Size(200, 284); + groupBox2.Size = new Size(200, 226); groupBox2.TabIndex = 6; groupBox2.TabStop = false; groupBox2.Text = "Наборы"; // // buttonDelObject // - buttonDelObject.Location = new Point(18, 244); + buttonDelObject.Location = new Point(18, 188); buttonDelObject.Name = "buttonDelObject"; buttonDelObject.Size = new Size(159, 34); buttonDelObject.TabIndex = 2; @@ -94,7 +118,7 @@ listBoxStorage.ItemHeight = 15; listBoxStorage.Location = new Point(15, 88); listBoxStorage.Name = "listBoxStorage"; - listBoxStorage.Size = new Size(162, 139); + listBoxStorage.Size = new Size(162, 94); listBoxStorage.TabIndex = 2; listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; // @@ -183,14 +207,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(180, 22); + SaveToolStripMenuItem.Size = new Size(133, 22); SaveToolStripMenuItem.Text = "Сохранить"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Size = new Size(133, 22); LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -244,5 +268,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog1; private SaveFileDialog saveFileDialog1; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirFighterCollection.cs b/AirFighter/AirFighter/FormAirFighterCollection.cs index c11023f..dbee92e 100644 --- a/AirFighter/AirFighter/FormAirFighterCollection.cs +++ b/AirFighter/AirFighter/FormAirFighterCollection.cs @@ -107,7 +107,7 @@ namespace AirFighter /// /// /// - + private void ButtonAddAirFighter_Click(object sender, EventArgs e) { if (listBoxStorage.SelectedIndex == -1) @@ -173,7 +173,7 @@ namespace AirFighter } else { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show("Не удалось удалить объект"); _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorage.SelectedItem.ToString()}"); _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorage.SelectedItem.ToString()}"); } } @@ -234,6 +234,27 @@ namespace AirFighter } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAirFighter(new AirFighterCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAirFighter(new fighterCompareByColor()); + //сортировка + private void CompareAirFighter(IComparer comparer) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowAirFighter(); + } + } } diff --git a/AirFighter/AirFighter/SetGeneric.cs b/AirFighter/AirFighter/SetGeneric.cs index 291e24c..41a16d5 100644 --- a/AirFighter/AirFighter/SetGeneric.cs +++ b/AirFighter/AirFighter/SetGeneric.cs @@ -14,41 +14,26 @@ namespace AirFighter.Generics _maxCount = count; _places = new List(count); } - public int Insert(T fighter) + public void SortSet(IComparer comparer) => _places.Sort(comparer); + public bool Insert(T fighter, IEqualityComparer? equal = null) { - if (_places.Count == 0) - { - _places.Add(fighter); - return 0; - } - else - { - if (_places.Count < _maxCount) - { - _places.Add(fighter); - 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(fighter, 0, equal); + return true; } - public bool Insert(T fighter, int position) + public bool Insert(T fighter, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new AirFighterNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(position); - _places.Insert(0, fighter); + if (equal != null) + { + if (_places.Contains(fighter, equal)) + throw new ArgumentException(nameof(fighter)); + } + _places.Insert(position, fighter); return true; } public bool Remove(int position) diff --git a/AirFighter/AirFighter/appsettings.json b/AirFighter/AirFighter/appsettings.json index 66e218d..c9ceae7 100644 --- a/AirFighter/AirFighter/appsettings.json +++ b/AirFighter/AirFighter/appsettings.json @@ -14,7 +14,7 @@ ], "Enrich": [ "FromLogContext", "WithShipName", "WithThreadId" ], "Properties": { - "Application": "AirFighter" + "Application": "ContainerShip" } } } \ No newline at end of file -- 2.25.1 From ff8cbd3ea60ae2bffa6085155ac108265deedb24 Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Fri, 29 Dec 2023 20:42:01 +0400 Subject: [PATCH 2/3] lab_8 --- .../AirFighter/AirFighterGenericStorage.cs | 88 ++++++++++++------- .../AirFighter/FormAirFighterCollection.cs | 2 +- AirFighter/AirFighter/appsettings.json | 2 +- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/AirFighter/AirFighter/AirFighterGenericStorage.cs b/AirFighter/AirFighter/AirFighterGenericStorage.cs index 843e080..3922efc 100644 --- a/AirFighter/AirFighter/AirFighterGenericStorage.cs +++ b/AirFighter/AirFighter/AirFighterGenericStorage.cs @@ -3,68 +3,82 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirFighter.DrawningObjects; -using AirFighter.MovementStrategy; -using AirFighter.Generics; -using System.IO; -using AirFighter.Exceptions; +using AirFighter.Drawnings; +using AirFighter.MovementStrategy; +using AirFighter.DrawningObjects; +using AirFighter.Exceptions; +using AirFighter.Exceptions; namespace AirFighter.Generics + { internal class AirFighterGenericStorage { - readonly Dictionary> _fighterStorages; - - public List Keys => _fighterStorages.Keys.ToList(); - + readonly Dictionary> _lincornStorages; + public List Keys => _lincornStorages.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 AirFighterGenericStorage(int pictureWidth, int pictureHeight) { - _fighterStorages = new Dictionary>(); + _lincornStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } + /// + /// Добавление набора + /// + /// Название набора public void AddSet(string name) { // TODO Прописать логику для добавления - if (!_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + if (!_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) { - var fighterCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); - _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), fighterCollection); + var lincornCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); + _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), lincornCollection); } } - + /// + /// Удаление набора + /// + /// Название набора public void DelSet(string name) { // TODO Прописать логику для удаления - if (_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + if (_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) { - _fighterStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); + _lincornStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); } } - + /// + /// Доступ к набору + /// + /// + /// public AirFighterGenericCollection? this[string ind] { get { AirFighterCollectionInfo indObj = new AirFighterCollectionInfo(ind, string.Empty); // TODO Продумать логику получения набора - if (_fighterStorages.ContainsKey(indObj)) + if (_lincornStorages.ContainsKey(indObj)) { - return _fighterStorages[indObj]; + return _lincornStorages[indObj]; } return null; } } - - private static readonly char _separatorForKeyValue = '|'; - - private readonly char _separatorRecords = ';'; - - private static readonly char _separatorForObject = ':'; + /// + /// Сохранение информации по установкам в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных public void SaveData(string filename) { if (File.Exists(filename)) @@ -72,7 +86,7 @@ namespace AirFighter.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _fighterStorages) + foreach (KeyValuePair> record in _lincornStorages) { StringBuilder records = new(); foreach (DrawningAirFighter? elem in record.Value.GetAirFighter) @@ -88,15 +102,22 @@ namespace AirFighter.Generics using (StreamWriter writer = new StreamWriter(filename)) { - writer.Write($"fighterStorage{Environment.NewLine}{data}"); + writer.Write($"lincornStorage{Environment.NewLine}{data}"); } } + + // + /// Загрузка информации по установкам в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных public void LoadData(string filename) { if (!File.Exists(filename)) { throw new Exception("Файл не найден"); } + using (StreamReader reader = new StreamReader(filename)) { string cheker = reader.ReadLine(); @@ -104,11 +125,11 @@ namespace AirFighter.Generics { throw new Exception("Нет данных для загрузки"); } - if (!cheker.StartsWith("fighterStorage")) + if (!cheker.StartsWith("lincornStorage")) { throw new Exception("Неверный формат ввода"); } - _fighterStorages.Clear(); + _lincornStorages.Clear(); string strs; bool firstinit = true; while ((strs = reader.ReadLine()) != null) @@ -126,11 +147,11 @@ namespace AirFighter.Generics AirFighterGenericCollection collection = new(_pictureWidth, _pictureHeight); foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords)) { - DrawningAirFighter? fighter = + DrawningAirFighter? lincorn = data?.CreateDrawningAirFighter(_separatorForObject, _pictureWidth, _pictureHeight); - if (fighter != null) + if (lincorn != null) { - try { _ = collection + fighter; } + try { _ = collection + lincorn; } catch (AirFighterNotFoundException e) { throw e; @@ -141,9 +162,10 @@ namespace AirFighter.Generics } } } - _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); + _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); } } } } } + diff --git a/AirFighter/AirFighter/FormAirFighterCollection.cs b/AirFighter/AirFighter/FormAirFighterCollection.cs index dbee92e..16f0049 100644 --- a/AirFighter/AirFighter/FormAirFighterCollection.cs +++ b/AirFighter/AirFighter/FormAirFighterCollection.cs @@ -38,7 +38,7 @@ namespace AirFighter 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)) diff --git a/AirFighter/AirFighter/appsettings.json b/AirFighter/AirFighter/appsettings.json index c9ceae7..66e218d 100644 --- a/AirFighter/AirFighter/appsettings.json +++ b/AirFighter/AirFighter/appsettings.json @@ -14,7 +14,7 @@ ], "Enrich": [ "FromLogContext", "WithShipName", "WithThreadId" ], "Properties": { - "Application": "ContainerShip" + "Application": "AirFighter" } } } \ No newline at end of file -- 2.25.1 From b1be9c302ddd287f62a5a9115b1a1923a2d7b283 Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Fri, 29 Dec 2023 21:02:13 +0400 Subject: [PATCH 3/3] lab_8 --- .../AirFighter/AirFighterCompareByColor .cs | 35 +++++--- .../AirFighter/AirFighterGenericStorage.cs | 89 +++++++------------ 2 files changed, 55 insertions(+), 69 deletions(-) diff --git a/AirFighter/AirFighter/AirFighterCompareByColor .cs b/AirFighter/AirFighter/AirFighterCompareByColor .cs index 642f89c..895e91a 100644 --- a/AirFighter/AirFighter/AirFighterCompareByColor .cs +++ b/AirFighter/AirFighter/AirFighterCompareByColor .cs @@ -14,35 +14,42 @@ namespace AirFighter public int Compare(DrawningAirFighter? x, DrawningAirFighter? y) { if (x == null || x.EntityAirFighter == null) + { throw new ArgumentNullException(nameof(x)); + } if (y == null || y.EntityAirFighter == null) + { throw new ArgumentNullException(nameof(y)); + } - if (x.EntityAirFighter.BodyColor.Name != y.EntityAirFighter.BodyColor.Name) + var bodyColorCompare = x.EntityAirFighter.BodyColor.Name.CompareTo(y.EntityAirFighter.BodyColor.Name); + + if (bodyColorCompare != 0) { - return x.EntityAirFighter.BodyColor.Name.CompareTo(y.EntityAirFighter.BodyColor.Name); + return bodyColorCompare; } - if (x.GetType().Name != y.GetType().Name) + + if (x.EntityAirFighter is EntityAirFighterMilitary xEntitySailCatamaran && y.EntityAirFighter is EntityAirFighterMilitary yEntitySailCatamaran) { - if (x is EntityAirFighter) - return -1; - else - return 1; - } - if (x.GetType().Name == y.GetType().Name && x is DrawningAirFighterMilitary) - { - EntityAirFighterMilitary EntityX = (EntityAirFighterMilitary)x.EntityAirFighter; - EntityAirFighterMilitary EntityY = (EntityAirFighterMilitary)y.EntityAirFighter; - if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name) + var BodyColorCompare = xEntitySailCatamaran.BodyColor.Name.CompareTo(yEntitySailCatamaran.BodyColor.Name); + if (BodyColorCompare != 0) { - return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name); + return BodyColorCompare; + } + var AdditionalColorCompare = xEntitySailCatamaran.AdditionalColor.Name.CompareTo(yEntitySailCatamaran.AdditionalColor.Name); + if (AdditionalColorCompare != 0) + { + return AdditionalColorCompare; } } + var speedCompare = x.EntityAirFighter.Speed.CompareTo(y.EntityAirFighter.Speed); if (speedCompare != 0) + { return speedCompare; + } return x.EntityAirFighter.Weight.CompareTo(y.EntityAirFighter.Weight); } diff --git a/AirFighter/AirFighter/AirFighterGenericStorage.cs b/AirFighter/AirFighter/AirFighterGenericStorage.cs index 3922efc..95321c2 100644 --- a/AirFighter/AirFighter/AirFighterGenericStorage.cs +++ b/AirFighter/AirFighter/AirFighterGenericStorage.cs @@ -3,82 +3,69 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; - -using AirFighter.Drawnings; -using AirFighter.MovementStrategy; using AirFighter.DrawningObjects; +using AirFighter.MovementStrategy; +using AirFighter.Generics; +using System.IO; using AirFighter.Exceptions; -using AirFighter.Exceptions; + namespace AirFighter.Generics - { internal class AirFighterGenericStorage { - readonly Dictionary> _lincornStorages; - public List Keys => _lincornStorages.Keys.ToList(); + readonly Dictionary> _fighterStorages; + + public List Keys => _fighterStorages.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 AirFighterGenericStorage(int pictureWidth, int pictureHeight) { - _lincornStorages = new Dictionary>(); + _fighterStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } - /// - /// Добавление набора - /// - /// Название набора public void AddSet(string name) { // TODO Прописать логику для добавления - if (!_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + if (!_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) { - var lincornCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); - _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), lincornCollection); + var fighterCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); + _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), fighterCollection); } } - /// - /// Удаление набора - /// - /// Название набора + public void DelSet(string name) { // TODO Прописать логику для удаления - if (_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + if (_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) { - _lincornStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); + _fighterStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); } } - /// - /// Доступ к набору - /// - /// - /// - public AirFighterGenericCollection? this[string ind] + + public AirFighterGenericCollection? + this[string ind] { get { AirFighterCollectionInfo indObj = new AirFighterCollectionInfo(ind, string.Empty); // TODO Продумать логику получения набора - if (_lincornStorages.ContainsKey(indObj)) + if (_fighterStorages.ContainsKey(indObj)) { - return _lincornStorages[indObj]; + return _fighterStorages[indObj]; } return null; } } - /// - /// Сохранение информации по установкам в хранилище в файл - /// - /// Путь и имя файла - /// true - сохранение прошло успешно, false - ошибка при сохранении данных + + private static readonly char _separatorForKeyValue = '|'; + + private readonly char _separatorRecords = ';'; + + private static readonly char _separatorForObject = ':'; public void SaveData(string filename) { if (File.Exists(filename)) @@ -86,7 +73,7 @@ namespace AirFighter.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _lincornStorages) + foreach (KeyValuePair> record in _fighterStorages) { StringBuilder records = new(); foreach (DrawningAirFighter? elem in record.Value.GetAirFighter) @@ -102,22 +89,15 @@ namespace AirFighter.Generics using (StreamWriter writer = new StreamWriter(filename)) { - writer.Write($"lincornStorage{Environment.NewLine}{data}"); + writer.Write($"shipStorage{Environment.NewLine}{data}"); } } - - // - /// Загрузка информации по установкам в хранилище из файла - /// - /// Путь и имя файла - /// true - загрузка прошла успешно, false - ошибка при загрузке данных public void LoadData(string filename) { if (!File.Exists(filename)) { throw new Exception("Файл не найден"); } - using (StreamReader reader = new StreamReader(filename)) { string cheker = reader.ReadLine(); @@ -125,11 +105,11 @@ namespace AirFighter.Generics { throw new Exception("Нет данных для загрузки"); } - if (!cheker.StartsWith("lincornStorage")) + if (!cheker.StartsWith("fighterStorage")) { throw new Exception("Неверный формат ввода"); } - _lincornStorages.Clear(); + _fighterStorages.Clear(); string strs; bool firstinit = true; while ((strs = reader.ReadLine()) != null) @@ -147,11 +127,11 @@ namespace AirFighter.Generics AirFighterGenericCollection collection = new(_pictureWidth, _pictureHeight); foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords)) { - DrawningAirFighter? lincorn = + DrawningAirFighter? fighter = data?.CreateDrawningAirFighter(_separatorForObject, _pictureWidth, _pictureHeight); - if (lincorn != null) + if (fighter != null) { - try { _ = collection + lincorn; } + try { _ = collection + fighter; } catch (AirFighterNotFoundException e) { throw e; @@ -162,10 +142,9 @@ namespace AirFighter.Generics } } } - _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); + _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); } } } } } - -- 2.25.1