лабораторная 7

This commit is contained in:
Daria 2024-05-21 15:18:19 +04:00
parent 81703aa1f3
commit 22f080fa4f
14 changed files with 330 additions and 99 deletions

4
Excavator/.editorconfig Normal file
View File

@ -0,0 +1,4 @@
[*.cs]
# IDE0058: Значение выражения никогда не используется
csharp_style_unused_value_expression_statement_preference = discard_variable

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188 VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Excavator", "Excavator\Excavator.csproj", "{DD2A9FE6-2F74-4387-9318-59AE715EBA73}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Excavator", "Excavator\Excavator.csproj", "{DD2A9FE6-2F74-4387-9318-59AE715EBA73}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,4 +1,5 @@
using Excavator.Drawnings; using Excavator.Drawnings;
using Excavator.Exceptions;
namespace Excavator.CollectionGenericObjects; namespace Excavator.CollectionGenericObjects;
@ -95,14 +96,19 @@ public abstract class AbstractCompany
Bitmap bitmap = new(_pictureWidth, _pictureHeight); Bitmap bitmap = new(_pictureWidth, _pictureHeight);
Graphics graphics = Graphics.FromImage(bitmap); Graphics graphics = Graphics.FromImage(bitmap);
DrawBackgound(graphics); DrawBackgound(graphics);
SetObjectsPosition(); SetObjectsPosition();
for (int i = 0; i < (_collection?.Count ?? 0); ++i) for (int i = 0; i < (_collection?.Count ?? 0); ++i)
{
try
{ {
DrawningTrackedVehicle? obj = _collection?.Get(i); DrawningTrackedVehicle? obj = _collection?.Get(i);
obj?.DrawTransport(graphics); obj?.DrawTransport(graphics);
} }
catch (ObjectNotFoundException)
{
continue;
}
}
return bitmap; return bitmap;
} }

View File

@ -1,4 +1,5 @@
using Excavator.Drawnings; using Excavator.Drawnings;
using Excavator.Exceptions;
namespace Excavator.CollectionGenericObjects; namespace Excavator.CollectionGenericObjects;
@ -36,16 +37,31 @@ public class Garage(int picWidth, int picHeight, ICollectionGenericObjects<Drawn
{ {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
int curWidth = width - 1;
int curHeight = 0;
for (int i = 0; i < (_collection?.Count ?? 0); i++)
{ {
DrawningTrackedVehicle? excavator = _collection.Get(i); try
if (excavator == null) {
continue; _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
int r = i / width; _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 10, curHeight * _placeSizeHeight + 10);
int s = width - 1 - (i % width); }
excavator.SetPictureSize(_pictureWidth, _pictureHeight); catch (ObjectNotFoundException) { }
excavator.SetPosition(s * _placeSizeWidth + t, (r * _placeSizeHeight * 2)+ t); catch (PositionOutOfCollectionException e) { }
excavator.DrawTransport(g); if (curWidth > 0)
curWidth--;
else
{
curWidth = width - 1;
curHeight++;
}
if (curHeight >= height)
{
return;
}
} }
} }
} }

View File

@ -1,4 +1,6 @@
 using Excavator.Exceptions;
namespace Excavator.CollectionGenericObjects; namespace Excavator.CollectionGenericObjects;
/// <summary> /// <summary>
/// Параметризованный набор объектов /// Параметризованный набор объектов
@ -33,31 +35,51 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
public T? Get(int position) public T? Get(int position)
{ {
if (position >= Count || position < 0) return null; if (position < 0 || position >= Count)
{
throw new PositionOutOfCollectionException(position);
}
return _collection[position]; return _collection[position];
} }
public int Insert(T obj) public int Insert(T obj)
{ {
if (Count + 1 > _maxCount) return -1; if (Count == _maxCount)
{
throw new CollectionOverflowException(Count);
}
_collection.Add(obj); _collection.Add(obj);
return Count; return _collection.Count;
} }
public int Insert(T obj, int position) public int Insert(T obj, int position)
{ {
if (Count + 1 > _maxCount) return -1;
if (position < 0 || position > Count) return -1; if (position < 0 || position > Count)
_collection.Insert(position, obj); {
return 1; throw new PositionOutOfCollectionException(position);
}
if (Count == _maxCount)
{
throw new CollectionOverflowException(Count);
} }
_collection.Insert(position, obj);
return position;
}
public T? Remove(int position) public T? Remove(int position)
{ {
if (position < 0 || position > Count) return null;
T? pos = _collection[position]; if (position < 0 || position > Count)
{
throw new PositionOutOfCollectionException(position);
}
T? obj = _collection[position];
_collection.RemoveAt(position); _collection.RemoveAt(position);
return pos; return obj;
} }
public IEnumerable<T?> GetItems() public IEnumerable<T?> GetItems()

