lab7
This commit is contained in:
parent
38c74b051e
commit
225058cf52
@ -10,7 +10,7 @@ namespace speed_Boat.Exceptions
|
|||||||
[Serializable]
|
[Serializable]
|
||||||
internal class BoatNotFoundException : ApplicationException
|
internal class BoatNotFoundException : ApplicationException
|
||||||
{
|
{
|
||||||
public BoatNotFoundException(int i) : base($"Не найден объект по позиции { i}") { }
|
public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
|
||||||
public BoatNotFoundException() : base() { }
|
public BoatNotFoundException() : base() { }
|
||||||
public BoatNotFoundException(string message) : base(message) { }
|
public BoatNotFoundException(string message) : base(message) { }
|
||||||
public BoatNotFoundException(string message, Exception exception) : base(message, exception){ }
|
public BoatNotFoundException(string message, Exception exception) : base(message, exception){ }
|
||||||
|
@ -6,7 +6,6 @@ using System.Threading.Tasks;
|
|||||||
using SpeedBoatLab.Drawings;
|
using SpeedBoatLab.Drawings;
|
||||||
using speed_Boat.MovementStrategy;
|
using speed_Boat.MovementStrategy;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace speed_Boat.Generics
|
namespace speed_Boat.Generics
|
||||||
{
|
{
|
||||||
@ -53,13 +52,13 @@ namespace speed_Boat.Generics
|
|||||||
/// Перегрузка оператора сложения
|
/// Перегрузка оператора сложения
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool operator + (BoatsGenericCollection<T, U> collect, T? obj)
|
public static int operator + (BoatsGenericCollection<T, U> collect, T? obj)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (obj != null)
|
||||||
{
|
{
|
||||||
return false;
|
return collect?._collection.Insert(obj) ?? -1;
|
||||||
}
|
}
|
||||||
return collect?._collection.Insert(obj) ?? false;
|
return 0;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Перегрузка оператора вычитания
|
/// Перегрузка оператора вычитания
|
||||||
|
@ -80,7 +80,10 @@ namespace speed_Boat.Generics
|
|||||||
}
|
}
|
||||||
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
||||||
}
|
}
|
||||||
|
if(data.Length == 0)
|
||||||
|
{
|
||||||
|
throw new Exception("Невалидная операция, нет данных для сохранения");
|
||||||
|
}
|
||||||
using (StreamWriter sw = new(filename))
|
using (StreamWriter sw = new(filename))
|
||||||
{
|
{
|
||||||
sw.WriteLine($"BoatStorage{Environment.NewLine}{data}");
|
sw.WriteLine($"BoatStorage{Environment.NewLine}{data}");
|
||||||
@ -126,9 +129,14 @@ namespace speed_Boat.Generics
|
|||||||
DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight);
|
DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||||
if (boat != null)
|
if (boat != null)
|
||||||
{
|
{
|
||||||
if (!(collection + boat))
|
try { _ = collection + boat; }
|
||||||
|
catch (BoatNotFoundException e)
|
||||||
{
|
{
|
||||||
throw new StorageOverflowException("Ошибка добавления в коллекцию");
|
throw e;
|
||||||
|
}
|
||||||
|
catch (StorageOverflowException e)
|
||||||
|
{
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ using speed_Boat.Generics;
|
|||||||
using speed_Boat.MovementStrategy;
|
using speed_Boat.MovementStrategy;
|
||||||
using speed_Boat;
|
using speed_Boat;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using speed_Boat.Exceptions;
|
||||||
|
|
||||||
namespace SpeedBoatLab
|
namespace SpeedBoatLab
|
||||||
{
|
{
|
||||||
@ -74,7 +74,7 @@ namespace SpeedBoatLab
|
|||||||
{
|
{
|
||||||
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
||||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
_logger.LogWarning($"Не все данные заполнены:{nameStorageTextBox.Name}");
|
_logger.LogWarning("Пустое название набора");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_storage.AddSet(nameStorageTextBox.Text);
|
_storage.AddSet(nameStorageTextBox.Text);
|
||||||
@ -97,6 +97,7 @@ namespace SpeedBoatLab
|
|||||||
{
|
{
|
||||||
if (storagesListBox.SelectedIndex == -1)
|
if (storagesListBox.SelectedIndex == -1)
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning("Набор для удаления не выбран");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
string name = storagesListBox.SelectedItem.ToString() ?? string.Empty;
|
string name = storagesListBox.SelectedItem.ToString() ?? string.Empty;
|
||||||
@ -119,6 +120,11 @@ namespace SpeedBoatLab
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonAddBoat_Click(object sender, EventArgs e)
|
private void ButtonAddBoat_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
if (storagesListBox.SelectedIndex == -1)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Набор для добавления обьекта не выбран");
|
||||||
|
return;
|
||||||
|
}
|
||||||
var FormBoatConfig = new FormBoatConfig();
|
var FormBoatConfig = new FormBoatConfig();
|
||||||
FormBoatConfig.AddEvent(new(AddBoat));
|
FormBoatConfig.AddEvent(new(AddBoat));
|
||||||
FormBoatConfig.Show();
|
FormBoatConfig.Show();
|
||||||
@ -128,25 +134,29 @@ namespace SpeedBoatLab
|
|||||||
{
|
{
|
||||||
if (storagesListBox.SelectedIndex == -1)
|
if (storagesListBox.SelectedIndex == -1)
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning("Набор для удаления не выбран");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty];
|
var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty];
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning("Добавление пустого обьекта");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (obj + boat)
|
try
|
||||||
{
|
{
|
||||||
|
_ = obj + boat;
|
||||||
MessageBox.Show("Объект добавлен");
|
MessageBox.Show("Объект добавлен");
|
||||||
_logger.LogInformation($"Объект добавлен:{obj}");
|
_logger.LogInformation($"Добавлен объект в набор {storagesListBox.SelectedItem.ToString()}");
|
||||||
pictureBoxCollection.Image = obj.ShowBoats();
|
pictureBoxCollection.Image = obj.ShowBoats();
|
||||||
}
|
}
|
||||||
else
|
catch(StorageOverflowException ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось добавить объект");
|
MessageBox.Show("Не удалось добавить объект");
|
||||||
_logger.LogInformation($"Не удалось добавить объект:{obj}");
|
_logger.LogWarning($"{ex.Message} в наборе {storagesListBox.SelectedItem.ToString()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление объекта из набора
|
/// Удаление объекта из набора
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -156,6 +166,7 @@ namespace SpeedBoatLab
|
|||||||
{
|
{
|
||||||
if (storagesListBox.SelectedIndex == -1)
|
if (storagesListBox.SelectedIndex == -1)
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning("Удаление объекта из несуществующего набора");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty];
|
var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty];
|
||||||
@ -178,23 +189,31 @@ namespace SpeedBoatLab
|
|||||||
MessageBox.Show("Неверный формат позиции");
|
MessageBox.Show("Неверный формат позиции");
|
||||||
_logger.LogWarning($"Неверный формат позиции:{pos}");
|
_logger.LogWarning($"Неверный формат позиции:{pos}");
|
||||||
}
|
}
|
||||||
|
try
|
||||||
if (obj - pos != null)
|
|
||||||
{
|
{
|
||||||
MessageBox.Show("Объект удален");
|
if (obj - pos != null)
|
||||||
pictureBoxCollection.Image = obj.ShowBoats();
|
{
|
||||||
|
MessageBox.Show("Объект удален");
|
||||||
|
pictureBoxCollection.Image = obj.ShowBoats();
|
||||||
|
_logger.LogInformation($"Удален объект из набора {storagesListBox.SelectedItem.ToString()}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Не удалось удалить объект");
|
||||||
|
_logger.LogInformation($"Не удалось удалить объект из набора {storagesListBox.SelectedItem.ToString()}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(BoatNotFoundException ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show(ex.Message);
|
||||||
|
_logger.LogWarning($"{ex.Message} из набора {storagesListBox.SelectedItem.ToString()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (insertPosition == string.Empty)
|
else if(insertPosition == string.Empty)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Неверный формат позиции");
|
MessageBox.Show("Неверный формат позиции");
|
||||||
_logger.LogWarning($"Неверный формат позиции:{insertPosition}");
|
_logger.LogWarning($"Неверный формат позиции:{insertPosition}");
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("Не удалось удалить объект");
|
|
||||||
_logger.LogWarning($"Не удалось удалить объект:{obj}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Обновление рисунка по набору
|
/// Обновление рисунка по набору
|
||||||
@ -230,14 +249,14 @@ namespace SpeedBoatLab
|
|||||||
_storage.SaveData(saveFileDialog.FileName);
|
_storage.SaveData(saveFileDialog.FileName);
|
||||||
MessageBox.Show("Сохранение прошло успешно",
|
MessageBox.Show("Сохранение прошло успешно",
|
||||||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
_logger.LogInformation($"Сохранение прошло успешно:");
|
_logger.LogInformation($"Сохранение наборов в файл {saveFileDialog.FileName} прошло успешно:");
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show($"Не сохранилось: {ex.Message}",
|
MessageBox.Show($"Не сохранилось: {ex.Message}",
|
||||||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
_logger.LogWarning($"Не сохранилось:{ex.Message}");
|
_logger.LogWarning($"Не удалось сохранить наборы: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -252,13 +271,15 @@ namespace SpeedBoatLab
|
|||||||
{
|
{
|
||||||
_storage.LoadData(saveFileDialog.FileName);
|
_storage.LoadData(saveFileDialog.FileName);
|
||||||
ReloadObjects();
|
ReloadObjects();
|
||||||
MessageBox.Show("Сохранение прошло успешно",
|
MessageBox.Show("Загрузка прошла успешно",
|
||||||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation($"Загрузились наборы из файла {openFileDialog.FileName}");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show($"Не удалось загрузить: {ex.Message}",
|
MessageBox.Show($"Не удалось загрузить: {ex.Message}",
|
||||||
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogWarning($"Не удалось сохранить наборы с ошибкой: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,12 +35,12 @@ namespace speed_Boat.Generics
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор
|
/// Добавление объекта в набор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Insert(T boat)
|
public int Insert(T boat)
|
||||||
{
|
{
|
||||||
if(_places.Count == 0)
|
if(_places.Count == 0)
|
||||||
{
|
{
|
||||||
_places.Add(boat);
|
_places.Add(boat);
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -53,7 +53,7 @@ namespace speed_Boat.Generics
|
|||||||
_places[i] = _places[_places.Count - 1];
|
_places[i] = _places[_places.Count - 1];
|
||||||
_places[_places.Count - 1] = temp;
|
_places[_places.Count - 1] = temp;
|
||||||
}
|
}
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -103,9 +103,8 @@ namespace speed_Boat.Generics
|
|||||||
public bool Remove(int position)
|
public bool Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= Count)
|
if (position < 0 || position >= Count)
|
||||||
{
|
{
|
||||||
return false;
|
throw new BoatNotFoundException();
|
||||||
throw new BoatNotFoundException();
|
|
||||||
}
|
}
|
||||||
if (_places[position] == null)
|
if (_places[position] == null)
|
||||||
{
|
{
|
||||||
|
@ -14,6 +14,7 @@ namespace SpeedBoatLab
|
|||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
|
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
ConfigureServices(services);
|
ConfigureServices(services);
|
||||||
@ -40,7 +41,7 @@ namespace SpeedBoatLab
|
|||||||
option.SetMinimumLevel(LogLevel.Information);
|
option.SetMinimumLevel(LogLevel.Information);
|
||||||
option.AddSerilog(logger);
|
option.AddSerilog(logger);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
speed_Boat/speed_Boat/appsettings.json
Normal file
20
speed_Boat/speed_Boat/appsettings.json
Normal 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": "speed_Boat"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@
|
|||||||
<PackageReference Include="Serilog" Version="3.1.1" />
|
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user