From 4148c28d17a77deb1bc79533ed42664345102623 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Fri, 1 Dec 2023 10:51:44 +0300 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B5=20=D1=82=D0=BE=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=85=D0=BE=D1=87=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirplanesGenericCollection.cs | 3 +- .../AirplanesGenericStorage.cs | 9 ++- .../DrawningAirplane.cs | 1 + .../Exceptions/AirplaneNotFoundException.cs | 20 +++++++ .../Exceptions/StorageOverflowException.cs | 19 ++++++ .../ExtentionDrawningAirplane.cs | 2 + .../FormAirplanesCollection.cs | 58 +++++++++++++------ .../ProjectAirplaneWithRadar/Program.cs | 19 ++++++ .../ProjectAirplaneWithRadar/SetGeneric.cs | 13 +++-- 9 files changed, 116 insertions(+), 28 deletions(-) create mode 100644 ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/AirplaneNotFoundException.cs create mode 100644 ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/StorageOverflowException.cs diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs index a1fba9a..917d53c 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs @@ -33,8 +33,7 @@ namespace ProjectAirplaneWithRadar.Generics public static bool operator -(AirplanesGenericCollection collect, int pos) { T? obj = collect._collection[pos]; - if (obj != null && collect != null) - return collect._collection.Remove(pos); + collect._collection.Remove(pos); return false; } public U? GetU(int pos) diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs index 91c2fe2..db8ed75 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs @@ -43,7 +43,7 @@ namespace ProjectAirplaneWithRadar.Generics } if (data.Length == 0) { - return false; + throw new Exception("Невалиданя операция, нет данных для сохранения"); } string toWrite = $"AirplanesStorage{Environment.NewLine}{data}"; var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); @@ -60,7 +60,7 @@ namespace ProjectAirplaneWithRadar.Generics { if (!File.Exists(filename)) { - return false; + throw new IOException("Файл не найден"); } using (StreamReader sr = new(filename)) { @@ -68,12 +68,11 @@ namespace ProjectAirplaneWithRadar.Generics var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); if (strs == null || strs.Length == 0) { - return false; + throw new IOException("Нет данных для загрузки"); } if (!strs[0].StartsWith("AirplanesStorage")) { - //если нет такой записи, то это не те данные - return false; + throw new IOException("Неверный формат данных"); } _airplanesStorages.Clear(); do diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/DrawningAirplane.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/DrawningAirplane.cs index 9e38a76..23136e3 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/DrawningAirplane.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/DrawningAirplane.cs @@ -18,6 +18,7 @@ namespace ProjectAirplaneWithRadar.DrawningObjects protected int _startPosY; protected readonly int _airplaneWidth = 200; protected readonly int _airplaneHeight = 78; + char separator = '|'; public int GetPosX => _startPosX; public int GetPosY => _startPosY; public int GetWidth => _airplaneWidth; diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/AirplaneNotFoundException.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/AirplaneNotFoundException.cs new file mode 100644 index 0000000..f4bbaa5 --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/AirplaneNotFoundException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace ProjectAirplaneWithRadar.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/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/StorageOverflowException.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/StorageOverflowException.cs new file mode 100644 index 0000000..63ee0b0 --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.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) { } + } +} diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/ExtentionDrawningAirplane.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/ExtentionDrawningAirplane.cs index 1b014fc..5fb0760 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/ExtentionDrawningAirplane.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/ExtentionDrawningAirplane.cs @@ -5,6 +5,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.ComponentModel; +using Microsoft.VisualBasic.Logging; namespace ProjectAirplaneWithRadar { diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs index f6b043d..a3a63ae 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs @@ -1,8 +1,13 @@ using ProjectAirplaneWithRadar.DrawningObjects; using ProjectAirplaneWithRadar.MovementStrategy; using ProjectAirplaneWithRadar.Generics; +using ProjectAirplaneWithRadar.Exceptions; using System.Diagnostics.Metrics; using System.Windows.Forms; +using Microsoft.Extensions.Logging; +using System.Xml.Linq; +using Serilog; +using Microsoft.VisualBasic.Logging; namespace ProjectAirplaneWithRadar { @@ -50,15 +55,17 @@ namespace ProjectAirplaneWithRadar 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}"); pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); } - else + catch(StorageOverflowException ex) { - MessageBox.Show(" "); + Log.Warning($" {listBoxStorages.SelectedItem.ToString() ?? string.Empty} "); + MessageBox.Show(ex.Message); } }); form.AddEvent(airplaneDelegate); @@ -81,14 +88,25 @@ namespace ProjectAirplaneWithRadar 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}"); pictureBoxAirplanesCollection.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(" "); } } private void buttonUpdateCollection_Click(object sender, EventArgs e) @@ -115,7 +133,7 @@ namespace ProjectAirplaneWithRadar return; } _storage.AddSet(textBoxStorageName.Text); - ReloadObjects(); + ReloadObjects(); Log.Information($" : {textBoxStorageName.Text}"); } private void buttonRemoveObject_Click(object sender, EventArgs e) { @@ -125,9 +143,10 @@ namespace ProjectAirplaneWithRadar } 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}"); } } @@ -144,15 +163,17 @@ namespace ProjectAirplaneWithRadar { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { + _storage.SaveData(saveFileDialog.FileName); MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($" {saveFileDialog.FileName} "); } - else + catch (Exception ex) { - MessageBox.Show(" ", "", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning(" "); + MessageBox.Show($" : {ex.Message}","", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -160,19 +181,22 @@ namespace ProjectAirplaneWithRadar { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { + _storage.LoadData(openFileDialog.FileName); MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($" {openFileDialog.FileName} "); 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); } } } diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs index c60668a..c9a2e48 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs @@ -1,3 +1,18 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; +using Serilog; +using Serilog.Events; +using Serilog.Formatting.Json; +using Microsoft.VisualBasic.Logging; + namespace ProjectAirplaneWithRadar { internal static class Program @@ -8,6 +23,10 @@ namespace ProjectAirplaneWithRadar [STAThread] static void Main() { + Log.Logger = new LoggerConfiguration() + .WriteTo.File("log.txt") + .MinimumLevel.Debug() + .CreateLogger(); // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs index da5fc74..16f3f1a 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs @@ -1,4 +1,7 @@ -using System.CodeDom; +using ProjectAirplaneWithRadar.Exceptions; +using System; +using System.Windows.Forms; +using System.CodeDom; using System.ComponentModel; using System.Numerics; using System.Windows.Forms.VisualStyles; @@ -19,13 +22,15 @@ namespace ProjectAirplaneWithRadar.Generics public bool Insert(T airplane) { if (_places.Count == _maxCount) - return false; + throw new StorageOverflowException(_maxCount); Insert(airplane, 0); return true; } 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; @@ -33,7 +38,7 @@ namespace ProjectAirplaneWithRadar.Generics public bool Remove(int position) { if (!(position >= 0 && position < Count)) - return false; + throw new AirplaneNotFoundException(position); _places.RemoveAt(position); return true; }