View File

@ -1,4 +1,6 @@
 
using Excavator.Exceptions;
namespace Excavator.CollectionGenericObjects; namespace Excavator.CollectionGenericObjects;
/// <summary> /// <summary>
@ -49,53 +51,79 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
public T? Get(int position) public T? Get(int position)
{ {
if (position < 0 || position > Count) if (position >= _collection.Length || position < 0)
{ {
return null; throw new PositionOutOfCollectionException(position);
}
if (_collection[position] == null)
{
throw new ObjectNotFoundException(position);
} }
return _collection[position]; return _collection[position];
} }
public int Insert(T excavator) public int Insert(T excavator)
{ {
return Insert(excavator, 0); for (int i = 0; i < Count; i++)
}
public int Insert(T excavator, int position)
{
int nullIndex = -1, i;
if (!(position >= 0 && position < Count))
return -1;
for (i = position; i < Count; i++)
{ {
if (_collection[i] == null) if (_collection[i] == null)
{ {
nullIndex = i; _collection[i] = excavator;
break; return i;
} }
} }
if (nullIndex < 0)
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position)
{ {
return -1;
} if (position < 0 || position >= Count)
for (i = nullIndex; i > position; i--)
{ {
_collection[i] = _collection[i - 1]; throw new PositionOutOfCollectionException(position);
} }
_collection[position] = excavator;
if (_collection[position] == null)
{
_collection[position] = obj;
return position; return position;
} }
public T Remove(int position) for (int i = position + 1; i < Count; i++)
{ {
if (position < 0 || position > Count) if (_collection[i] == null)
{ {
return null; _collection[i] = obj;
return i;
} }
T drawningTrackedVehicle = _collection[position]; }
for (int i = position - 1; i >= 0; i--)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
throw new CollectionOverflowException(Count);
}
public T? Remove(int position)
{
if (position < 0 || position >= Count)
{
throw new PositionOutOfCollectionException(position);
}
if (_collection[position] == null)
{
throw new ObjectNotFoundException(position);
}
T? obj = _collection[position];
_collection[position] = null; _collection[position] = null;
return obj;
return drawningTrackedVehicle;
} }
public IEnumerable<T?> GetItems() public IEnumerable<T?> GetItems()

View File

