From 53d754014b35a0840fac3d0fb3a4c5a48fabf0b1 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Fri, 8 Dec 2023 21:44:38 +0300 Subject: [PATCH] AllDone --- AirFighter/AirFighter.csproj | 7 +++ AirFighter/AirplaneGenericCollection.cs | 3 +- AirFighter/AirplaneNotFoundException.cs | 21 +++++++++ AirFighter/AirplanesGenericStorage.cs | 8 ++-- AirFighter/DrawningAirplane.cs | 1 + AirFighter/ExtentionDrawningAirplane.cs | 4 +- AirFighter/FormAirplaneCollection.cs | 62 +++++++++++++++++-------- AirFighter/Program.cs | 17 +++++-- AirFighter/SetGeneric.cs | 10 ++-- AirFighter/StorageOverflowException.cs | 19 ++++++++ 10 files changed, 120 insertions(+), 32 deletions(-) create mode 100644 AirFighter/AirplaneNotFoundException.cs create mode 100644 AirFighter/StorageOverflowException.cs diff --git a/AirFighter/AirFighter.csproj b/AirFighter/AirFighter.csproj index 567c22c..822b070 100644 --- a/AirFighter/AirFighter.csproj +++ b/AirFighter/AirFighter.csproj @@ -8,6 +8,13 @@ enable + + + + + + + diff --git a/AirFighter/AirplaneGenericCollection.cs b/AirFighter/AirplaneGenericCollection.cs index d602e8f..4a08e12 100644 --- a/AirFighter/AirplaneGenericCollection.cs +++ b/AirFighter/AirplaneGenericCollection.cs @@ -40,8 +40,7 @@ namespace ProjectAirFighter.Generics pos) { T? obj = collect._collection[pos]; - if (obj != null) - collect._collection.Remove(pos); + collect._collection.Remove(pos); return obj; } diff --git a/AirFighter/AirplaneNotFoundException.cs b/AirFighter/AirplaneNotFoundException.cs new file mode 100644 index 0000000..e25f1fc --- /dev/null +++ b/AirFighter/AirplaneNotFoundException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + + +namespace ProjectAirFighter.Exceptions +{ + [Serializable] + internal class AirplaneNotFoundException : ApplicationException + { + public AirplaneNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public AirplaneNotFoundException() : base() { } + public AirplaneNotFoundException(string message) : base(message) { } + public AirplaneNotFoundException(string message, Exception exception) : base(message, exception) { } + protected AirplaneNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} + diff --git a/AirFighter/AirplanesGenericStorage.cs b/AirFighter/AirplanesGenericStorage.cs index b89e988..7bfd6a9 100644 --- a/AirFighter/AirplanesGenericStorage.cs +++ b/AirFighter/AirplanesGenericStorage.cs @@ -48,7 +48,7 @@ namespace ProjectAirFighter.Generics if (data.Length == 0) { - return false; + throw new Exception("Невалиданя операция, нет данных для сохранения"); } string toWrite = $"AirplaneStorage{Environment.NewLine}{data}"; var strs = toWrite.Split(new char[] { '\n', '\r' }, @@ -68,7 +68,7 @@ StringSplitOptions.RemoveEmptyEntries); { if (!File.Exists(filename)) { - return false; + throw new IOException("Файл не найден"); } using (StreamReader sr = new(filename)) { @@ -79,11 +79,11 @@ StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries); if (strs == null || strs.Length == 0) { - return false; + throw new IOException("Нет данных для загрузки"); } if (!strs[0].StartsWith("AirplaneStorage")) { - return false; + throw new IOException("Неверный формат данных"); } _airplaneStorages.Clear(); do diff --git a/AirFighter/DrawningAirplane.cs b/AirFighter/DrawningAirplane.cs index 4c89625..2ad0274 100644 --- a/AirFighter/DrawningAirplane.cs +++ b/AirFighter/DrawningAirplane.cs @@ -16,6 +16,7 @@ namespace ProjectAirFighter.DrawningObjects private int _pictureWidth; private int _pictureHeight; protected int _startPosX; + char separator = '|'; protected int _startPosY; protected readonly int _airplaneWidth = 163; protected readonly int _airplaneHeight = 160; diff --git a/AirFighter/ExtentionDrawningAirplane.cs b/AirFighter/ExtentionDrawningAirplane.cs index 3b0356a..6fbc28a 100644 --- a/AirFighter/ExtentionDrawningAirplane.cs +++ b/AirFighter/ExtentionDrawningAirplane.cs @@ -1,6 +1,8 @@ -using System; +using Microsoft.VisualBasic.Logging; +using System; using System.Collections.Generic; using System.Linq; +using System.ComponentModel; using System.Text; using System.Threading.Tasks; using ProjectAirFighter.Entities; diff --git a/AirFighter/FormAirplaneCollection.cs b/AirFighter/FormAirplaneCollection.cs index cb0d96a..5c39787 100644 --- a/AirFighter/FormAirplaneCollection.cs +++ b/AirFighter/FormAirplaneCollection.cs @@ -1,5 +1,7 @@ using ProjectAirFighter.DrawningObjects; using ProjectAirFighter.Generics; +using Microsoft.Extensions.Logging; +using ProjectAirFighter.Exceptions; using ProjectAirFighter.MovementStrategy; using System; using System.Collections.Generic; @@ -10,6 +12,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using System.Xml.Linq; +using Serilog; +using Microsoft.VisualBasic.Logging; +using Log = Serilog.Log; namespace ProjectAirFighter { @@ -28,15 +34,17 @@ namespace ProjectAirFighter { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { + _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось сохранить"); + MessageBox.Show($"Не сохранилось: {ex.Message}", +"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -44,20 +52,22 @@ namespace ProjectAirFighter { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { + _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); foreach (var collection in _storage.Keys) { listBoxStorages.Items.Add(collection); } - + ReloadObjects(); } - else + catch (Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось загрузить"); + MessageBox.Show($"Не загрузилось: {ex.Message}", +"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -103,6 +113,7 @@ namespace ProjectAirFighter } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); + Log.Information($"Добавлен набор: {textBoxStorageName.Text}"); } private void deleteAirplaneButton_Click(object sender, EventArgs e) @@ -122,15 +133,23 @@ namespace ProjectAirFighter { return; } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + try { + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + var q = obj - pos; MessageBox.Show("Объект удален"); + Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); pictureBoxCollection.Image = obj.ShowAirplanes(); } - else + catch (AirplaneNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show(ex.Message); + } + catch (FormatException ex) + { + Log.Warning($"Было введено не число"); + MessageBox.Show("Введите число"); } } @@ -165,15 +184,18 @@ namespace ProjectAirFighter form.Show(); Action? airplaneDelegate = new((m) => { - bool q = (obj + m); - if (q) + try { + bool q = obj + m; MessageBox.Show("Объект добавлен"); + Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); pictureBoxCollection.Image = obj.ShowAirplanes(); } - else + catch (StorageOverflowException ex) { MessageBox.Show("Не удалось добавить объект"); + Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена"); + MessageBox.Show(ex.Message); } }); form.AddEvent(airplaneDelegate); @@ -191,12 +213,14 @@ _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplane { return; } - if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(listBoxStorages.SelectedItem.ToString() - ?? string.Empty); + string name = listBoxStorages.SelectedItem.ToString() + ?? string.Empty; + _storage.DelSet(name); ReloadObjects(); + Log.Information($"Удален набор: {name}"); } } diff --git a/AirFighter/Program.cs b/AirFighter/Program.cs index 7511a58..1248868 100644 --- a/AirFighter/Program.cs +++ b/AirFighter/Program.cs @@ -1,5 +1,11 @@ using System.Drawing; - +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using System.Drawing; +using Serilog; +using Serilog.Events; +using Serilog.Formatting.Json; namespace ProjectAirFighter { internal static class Program @@ -7,8 +13,13 @@ namespace ProjectAirFighter [STAThread] static void Main() { - - ApplicationConfiguration.Initialize(); + Log.Logger = new LoggerConfiguration() + .WriteTo.File("log.txt") + .MinimumLevel.Debug() + .CreateLogger(); + Application.SetHighDpiMode(HighDpiMode.SystemAware); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormAirplaneCollection()); } } diff --git a/AirFighter/SetGeneric.cs b/AirFighter/SetGeneric.cs index 5c2063d..da972ac 100644 --- a/AirFighter/SetGeneric.cs +++ b/AirFighter/SetGeneric.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectAirFighter.Exceptions; +using System.Windows.Forms; namespace ProjectAirFighter.Generics { @@ -23,7 +25,7 @@ namespace ProjectAirFighter.Generics public bool Insert(T airplane) { if (_places.Count == _maxCount) - return false; + throw new StorageOverflowException(_maxCount); Insert(airplane, 0); return true; @@ -31,7 +33,9 @@ namespace ProjectAirFighter.Generics public bool Insert(T airplane, int position) { - if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + if (!(position >= 0 && position <= Count)) return false; _places.Insert(position, airplane); return true; @@ -39,7 +43,7 @@ namespace ProjectAirFighter.Generics public bool Remove(int position) { if (!(position >= 0 && position < Count)) - return false; + throw new AirplaneNotFoundException(position); _places.RemoveAt(position); return true; } diff --git a/AirFighter/StorageOverflowException.cs b/AirFighter/StorageOverflowException.cs new file mode 100644 index 0000000..3135cfd --- /dev/null +++ b/AirFighter/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace ProjectAirFighter.Exceptions +{ + [Serializable] + internal class StorageOverflowException : ApplicationException + { + public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { } + public StorageOverflowException() : base() { } + public StorageOverflowException(string message) : base(message) { } + public StorageOverflowException(string message, Exception exception) : base(message, exception) { } + protected StorageOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} \ No newline at end of file -- 2.25.1