From c2e139d3e34b6f2134a110d21e4fb8f494fed2a2 Mon Sep 17 00:00:00 2001 From: kirkorovka <147087189+kirkorovka@users.noreply.github.com> Date: Sun, 9 Jun 2024 02:07:07 +0400 Subject: [PATCH] Lab7 --- .../AbstractCompany.cs | 2 +- .../ListGenericObjects.cs | 39 ++++++--- .../MassiveGenericObjects.cs | 83 ++++++++++--------- .../StorageCollection.cs | 36 +++++--- .../Drawings/DrawingBase.cs | 20 ++--- .../Drawings/DrawingSAU.cs | 12 +-- .../Drawings/ExtentionDrawing.cs | 2 +- .../Entity/BaseSAU.cs | 4 +- .../Entity/EntitySAU.cs | 6 +- .../Exceptions/CollectionOverflowException.cs | 22 +++++ .../Exceptions/ObjectNotFoundException.cs | 22 +++++ .../PositionOutOfCollectionException.cs | 22 +++++ .../FormSAU.cs | 8 +- .../FormSAUCollection.cs | 73 ++++++++++------ .../FormSAUConfig.cs | 4 +- .../MovementStrategy/AbstractStrategy.cs | 28 +++---- .../MovementStrategy/IMoveableObject.cs | 2 +- .../MovementStrategy/MoveableSAU.cs | 8 +- .../MovementStrategy/MovementDirection.cs | 2 +- .../Program.cs | 21 ++++- .../Project_SelfPropelledArtilleryUnit.csproj | 30 +++++++ .../nlog.config | 15 ++++ 22 files changed, 323 insertions(+), 138 deletions(-) create mode 100644 Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/CollectionOverflowException.cs create mode 100644 Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotFoundException.cs create mode 100644 Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/PositionOutOfCollectionException.cs create mode 100644 Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/nlog.config diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/AbstractCompany.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/AbstractCompany.cs index 22a376a..f998fb9 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/AbstractCompany.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/AbstractCompany.cs @@ -19,7 +19,7 @@ public abstract class AbstractCompany protected ICollectionGenericObjects? _collection = null; - private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight) - 1; public AbstractCompany(int picWidth, int picHeigth, ICollectionGenericObjects collection) { diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs index 999f0ad..7de6a98 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs @@ -1,14 +1,15 @@ -using System; +using Project_SelfPropelledArtilleryUnit.Exceptions; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Project_SelfPropelledArtilleryUnit.CollectionGenericObjects; -public class ListGenericObjects : ICollectionGenericObjects +public class ListGenericObjects : ICollectionGenericObjects where T : class { - private readonly List _collection; + private readonly List _collection; private int _maxCount; public int MaxCount @@ -24,7 +25,7 @@ public class ListGenericObjects : ICollectionGenericObjects } public int Count => _collection.Count; - + public CollectionType GetCollectionType => CollectionType.List; @@ -35,30 +36,46 @@ public class ListGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position < 0 || position >= _collection.Count || _collection == null || _collection.Count == 0) return null; + if (position < 0 || position >= Count) + { + throw new PositionOutOfCollectionException(position); + } return _collection[position]; } public int Insert(T obj) { - if (Count == _maxCount) return -1; + if (Count == _maxCount) + { + throw new CollectionOverflowException(Count); + } _collection.Add(obj); - return _collection.Count; + return Count; } public int Insert(T obj, int position) { - if (Count == _maxCount || position < 0 || position > Count) return -1; + if (Count == _maxCount) + { + throw new CollectionOverflowException(Count); + } + if (position >= Count || position < 0) + { + throw new CollectionOverflowException(Count); + } _collection.Insert(position, obj); return position; } public T? Remove(int position) { - if (_collection == null || position < 0 || position >= _collection.Count) return null; - T? obj = _collection[position]; - _collection[position] = null; + if (position >= Count || position < 0) + { + throw new PositionOutOfCollectionException(position); + } + T obj = _collection[position]; + _collection.RemoveAt(position); return obj; } diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs index a7e7cc7..4df3bdb 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ using Project_SelfPropelledArtilleryUnit.Drawings; +using Project_SelfPropelledArtilleryUnit.Exceptions; using System; using System.Collections.Generic; using System.Linq; @@ -7,7 +8,7 @@ using System.Threading.Tasks; namespace Project_SelfPropelledArtilleryUnit.CollectionGenericObjects; -public class MassiveGenericObjects : ICollectionGenericObjects +public class MassiveGenericObjects : ICollectionGenericObjects where T : class { private T?[] _collection; @@ -43,77 +44,79 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { - - if (position >= _collection.Length || position < 0) + if (position < 0 || position >= Count) { - return null; + throw new PositionOutOfCollectionException(position); } + return _collection[position]; } public int Insert(T obj) { - - int index = 0; - while (index < _collection.Length) + for (int i = 0; i < Count; i++) { - if (_collection[index] == null) + if (_collection[i] == null) { - _collection[index] = obj; - return index; + _collection[i] = obj; + return i; } - index++; } - return -1; + + throw new CollectionOverflowException(Count); } public int Insert(T obj, int position) { - - if (position >= _collection.Length || position < 0) - return -1; - - - if (_collection[position] != null) + if (position < 0 || position >= Count) + { + throw new PositionOutOfCollectionException(position); + } + + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + else { - - int nullIndex = -1; for (int i = position + 1; i < Count; i++) { if (_collection[i] == null) { - nullIndex = i; - break; + _collection[i] = obj; + return i; } } - - if (nullIndex < 0) + + for (int i = 0; i < position; i++) { - return -1; - } - - int j = nullIndex - 1; - while (j >= position) - { - _collection[j + 1] = _collection[j]; - j--; + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } } } - - _collection[position] = obj; - return position; + + throw new CollectionOverflowException(Count); } public T? Remove(int position) { - - if (position >= _collection.Length || position < 0) + + if (position >= Count || position < 0) { - return null; + throw new PositionOutOfCollectionException(position); + } + + T? obj = _collection[position]; + if (obj == null) + { + throw new ObjectNotFoundException(position); } - T temp = _collection[position]; _collection[position] = null; - return temp; + return obj; } public IEnumerable GetItems() diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs index 43c5d30..5618128 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,5 @@ using Project_SelfPropelledArtilleryUnit.Drawings; +using Project_SelfPropelledArtilleryUnit.Exceptions; using System; using System.Collections.Generic; using System.Linq; @@ -15,9 +16,9 @@ internal class StorageCollection public List Keys => _storages.Keys.ToList(); private readonly string _collectionKey = "CollectionsStorage"; - + private readonly string _separatorForKeyValue = "|"; - + private readonly string _separatorItems = ";"; public StorageCollection() @@ -55,11 +56,11 @@ internal class StorageCollection } } - public bool SaveData(string filename) + public void SaveData(string filename) { if (_storages.Count == 0) { - return false; + throw new Exception("В хранилище отсутствуют коллекции для сохранения"); } if (File.Exists(filename)) @@ -97,25 +98,25 @@ internal class StorageCollection } } } - return true; } - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new FileNotFoundException("Файл не существует"); } using (StreamReader reader = new(filename)) { string line = reader.ReadLine(); if (line == null || line.Length == 0) { - return false; + throw new IOException("В файле нет данных"); } if (!line.Equals(_collectionKey)) { - return false; + throw new IOException("В файле неверные данные"); + } _storages.Clear(); while ((line = reader.ReadLine()) != null) @@ -129,7 +130,7 @@ internal class StorageCollection ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); if (collection == null) { - return false; + throw new IOException("В файле нет данных"); } collection.MaxCount = Convert.ToInt32(record[2]); string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); @@ -139,17 +140,26 @@ internal class StorageCollection { if (collection.Insert(truck) == -1) { - return false; + try + { + if (collection.Insert(truck) == -1) + { + throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); + } + } + catch (CollectionOverflowException ex) + { + throw new Exception("Коллекция переполнена", ex); + } } } } _storages.Add(record[0], collection); } } - return true; } - + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) { diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingBase.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingBase.cs index 37bf8d9..4d59687 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingBase.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingBase.cs @@ -18,7 +18,7 @@ namespace Project_SelfPropelledArtilleryUnit.Drawings private readonly int _drawingSAUWidth = 150; private readonly int _drawingSAUHeight = 80; - + public int? GetPosX => _startPosX; public int? GetPosY => _startPosY; @@ -29,18 +29,18 @@ namespace Project_SelfPropelledArtilleryUnit.Drawings public DrawingBase(int speed, double weight, Color bodyColor) : this() { - BaseSAU =new BaseSAU(speed, weight, bodyColor); + BaseSAU = new BaseSAU(speed, weight, bodyColor); } public DrawingBase() { - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; } - public DrawingBase(int width,int height) : this() + public DrawingBase(int width, int height) : this() { _drawingSAUWidth = width; _drawingSAUHeight = height; @@ -129,7 +129,7 @@ namespace Project_SelfPropelledArtilleryUnit.Drawings if (_startPosY.Value - BaseSAU.Step > 0) { _startPosY -= (int)BaseSAU.Step; - } + } return true; case DirectionType.Right: if (_startPosX.Value + BaseSAU.Step + _drawingSAUWidth < _pictureWidth.Value) @@ -155,7 +155,7 @@ namespace Project_SelfPropelledArtilleryUnit.Drawings return; } - + Pen pen = new(Color.Black); Brush BaseBrush = new SolidBrush(BaseSAU.BodyColor); Brush WheelBrush = new SolidBrush(Color.Brown); @@ -186,7 +186,7 @@ namespace Project_SelfPropelledArtilleryUnit.Drawings Point point8 = new Point(_startPosX.Value + 150, _startPosY.Value + 20); Point point9 = new Point(_startPosX.Value + 100, _startPosY.Value + 35); Point[] Points2 = { point6, point7, point8, point9 }; - g.FillPolygon(BaseBrush, Points2); + g.FillPolygon(BaseBrush, Points2); Point point10 = new Point(_startPosX.Value + 140, _startPosY.Value + 10); Point point11 = new Point(_startPosX.Value + 145, _startPosY.Value + 5); diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAU.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAU.cs index 2fa55d4..b1d573c 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAU.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/DrawingSAU.cs @@ -10,12 +10,12 @@ namespace Project_SelfPropelledArtilleryUnit.Drawings; public class DrawingSAU : DrawingBase { - + public DrawingSAU(int speed, double weight, Color bodyColor, Color additionalColor, bool tracks, bool guns) : base(150, 100) { - BaseSAU = new EntitySAU( speed, weight, bodyColor, additionalColor, tracks, guns); - + BaseSAU = new EntitySAU(speed, weight, bodyColor, additionalColor, tracks, guns); + } public DrawingSAU(BaseSAU? baseSAU) { @@ -27,7 +27,7 @@ public class DrawingSAU : DrawingBase public override void DrawSAU(Graphics g) { - if (BaseSAU==null || BaseSAU is not EntitySAU SAU || !_startPosX.HasValue || !_startPosY.HasValue) + if (BaseSAU == null || BaseSAU is not EntitySAU SAU || !_startPosX.HasValue || !_startPosY.HasValue) { return; } @@ -55,7 +55,7 @@ public class DrawingSAU : DrawingBase } } - - + + } diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/ExtentionDrawing.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/ExtentionDrawing.cs index bc8c2a0..f8a89eb 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/ExtentionDrawing.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Drawings/ExtentionDrawing.cs @@ -11,7 +11,7 @@ public static class ExtentionDrawing { private static readonly string _separatorForObject = ":"; - + public static DrawingBase? CreateDrawningBase(this string info) { string[] strs = info.Split(_separatorForObject); diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/BaseSAU.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/BaseSAU.cs index 79d566c..64483eb 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/BaseSAU.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/BaseSAU.cs @@ -12,7 +12,7 @@ namespace Project_SelfPropelledArtilleryUnit.Entity public double Weight { get; private set; } public Color BodyColor { get; private set; } - + public double Step => Speed * 100 / Weight; public BaseSAU(int speed, double weight, Color bodyColor) @@ -20,7 +20,7 @@ namespace Project_SelfPropelledArtilleryUnit.Entity Speed = speed; Weight = weight; BodyColor = bodyColor; - + } public void SetBodyColor(Color bodyColor) { diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/EntitySAU.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/EntitySAU.cs index 295b74b..9f21964 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/EntitySAU.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Entity/EntitySAU.cs @@ -6,13 +6,13 @@ using System.Threading.Tasks; namespace Project_SelfPropelledArtilleryUnit.Entity; -public class EntitySAU: BaseSAU +public class EntitySAU : BaseSAU { - + public Color AdditionalColor { get; private set; } public bool Tracks { get; private set; } - + public bool Guns { get; private set; } public EntitySAU(int speed, double weight, Color bodyColor, Color additionalColor, bool tracks, bool guns) : base(speed, weight, bodyColor) diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/CollectionOverflowException.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/CollectionOverflowException.cs new file mode 100644 index 0000000..69457c9 --- /dev/null +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/CollectionOverflowException.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Project_SelfPropelledArtilleryUnit.Exceptions; +[Serializable] + +internal class CollectionOverflowException : ApplicationException +{ + public CollectionOverflowException(int count) : base("В коллекции превышено допустимое количество: " + count) { } + + public CollectionOverflowException() : base() { } + + public CollectionOverflowException(string message) : base(message) { } + + public CollectionOverflowException(string message, Exception exception) : base(message, exception) { } + + protected CollectionOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotFoundException.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotFoundException.cs new file mode 100644 index 0000000..4306b13 --- /dev/null +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/ObjectNotFoundException.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Project_SelfPropelledArtilleryUnit.Exceptions; +[Serializable] + +internal class ObjectNotFoundException : ApplicationException +{ + public ObjectNotFoundException(int i) : base("Не найден объект по позиции " + i) { } + + public ObjectNotFoundException() : base() { } + + public ObjectNotFoundException(string message) : base(message) { } + + public ObjectNotFoundException(string message, Exception exception) : base(message, exception) { } + + protected ObjectNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/PositionOutOfCollectionException.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/PositionOutOfCollectionException.cs new file mode 100644 index 0000000..dfbd45f --- /dev/null +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Exceptions/PositionOutOfCollectionException.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Project_SelfPropelledArtilleryUnit.Exceptions; +[Serializable] + +internal class PositionOutOfCollectionException : ApplicationException +{ + public PositionOutOfCollectionException(int i) : base("Выход за границы коллекции. Позиция " + i) { } + + public PositionOutOfCollectionException() : base() { } + + public PositionOutOfCollectionException(string message) : base(message) { } + + public PositionOutOfCollectionException(string message, Exception exception) : base(message, exception) { } + + protected PositionOutOfCollectionException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAU.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAU.cs index d2d26e4..d8a856a 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAU.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAU.cs @@ -48,7 +48,7 @@ public partial class FormSAU : Form pictureBox1.Image = bmp; } - + private void Move_Click(object sender, EventArgs e) { @@ -81,7 +81,7 @@ public partial class FormSAU : Form } } - + private void strategyStep_Click(object sender, EventArgs e) { @@ -99,8 +99,8 @@ public partial class FormSAU : Form 1 => new MoveToBorder(), _ => null, }; - - + + if (_strategy == null) { return; diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs index bdabed9..9e79d51 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUCollection.cs @@ -1,5 +1,9 @@ -using Project_SelfPropelledArtilleryUnit.CollectionGenericObjects; +using Microsoft.Extensions.Logging; +using Microsoft.VisualBasic.Logging; +using NLog; +using Project_SelfPropelledArtilleryUnit.CollectionGenericObjects; using Project_SelfPropelledArtilleryUnit.Drawings; +using Project_SelfPropelledArtilleryUnit.Exceptions; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,6 +14,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Project_SelfPropelledArtilleryUnit; @@ -17,10 +22,13 @@ public partial class FormSAUCollection : Form { private readonly StorageCollection _storageCollection; private AbstractCompany? _company = null; - public FormSAUCollection() + private readonly ILogger _logger; + public FormSAUCollection(ILogger logger) { InitializeComponent(); _storageCollection = new(); + _logger = logger; + _logger.LogInformation("Форма создалась"); } @@ -40,21 +48,27 @@ public partial class FormSAUCollection : Form form.AddEvent(SetSAU); } - private void SetSAU(DrawingBase sau) + private void SetSAU(DrawingBase? sau) { - if (_company == null || sau == null) + try { - return; - } + if (_company == null || sau == null) + { + return; + } - if (_company + sau != -1) - { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _company.Show(); + if (_company + sau != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + _logger.LogInformation("Добавлен объект: " + sau.GetDataForSave()); + } } - else + catch (ObjectNotFoundException ex) { } + catch (CollectionOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -71,14 +85,19 @@ public partial class FormSAUCollection : Form } int pos = Convert.ToInt32(maskedTextBox.Text); - if (_company - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBox.Image = _company.Show(); + if (_company - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + _logger.LogInformation("Удален объект по позиции " + pos); + } } - else + catch (Exception ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -211,13 +230,16 @@ public partial class FormSAUCollection : Form { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storageCollection.SaveData(saveFileDialog.FileName)) + try { + _storageCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName); } - else + catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } } @@ -226,15 +248,18 @@ public partial class FormSAUCollection : Form { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storageCollection.LoadData(openFileDialog.FileName)) + try { - MessageBox.Show("Загрузка прошла успешно", "Реузльтат", MessageBoxButtons.OK, MessageBoxIcon.Information); - UpdateListBoxItems(); + _storageCollection.LoadData(openFileDialog.FileName); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName); } - else + catch (Exception ex) { MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } + UpdateListBoxItems(); } } \ No newline at end of file diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUConfig.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUConfig.cs index 6d291aa..b9be8d6 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUConfig.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/FormSAUConfig.cs @@ -15,7 +15,7 @@ namespace Project_SelfPropelledArtilleryUnit; public partial class FormSAUConfig : Form { private DrawingBase? _drawingBase; - + private event Action? SauDelegate; public FormSAUConfig() { @@ -31,7 +31,7 @@ public partial class FormSAUConfig : Form panelPurple.MouseDown += panel_MouseDown; buttonCancel.Click += (sender, e) => Close(); - + } public void AddEvent(Action sauDelegate) diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.cs index 2b8ef24..a1ca39f 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.cs @@ -8,22 +8,22 @@ namespace Project_SelfPropelledArtilleryUnit.MovementStrategy; public abstract class AbstractStrategy { - + private IMoveableObject? _moveableObject; - + private StrategyStatus _state = StrategyStatus.NotInit; - + protected int FieldWidth { get; private set; } - + protected int FieldHeight { get; private set; } - + public StrategyStatus GetStatus() { return _state; } - + public void SetData(IMoveableObject moveableObject, int width, int height) { if (moveableObject == null) @@ -54,22 +54,22 @@ public abstract class AbstractStrategy MoveToTarget(); } - + protected bool MoveLeft() => MoveTo(MovementDirection.Left); - + protected bool MoveRight() => MoveTo(MovementDirection.Right); - + protected bool MoveUp() => MoveTo(MovementDirection.Up); - + protected bool MoveDown() => MoveTo(MovementDirection.Down); - + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; - + protected int? GetStep() { if (_state != StrategyStatus.InProgress) @@ -81,10 +81,10 @@ public abstract class AbstractStrategy protected abstract void MoveToTarget(); - + protected abstract bool IsTargetDestination(); - + private bool MoveTo(MovementDirection movementDirection) { if (_state != StrategyStatus.InProgress) diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/IMoveableObject.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/IMoveableObject.cs index f0ff3a3..4c7c4cb 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/IMoveableObject.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/IMoveableObject.cs @@ -9,7 +9,7 @@ namespace Project_SelfPropelledArtilleryUnit.MovementStrategy; public interface IMoveableObject { - + ObjectParameters? GetObjectPosition { get; } diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MoveableSAU.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MoveableSAU.cs index 5ff3a44..0892f95 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MoveableSAU.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MoveableSAU.cs @@ -9,16 +9,16 @@ namespace Project_SelfPropelledArtilleryUnit.MovementStrategy; public class MoveableSAU : IMoveableObject { - + private readonly DrawingBase? _SAU = null; - + public MoveableSAU(DrawingBase sau) { _SAU = sau; } - + public ObjectParameters? GetObjectPosition { @@ -46,7 +46,7 @@ public class MoveableSAU : IMoveableObject return _SAU.MoveSAU(GetDirectionType(direction)); } - + private static DirectionType GetDirectionType(MovementDirection direction) { return direction switch diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MovementDirection.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MovementDirection.cs index 64b5d1a..4174df7 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MovementDirection.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/MovementStrategy/MovementDirection.cs @@ -8,7 +8,7 @@ namespace Project_SelfPropelledArtilleryUnit.MovementStrategy; public enum MovementDirection { - + Up = 1, Down = 2, Left = 3, diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Program.cs b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Program.cs index cbb9dd3..48f27d4 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Program.cs +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Program.cs @@ -1,3 +1,8 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using System; + namespace Project_SelfPropelledArtilleryUnit { internal static class Program @@ -11,7 +16,21 @@ namespace Project_SelfPropelledArtilleryUnit // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormSAUCollection()); + + ServiceCollection services = new(); + ConfigureServices(services); + using ServiceProvider serviceProvider = + services.BuildServiceProvider(); + Application.Run(serviceProvider.GetRequiredService()); + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton() + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); } } } \ No newline at end of file diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit.csproj b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit.csproj index af03d74..f6e5dc1 100644 --- a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit.csproj +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit.csproj @@ -8,6 +8,36 @@ enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + True diff --git a/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/nlog.config b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/nlog.config new file mode 100644 index 0000000..5c71e85 --- /dev/null +++ b/Project_SelfPropelledArtilleryUnit/Project_SelfPropelledArtilleryUnit/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file -- 2.25.1