@ -1,5 +1,7 @@
using Excavator.Drawnings; using Excavator.Drawnings;
using Excavator.Exceptions;
using System.Text; using System.Text;
using static System.Windows.Forms.LinkLabel;
namespace Excavator.CollectionGenericObjects; namespace Excavator.CollectionGenericObjects;
/// <summary> /// <summary>
@ -88,12 +90,11 @@ public class StorageCollection<T>
/// Сохранение информации по автомобилям в хранилище в файл /// Сохранение информации по автомобилям в хранилище в файл
/// </summary> /// </summary>
/// <param name="filename">Путь и имя файла</param> /// <param name="filename">Путь и имя файла</param>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns> public void SaveData(string filename)
public bool SaveData(string filename)
{ {
if (_storages.Count == 0) if (_storages.Count == 0)
{ {
return false; throw new NullReferenceException("В хранилище отсутствуют коллекции для сохранения");
} }
if (File.Exists(filename)) if (File.Exists(filename))
@ -134,18 +135,16 @@ public class StorageCollection<T>
} }
} }
return true;
} }
/// <summary> /// <summary>
/// Загрузка информации по автомобилям в хранилище из файла /// Загрузка информации по автомобилям в хранилище из файла
/// </summary> /// </summary>
/// <param name="filename">Путь и имя файла</param> /// <param name="filename">Путь и имя файла</param>
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns> public void LoadData(string filename)
public bool LoadData(string filename)
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
return false; throw new FileNotFoundException("Файл не существует");
} }
using (StreamReader reader = File.OpenText(filename)) using (StreamReader reader = File.OpenText(filename))
@ -153,11 +152,11 @@ public class StorageCollection<T>
string? str = reader.ReadLine(); string? str = reader.ReadLine();
if (str == null || str.Length == 0) if (str == null || str.Length == 0)
{ {
return false; throw new FileFormatException("В файле нет данных");
} }
if (!str.StartsWith(_collectionKey)) if (!str.Equals(_collectionKey))
{ {
return false; throw new FileFormatException("В файле неверные данные");
} }
_storages.Clear(); _storages.Clear();
string? strs = ""; string? strs = "";
@ -175,25 +174,31 @@ public class StorageCollection<T>
if (collection == null) if (collection == null)
{ {
return false; throw new InvalidOperationException("Не удалось создать коллекцию");
} }
collection.MaxCount = Convert.ToInt32(record[2]); collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{ {
if (elem?.CreateDrawningTrackedVehicle() is T car) if (elem?.CreateDrawningTrackedVehicle() is T ship)
{ {
if (collection.Insert(car) == -1) try
{ {
return false; if (collection.Insert(ship) == -1)
{
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
}
}
catch (CollectionOverflowException ex)
{
throw new OverflowException("Коллекция переполнена", ex);
} }
} }
} }
_storages.Add(record[0], collection); _storages.Add(record[0], collection);
} }
return true;
} }
} }
/// <summary> /// <summary>

View File

@ -8,6 +8,29 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<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.CommandLine" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.11" />
<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" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageReference Include="Walter.Web.FireWall.SqlLogger" Version="2023.11.13.1117" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="Properties\Resources.Designer.cs"> <Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
@ -23,4 +46,10 @@
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="serilogConfig.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,20 @@
using System.Runtime.Serialization;
namespace Excavator.Exceptions;
/// <summary>
/// Класс, описывающий ошибку переполнения коллекции
/// </summary>
[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) { }
}

View File

@ -0,0 +1,20 @@
using System.Runtime.Serialization;
namespace Excavator.Exceptions;
/// <summary>
/// Класс, описывающий ошибку, что по указанной позиции нет элемента
/// </summary>
[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) { }
}

View File

@ -0,0 +1,15 @@
using System.Runtime.Serialization;
namespace Excavator.Exceptions;
/// <summary>
/// Класс, описывающий ошибку выхода за границы коллекции
/// </summary>
[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) { }
}

View File

