work lab 7
This commit is contained in:
parent
9563a3d4e5
commit
926e9b1834
@ -8,6 +8,17 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.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>
|
||||
<Compile Update="Properties\Resources.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
|
@ -26,7 +26,7 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeigth;
|
||||
_collection = collection;
|
||||
_collection.MaxCount = GetMaxCount;
|
||||
_collection.MaxCount = GetMaxCount - 3;
|
||||
}
|
||||
|
||||
public static bool operator +(AbstractCompany company, DrawningBus bus)
|
||||
@ -51,7 +51,6 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
Graphics graphics = Graphics.FromImage(bitmap);
|
||||
DrawBackground(graphics);
|
||||
|
||||
|
||||
for (int i = 0; i < (_collection?.MaxCount ?? 0); i++)
|
||||
{
|
||||
DrawningBus? obj = _collection?.Get(i);
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using AccordionBus.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -36,14 +37,14 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
|
||||
public T? Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= _collection.Count || _collection == null || _collection.Count == 0) return null;
|
||||
if (position < 0 || position >= _collection.Count || _collection == null || _collection.Count == 0) throw new PozitionOutOfCollectionException(position);
|
||||
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
public bool Insert(T obj)
|
||||
{
|
||||
if (_collection == null || _collection.Count == _maxCount) return false;
|
||||
if (_collection == null || _collection.Count == _maxCount) throw new CollectionOverflowException(Count);
|
||||
|
||||
_collection.Add(obj);
|
||||
return true;
|
||||
@ -59,9 +60,8 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
|
||||
public bool Remove(int position)
|
||||
{
|
||||
if (_collection == null || position < 0 || position >= _collection.Count) return false;
|
||||
if (_collection == null || position < 0 || position >= _collection.Count) throw new PozitionOutOfCollectionException(position);
|
||||
|
||||
T? obj = _collection[position];
|
||||
_collection[position] = null;
|
||||
return true;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using AccordionBus.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
@ -39,7 +40,10 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
|
||||
public T? Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= _collection.Length) return null;
|
||||
if (position < 0 || position >= _collection.Length)
|
||||
{
|
||||
throw new PozitionOutOfCollectionException(position);
|
||||
}
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
@ -53,12 +57,12 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
throw new CollectionOverflowException(Count);
|
||||
}
|
||||
|
||||
public bool Insert(T obj, int position)
|
||||
{
|
||||
if (position < 0 || position >= _collection.Length) { return false; }
|
||||
if (position < 0 || position >= _collection.Length) throw new PozitionOutOfCollectionException(position);
|
||||
|
||||
if (_collection[position] == null)
|
||||
{
|
||||
@ -85,14 +89,14 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
throw new CollectionOverflowException(Count);
|
||||
}
|
||||
|
||||
public bool Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= _collection.Length) { return false;}
|
||||
if (position < 0 || position >= _collection.Length) throw new PozitionOutOfCollectionException(position);
|
||||
if (_collection[position] == null) throw new ObjectNotFoundException(position);
|
||||
|
||||
T? obj = _collection[position];
|
||||
_collection[position] = null;
|
||||
return true;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using AccordionBus.Drawnings;
|
||||
using AccordionBus.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -47,11 +48,11 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
}
|
||||
}
|
||||
|
||||
public bool SaveData(string filename)
|
||||
public void SaveData(string filename)
|
||||
{
|
||||
if (_storage.Count == 0)
|
||||
{
|
||||
return false;
|
||||
throw new InvalidOperationException("В хранилище отсутствуют коллекции для сохранения");
|
||||
}
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
@ -87,26 +88,25 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
}
|
||||
writer.Write(sb);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public bool LoadData(string filename)
|
||||
public void LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
throw new FileNotFoundException("Файл не существует");
|
||||
}
|
||||
using (StreamReader reader = File.OpenText(filename))
|
||||
{
|
||||
string str = reader.ReadLine();
|
||||
if (str == null || str.Length == 0)
|
||||
{
|
||||
return false;
|
||||
throw new InvalidOperationException("В файле нет данных");
|
||||
}
|
||||
if (!str.StartsWith(_collectionKey))
|
||||
{
|
||||
return false;
|
||||
throw new InvalidOperationException("В файле не верные данные");
|
||||
}
|
||||
_storage.Clear();
|
||||
string strs = "";
|
||||
@ -121,7 +121,7 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||
if (collection == null)
|
||||
{
|
||||
return false;
|
||||
throw new InvalidOperationException("Не удалось создать коллекцию");
|
||||
}
|
||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||
string[] set = record[3].Split(_separatorItem, StringSplitOptions.RemoveEmptyEntries);
|
||||
@ -129,15 +129,22 @@ namespace AccordionBus.CollectionGenericObjects
|
||||
{
|
||||
if (elem?.CreateDrawningBus() is T bus)
|
||||
{
|
||||
if (!collection.Insert(bus))
|
||||
try
|
||||
{
|
||||
return false;
|
||||
if (!collection.Insert(bus))
|
||||
{
|
||||
throw new InvalidOperationException("Объект не удалось добавить в коллекцию " + record[3]);
|
||||
}
|
||||
}
|
||||
catch (CollectionOverflowException ex)
|
||||
{
|
||||
throw new InvalidOperationException("Коллекция переполнена", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_storage.Add(record[0], collection);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.Exceptions
|
||||
{
|
||||
[Serializable]
|
||||
public class CollectionOverflowException : ApplicationException
|
||||
{
|
||||
public CollectionOverflowException(int count) : base("В коллекции превышено допустимое количество count : " + count) { }
|
||||
|
||||
public CollectionOverflowException() : base() { }
|
||||
|
||||
public CollectionOverflowException(string message) : base(message) { }
|
||||
|
||||
public CollectionOverflowException(string message, Exception exception) : base(message, exception) { }
|
||||
|
||||
protected CollectionOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.Exceptions
|
||||
{
|
||||
[Serializable]
|
||||
public 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 context) : base(info, context) { }
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.Exceptions
|
||||
{
|
||||
[Serializable]
|
||||
public class PozitionOutOfCollectionException : ApplicationException
|
||||
{
|
||||
public PozitionOutOfCollectionException(int i) : base("Выход за границы коллекции. Позиция " + i) { }
|
||||
|
||||
public PozitionOutOfCollectionException() : base() { }
|
||||
|
||||
public PozitionOutOfCollectionException(string message) : base(message) { }
|
||||
|
||||
public PozitionOutOfCollectionException(string message, Exception exception) : base(message, exception) { }
|
||||
|
||||
protected PozitionOutOfCollectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
using AccordionBus.CollectionGenericObjects;
|
||||
using AccordionBus.Drawnings;
|
||||
using AccordionBus.Exceptions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@ -19,10 +21,13 @@ namespace AccordionBus
|
||||
|
||||
private AbstractCompany? _company = null;
|
||||
|
||||
public FormBusCollection()
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public FormBusCollection(ILogger<FormBusCollection> logger)
|
||||
{
|
||||
InitializeComponent();
|
||||
_storageCollection = new StorageCollection<DrawningBus>();
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private void comboBoxSelectedCompany_SelectedIndexChanged(object sender, EventArgs e)
|
||||
@ -43,14 +48,19 @@ namespace AccordionBus
|
||||
|
||||
bus.SetPictureSize(pictureBox.Width, pictureBox.Height);
|
||||
|
||||
if (_company + bus)
|
||||
try
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBox.Image = _company.Show();
|
||||
if (_company + bus)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBox.Image = _company.Show();
|
||||
_logger.LogInformation("Объект добавлен в коллекцию, " + bus.GetDataForSave());
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (CollectionOverflowException ex)
|
||||
{
|
||||
MessageBox.Show("Объект не удалось добавить");
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
_logger.LogError("Ошибка: {message}", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,14 +72,24 @@ namespace AccordionBus
|
||||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
|
||||
|
||||
int pos = Convert.ToInt32(maskedTextBox.Text);
|
||||
if (_company - pos)
|
||||
try
|
||||
{
|
||||
MessageBox.Show("Объект удалён");
|
||||
pictureBox.Image = _company.Show();
|
||||
if (_company - pos)
|
||||
{
|
||||
MessageBox.Show("Объект удалён");
|
||||
pictureBox.Image = _company.Show();
|
||||
_logger.LogInformation("Объект по позиции {0} удалён", pos);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (PozitionOutOfCollectionException ex)
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
MessageBox.Show("Не удалось удалить объект. Не верно указана позиция");
|
||||
_logger.LogError("Ошибка: {message}", ex.Message);
|
||||
}
|
||||
catch (ObjectNotFoundException ex)
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект, его не существует");
|
||||
_logger.LogError("Ошибка: {message}", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,10 +99,16 @@ namespace AccordionBus
|
||||
|
||||
DrawningBus? bus = null;
|
||||
int counter = 100;
|
||||
while (bus == null && counter > 0)
|
||||
try
|
||||
{
|
||||
bus = _company.GetRandomObject();
|
||||
counter--;
|
||||
while (bus == null && counter > 0)
|
||||
{
|
||||
bus = _company.GetRandomObject();
|
||||
counter--;
|
||||
}
|
||||
} catch(PozitionOutOfCollectionException ex)
|
||||
{
|
||||
_logger.LogError("Ошибка: {message}", ex.Message);
|
||||
}
|
||||
|
||||
if (bus == null) return;
|
||||
@ -115,8 +141,10 @@ namespace AccordionBus
|
||||
else if (radioButtonList.Checked) collectionType = CollectionType.List;
|
||||
|
||||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||||
_logger.LogInformation("Добавлена новая коллекция {0}", textBoxCollectionName.Text);
|
||||
textBoxCollectionName.Text = "";
|
||||
RefreshListBoxItems();
|
||||
|
||||
}
|
||||
|
||||
private void buttonCollectionDel_Click(object sender, EventArgs e)
|
||||
@ -129,11 +157,13 @@ namespace AccordionBus
|
||||
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||||
RefreshListBoxItems();
|
||||
MessageBox.Show("Компания удалена");
|
||||
_logger.LogInformation("Компания удалена");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить компанию");
|
||||
}
|
||||
_logger.LogError("Ошибка: не удалось удалить компанию");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -181,15 +211,18 @@ namespace AccordionBus
|
||||
{
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_storageCollection.SaveData(saveFileDialog.FileName))
|
||||
try
|
||||
{
|
||||
_storageCollection.SaveData(saveFileDialog.FileName);
|
||||
MessageBox.Show("Сохранение прошло успешно", "Резудьтат",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
_logger.LogInformation("Сохранение в файл {filename}", saveFileDialog.FileName);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show("Не сохранилось", "Результат",
|
||||
MessageBox.Show(ex.Message, "Результат",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogError("Ошибка: {message}", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -198,16 +231,19 @@ namespace AccordionBus
|
||||
{
|
||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_storageCollection.LoadData(openFileDialog.FileName))
|
||||
try
|
||||
{
|
||||
_storageCollection.LoadData(openFileDialog.FileName);
|
||||
MessageBox.Show("Загрузка прошла успешно", "Результат",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
RefreshListBoxItems();
|
||||
_logger.LogInformation("Загрузка из файла {filename}", openFileDialog.FileName);
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Не загрузилось", "Результат",
|
||||
MessageBox.Show(ex.Message, "Результат",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogError("Ошибка: {message}", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,9 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace AccordionBus
|
||||
{
|
||||
internal static class Program
|
||||
@ -10,8 +16,24 @@ namespace AccordionBus
|
||||
{
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ServiceCollection services = new();
|
||||
ConfigureServices(services);
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new FormBusCollection());
|
||||
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
Application.Run(serviceProvider.GetRequiredService<FormBusCollection>());
|
||||
}
|
||||
|
||||
private static void ConfigureServices(ServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<FormBusCollection>()
|
||||
.AddLogging(option => {
|
||||
option.SetMinimumLevel(LogLevel.Debug);
|
||||
option.AddSerilog(new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||
.AddJsonFile("C:\\Users\\Professional\\Desktop\\labs\\8\\AccordionBus\\AccordionBus\\serilog.json")
|
||||
.Build())
|
||||
.CreateLogger());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
18
AccordionBus/AccordionBus/serilog.json
Normal file
18
AccordionBus/AccordionBus/serilog.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs\\log.txt",
|
||||
"outputTemplate": "[{Level:u}] [{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}] {Message:1j}{NewLine}{Exception}"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Properties": {
|
||||
"Application": "Sample"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user