Лабораторная работа №8

This commit is contained in:
victinass 2024-05-21 17:11:52 +04:00
parent 45a9db6f9c
commit b24c1358e2
9 changed files with 60 additions and 20 deletions

View File

@ -9,8 +9,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.9" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -26,7 +26,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="obj">Добавляемый объект</param>
/// <param name="comparer">Сравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, IEqualityComparer<DrawingWarship?> comparer = null);
int Insert(T obj, IEqualityComparer<T?> comparer = null);
/// <summary>
/// Добавление объекта в коллекцию на конкретную позицию
@ -35,7 +35,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="position">Позиция</param>
/// <param name="comparer">Сравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, int position, IEqualityComparer<DrawingWarship?> comparer = null);
int Insert(T obj, int position, IEqualityComparer<T?> comparer = null);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции

View File

@ -39,10 +39,10 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position];
}
public int Insert(T obj, IEqualityComparer<DrawingWarship?>? comparer = null)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если такой объект есть в коллекции
if (comparer != null && _collection.Contains(obj))
if (comparer != null && _collection.Contains(obj, comparer))
{
throw new CollectionAlreadyExistsException();
}
@ -56,10 +56,10 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _maxCount;
}
public int Insert(T obj, int position, IEqualityComparer<DrawingWarship?>? comparer = null)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если такой объект есть в коллекции
if (comparer != null && _collection.Contains(obj))
if (comparer != null && _collection.Contains(obj, comparer))
{
throw new CollectionAlreadyExistsException();
}

View File

@ -61,10 +61,10 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position];
}
public int Insert(T obj, IEqualityComparer<DrawingWarship?>? comparer = null)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если такой объект есть в коллекции
if (comparer != null && _collection.Contains(obj))
if (comparer != null && _collection.Contains(obj, comparer))
{
throw new CollectionAlreadyExistsException();
}
@ -82,11 +82,11 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position, IEqualityComparer<DrawingWarship?>? comparer = null)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если такой объект есть в коллекции
if (comparer != null && _collection.Contains(obj))
if (comparer != null && _collection.Contains(obj, comparer))
{
throw new CollectionAlreadyExistsException();
}

View File

@ -34,6 +34,10 @@ public class DrawingWarshipEqutables : IEqualityComparer<DrawingWarship?>
{
return false;
}
if (x.EntityWarship.BodyColor != y.EntityWarship.BodyColor)
{
return false;
}
if (x is DrawingBattleship && y is DrawingBattleship)
{

View File

@ -1,5 +1,4 @@
using Battleship.CollectionGenericObjects;
using Battleship.CollectionGenericObjects;
using System.Runtime.Serialization;
namespace Battleship.Exceptions;
@ -11,7 +10,7 @@ namespace Battleship.Exceptions;
internal class CollectionAlreadyExistsException : ApplicationException
{
public CollectionAlreadyExistsException(CollectionInfo collectionInfo) : base("В коллекции уже есть такой элемент: " + collectionInfo) { }
public CollectionAlreadyExistsException() : base() { }
public CollectionAlreadyExistsException() : base("В коллекции уже есть такой элемент") { }
public CollectionAlreadyExistsException(string message) : base(message) { }
public CollectionAlreadyExistsException(string message, Exception exception) : base(message, exception) { }
protected CollectionAlreadyExistsException(SerializationInfo info, StreamingContext context) : base(info, context) { }

View File

@ -80,7 +80,11 @@ public partial class FormWarshipCollection : Form
MessageBox.Show(ex.Message);
_logger.LogError("Ошибка: {Message}", ex.Message);
}
catch (CollectionAlreadyExistsException ex)
{
MessageBox.Show(ex.Message);
_logger.LogError("Ошибка: {Message}", ex.Message);
}
}
/// <summary>

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Serilog;
namespace Battleship
{
@ -27,13 +28,22 @@ namespace Battleship
/// <param name="services"></param>
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormWarshipCollection>().AddLogging(option =>
string[] path = Directory.GetCurrentDirectory().Split('\\');
string pathNeed = "";
for (int i = 0; i < path.Length - 3; i++)
{
pathNeed += path[i] + "\\";
}
services.AddSingleton<FormWarshipCollection>()
.AddLogging(option =>
{
option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config");
option.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(new ConfigurationBuilder()
.AddJsonFile($"{pathNeed}serilog.json")
.Build())
.CreateLogger());
});
}
}
}

View File

@ -0,0 +1,15 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": { "path": "log.log" }
}
],
"Properties": {
"Application": "Sample"
}
}
}