@ -1,5 +1,7 @@
using Excavator.CollectionGenericObjects; using Excavator.CollectionGenericObjects;
using Excavator.Drawnings; using Excavator.Drawnings;
using Excavator.Exceptions;
using Microsoft.Extensions.Logging;
namespace Excavator; namespace Excavator;
@ -17,13 +19,19 @@ public partial class FormTrackedVehicleCollection : Form
/// </summary> /// </summary>
private AbstractCompany? _company = null; private AbstractCompany? _company = null;
/// <summary>
/// Логер
/// </summary>
private readonly ILogger _logger;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormTrackedVehicleCollection() public FormTrackedVehicleCollection(ILogger<FormTrackedVehicleCollection> logger)
{ {
InitializeComponent(); InitializeComponent();
_storageCollection = new(); _storageCollection = new();
_logger = logger;
} }
@ -45,7 +53,6 @@ public partial class FormTrackedVehicleCollection : Form
private void ButtonAddCar_Click(object sender, EventArgs e) private void ButtonAddCar_Click(object sender, EventArgs e)
{ {
FormTrackedVehicleConfig form = new(); FormTrackedVehicleConfig form = new();
form.Show(); form.Show();
form.AddEvent(SetCar); form.AddEvent(SetCar);
} }
@ -55,6 +62,8 @@ public partial class FormTrackedVehicleCollection : Form
/// </summary> /// </summary>
/// <param name="car"></param> /// <param name="car"></param>
private void SetCar(DrawningTrackedVehicle? car) private void SetCar(DrawningTrackedVehicle? car)
{
try
{ {
if (_company == null || car == null) if (_company == null || car == null)
{ {
@ -64,11 +73,14 @@ public partial class FormTrackedVehicleCollection : Form
if (_company + car != -1) if (_company + car != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
_logger.LogInformation($"Добавлен объект {car.GetDataForSave()}");
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
} }
else }
catch (CollectionOverflowException ex)
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show(ex.Message);
_logger.LogWarning($"Ошибка: {ex.Message}");
} }
} }
@ -89,15 +101,25 @@ public partial class FormTrackedVehicleCollection : Form
return; return;
} }
try
{
int pos = Convert.ToInt32(maskedTextBox.Text); int pos = Convert.ToInt32(maskedTextBox.Text);
if (_company - pos != null) if (_company - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
_logger.LogInformation($"Удален объект по позиции {pos}");
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
} }
else }
catch (ObjectNotFoundException ex)
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show(ex.Message);
_logger.LogError($"Ошибка: {ex.Message}");
}
catch (PositionOutOfCollectionException ex)
{
MessageBox.Show(ex.Message);
_logger.LogError($"Ошибка: {ex.Message}");
} }
} }
@ -253,18 +275,18 @@ public partial class FormTrackedVehicleCollection : Form
{ {
if (saveFileDialog.ShowDialog() == DialogResult.OK) if (saveFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_storageCollection.SaveData(saveFileDialog.FileName)) try
{ {
MessageBox.Show("Сохранение прошло успешно", _storageCollection.SaveData(saveFileDialog.FileName);
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
} }
else catch (Exception ex)
{ {
MessageBox.Show("Не сохранилось", "Результат", MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError("Ошибка: {Message}", ex.Message);
} }
} }
} }
/// <summary> /// <summary>
/// Обработка нажатия "Загрузка" /// Обработка нажатия "Загрузка"
@ -275,16 +297,17 @@ public partial class FormTrackedVehicleCollection : Form
{ {
if (openFileDialog.ShowDialog() == DialogResult.OK) if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_storageCollection.LoadData(openFileDialog.FileName)) try
{ {
MessageBox.Show("Загрузка прошла успешно", _storageCollection.LoadData(openFileDialog.FileName);
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
RerfreshListBoxItems(); RerfreshListBoxItems();
_logger.LogInformation("Загрузка из фала: {filename}", openFileDialog.FileName);
} }
else catch (Exception ex)
{ {
MessageBox.Show("Не загрузилось", "Результат", MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError("Ошибка: {Message}", ex.Message);
} }
} }
} }

View File

@ -1,3 +1,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace Excavator namespace Excavator
{ {
internal static class Program internal static class Program
@ -8,10 +13,28 @@ namespace Excavator
[STAThread] [STAThread]
static void Main() static void Main()
{ {
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormTrackedVehicleCollection());
ServiceCollection services = new();
ConfigureServices(services);
using ServiceProvider serviceProvider = services.BuildServiceProvider();
Application.Run(serviceProvider.GetRequiredService<FormTrackedVehicleCollection>());
}
private static void ConfigureServices(ServiceCollection services)
{
services
.AddSingleton<FormTrackedVehicleCollection>()
.AddLogging(option =>
{
option.SetMinimumLevel(LogLevel.Information);
var config = new ConfigurationBuilder()
.AddJsonFile("serilogConfig.json", optional: false, reloadOnChange: true)
.Build();
option.AddSerilog(Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger());
});
} }
} }
} }

View File

@ -0,0 +1,20 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/log_.log",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "Excavator"
}
}
}