This commit is contained in:
Кашин Максим 2022-11-27 01:28:00 +04:00
parent ea083799c6
commit b6ae2985c9
8 changed files with 192 additions and 43 deletions

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -7,7 +8,6 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GasolineTanker
{
public partial class FormMapWithSetGasolineTanker : Form
@ -19,10 +19,12 @@ namespace GasolineTanker
};
private MapWithSetGasolienTankerGeneric<DrawingObjectGasolineTanker, AbstractMap> _mapGasolineTankerCollectionGeneric;
private readonly MapsCollection _mapsCollection;
public FormMapWithSetGasolineTanker()
private readonly ILogger _logger;
public FormMapWithSetGasolineTanker(ILogger<FormMapWithSetGasolineTanker> logger)
{
InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
_logger = logger;
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
@ -53,12 +55,14 @@ namespace GasolineTanker
{
if (listBoxMaps.SelectedIndex == -1)
{
_logger.LogInformation($"Attempt to remove a non - existent card: {textBoxNewMapName.Text}");
return;
}
if (MessageBox.Show($"Delete map {listBoxMaps.SelectedItem}?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
if (MessageBox.Show($"Delete card {listBoxMaps.SelectedItem}?", "Removal", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
_logger.LogInformation($"Map removed: {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}");
}
}
private void ButtonAddGasolineTanker_Click(object sender, EventArgs e)
@ -71,19 +75,36 @@ namespace GasolineTanker
}
private void AddGasolineTankerOnMap(DrawingGasolineTanker drawingGasolineTanker)
{
if (listBoxMaps.SelectedIndex == -1)
try
{
return;
if (listBoxMaps.SelectedIndex == -1)
{
_logger.LogInformation($"Attempt to add a feature to an unselected map");
return;
}
if (drawingGasolineTanker == null)
{
MessageBox.Show("You need to select an object before adding");
_logger.LogInformation($"No object was selected to add to the map");
return;
}
DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(drawingGasolineTanker);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + gasolineTanker != -1)
{
MessageBox.Show("Object added");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
_logger.LogInformation($"Object added {drawingGasolineTanker} to map ");
}
else
{
MessageBox.Show("Failed to add item");
_logger.LogInformation($"Failed to add item {drawingGasolineTanker} to map ");
}
}
DrawingObjectGasolineTanker gasolineTanker = new(drawingGasolineTanker);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + gasolineTanker != -1)
catch (StorageOverflowException ex)
{
MessageBox.Show("Object added");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Failed to add object");
_logger.LogWarning($"Storage full error: {ex.Message}");
MessageBox.Show($"Storage full error: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
@ -155,16 +176,19 @@ namespace GasolineTanker
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Not all data saved", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("Not all data is filled", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogInformation("When adding card {0}", comboBoxSelectorMap.SelectedIndex == -1 ? "No card selected" : "No card named");
return;
}
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("No such card", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("No card named: {0}", textBoxNewMapName.Text);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
_logger.LogInformation("Added map {0}", textBoxNewMapName.Text); ;
}
private void buttonDeleteMap_Click(object sender, EventArgs e)
{
@ -172,9 +196,10 @@ namespace GasolineTanker
{
return;
}
if (MessageBox.Show($"Delete map {listBoxMaps.SelectedItem}?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
if (MessageBox.Show($"Delete card {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
_logger.LogInformation("Map removed {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
}
}
@ -182,6 +207,7 @@ namespace GasolineTanker
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
_logger.LogInformation("You have navigated to the map named: {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
}
private void buttonRemoveGasolineTanker_Click(object sender, EventArgs e)
@ -194,19 +220,34 @@ namespace GasolineTanker
{
return;
}
if (MessageBox.Show("Delete object?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (MessageBox.Show("Delete object?", "Removal", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
try
{
MessageBox.Show("Object delete");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Object removed");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
_logger.LogInformation($"Object removed { pos}");
}
else
{
MessageBox.Show("Failed to delete object");
_logger.LogInformation($"Failed to delete object {pos}");
}
}
else
catch (GasolineTankerNotFoundException ex)
{
MessageBox.Show("Errore delete object");
_logger.LogWarning($"Deletion error: {ex.Message}");
MessageBox.Show($"Deletion error {ex.Message}");
}
catch (Exception ex)
{
_logger.LogWarning($"Unknown error: {ex.Message}");
MessageBox.Show($"Unknown error {ex.Message}");
}
}
@ -214,13 +255,16 @@ namespace GasolineTanker
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
try
{
MessageBox.Show("Save was successful", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
_mapsCollection.SaveData(saveFileDialog.FileName);
_logger.LogInformation("The save was successful. The file is located: {0}", saveFileDialog.FileName);
MessageBox.Show("The save was successful", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
catch (Exception ex)
{
MessageBox.Show("Not preserved", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Not preserved: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("Failed to save file '{0}'. Error text: {1}", saveFileDialog.FileName, ex.Message);
}
}
}
@ -229,14 +273,17 @@ namespace GasolineTanker
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
try
{
MessageBox.Show("Download successful", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
_mapsCollection.LoadData(openFileDialog.FileName);
MessageBox.Show("Opening was successful", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Opening file '{0}' was successful", openFileDialog.FileName);
ReloadMaps();
}
else
catch (Exception ex)
{
MessageBox.Show("Didn't load", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Failed to open: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("Failed to open file {0}. Error text: {1}", openFileDialog.FileName, ex.Message);
}
}
}

View File

@ -8,6 +8,19 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.1.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace GasolineTanker
{
[Serializable]
internal class GasolineTankerNotFoundException : ApplicationException
{
public GasolineTankerNotFoundException(int i) : base($"Object not found by position { i}") { }
public GasolineTankerNotFoundException() : base() { }
public GasolineTankerNotFoundException(string message) : base(message) { }
public GasolineTankerNotFoundException(string message, Exception exception) : base(message, exception) { }
protected GasolineTankerNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}
}

View File

@ -43,7 +43,7 @@ namespace GasolineTanker
return null;
}
}
public bool SaveData(string filename)
public void SaveData(string filename)
{
if (File.Exists(filename))
{
@ -57,13 +57,12 @@ namespace GasolineTanker
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
}
}
return true;
}
public bool LoadData(string filename)
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
throw new Exception("File not found");
}
using (StreamReader sr = new StreamReader(filename))
{
@ -72,7 +71,7 @@ namespace GasolineTanker
str = sr.ReadLine();
if (!str.Contains("MapsCollection"))
{
return false;
throw new Exception("The file format is incorrec");
}
while ((str = sr.ReadLine()) != null)
{
@ -90,7 +89,6 @@ namespace GasolineTanker
_mapStorages.Add(elem[0], new MapWithSetGasolienTankerGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
return true;
}
}
}

View File

@ -1,3 +1,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
namespace GasolineTanker
{
internal static class Program
@ -11,7 +16,30 @@ namespace GasolineTanker
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormMapWithSetGasolineTanker());
var services = new ServiceCollection();
ConfigureServices(services);
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
{
Application.Run(serviceProvider.GetRequiredService<FormMapWithSetGasolineTanker>());
}
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormMapWithSetGasolineTanker>()
.AddLogging(option =>
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "C:\\Users\\Ìàêñèì\\Desktop\\Óíèâåð\\3 ñåìåñòð\\ÐÏÏ\\GasolineTanker\\GasolineTanker\\GasolineTanker\\appSetting.json", optional: false, reloadOnChange: true)
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
option.SetMinimumLevel(LogLevel.Information);
option.AddSerilog(logger);
});
}
}
}

View File

@ -29,19 +29,30 @@ namespace GasolineTanker
}
return -1;
}
public int Insert(T gasolineTanker, int position)
private bool isCorrectPosition(int position)
{
return 0 <= position && position < _maxCount;
}
public int Insert(T gasolineTanker, int position)
{
if (position < 0 || position >= _places.Count) return -1;
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!isCorrectPosition(position))
{
return -1;
}
_places.Insert(position, gasolineTanker);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= _places.Count) return null;
if (_places[position] == null) return null;
T removed = _places[position];
if (!isCorrectPosition(position))
return null;
var result = this[position];
if (result == null)
throw new GasolineTankerNotFoundException(position);
_places.RemoveAt(position);
return removed;
return result;
}
public T this[int position]
{

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace GasolineTanker
{
[Serializable]
internal class StorageOverflowException : ApplicationException
{
public StorageOverflowException(int count) : base($"The set exceeded the allowed quantity: {count}") { }
public StorageOverflowException() : base() { }
public StorageOverflowException(string message) : base(message) { }
public StorageOverflowException(string message, Exception exception) : base(message, exception) { }
protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}
}

View File

@ -0,0 +1,12 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": { "path": "C:\\Университет\\2 курс\\РПП\\AntiAircraftGun\\AntiAircraftGun\\AntiAircraftGun\\bin\\Debug\\net6.0-windows\\Log.txt" }
}
]
}
}