PIbd-23_Nasyrov_A_G_Lab7 #9

Closed
gaillard wants to merge 4 commits from lab7 into lab6
11 changed files with 165 additions and 31 deletions

View File

@ -33,8 +33,7 @@ namespace ProjectAirplaneWithRadar.Generics
public static bool operator -(AirplanesGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection[pos];
if (obj != null && collect != null)
return collect._collection.Remove(pos);
collect._collection.Remove(pos);
return false;
}
public U? GetU(int pos)

View File

@ -43,7 +43,7 @@ namespace ProjectAirplaneWithRadar.Generics
}
if (data.Length == 0)
{
return false;
throw new Exception("Невалиданя операция, нет данных для сохранения");
}
string toWrite = $"AirplanesStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
@ -60,7 +60,7 @@ namespace ProjectAirplaneWithRadar.Generics
{
if (!File.Exists(filename))
{
return false;
throw new IOException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
@ -68,12 +68,11 @@ namespace ProjectAirplaneWithRadar.Generics
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
throw new IOException("Нет данных для загрузки");
}
if (!strs[0].StartsWith("AirplanesStorage"))
{
//если нет такой записи, то это не те данные
return false;
throw new IOException("Неверный формат данных");
}
_airplanesStorages.Clear();
do

View File

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

View File

@ -18,6 +18,7 @@ namespace ProjectAirplaneWithRadar.DrawningObjects
protected int _startPosY;
protected readonly int _airplaneWidth = 200;
protected readonly int _airplaneHeight = 78;
char separator = '|';
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _airplaneWidth;

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace ProjectAirplaneWithRadar.Exceptions
{
[Serializable]
internal class AirplaneNotFoundException :ApplicationException
{
public AirplaneNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
public AirplaneNotFoundException() : base() { }
public AirplaneNotFoundException(string message) : base(message) { }
public AirplaneNotFoundException(string message, Exception exception) : base(message, exception) { }
protected AirplaneNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.Exceptions
{
[Serializable]
internal class StorageOverflowException :ApplicationException
{
public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { }
public StorageOverflowException() : base() { }
public StorageOverflowException(string message) : base(message) { }
public StorageOverflowException(string message, Exception exception) : base(message, exception) { }
protected StorageOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}

View File

@ -5,6 +5,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using Microsoft.VisualBasic.Logging;
namespace ProjectAirplaneWithRadar
{

View File

@ -1,8 +1,15 @@
using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.MovementStrategy;
using ProjectAirplaneWithRadar.Generics;
using ProjectAirplaneWithRadar.Exceptions;
using System.Diagnostics.Metrics;
using System.Windows.Forms;
using System.Data;
using System;
using Microsoft.Extensions.Logging;
using System.Xml.Linq;
using Serilog;
//using Microsoft.VisualBasic.Logging;
namespace ProjectAirplaneWithRadar
{
@ -50,15 +57,17 @@ namespace ProjectAirplaneWithRadar
form.Show();
Action<DrawningAirplane>? airplaneDelegate = new((m) =>
{
bool q = (obj + m);
if (q)
try
{
bool q = obj + m;
MessageBox.Show("Îáúåêò äîáàâëåí");
Log.Information($"Äîáàâëåí îáúåêò â êîëëåêöèþ {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
pictureBoxAirplanesCollection.Image = obj.ShowAirplanes();
}
else
catch(StorageOverflowException ex)
{
MessageBox.Show("Íå óäàëîñü äîáàâèòü îáúåêò");
Log.Warning($"Êîëëåêöèÿ {listBoxStorages.SelectedItem.ToString() ?? string.Empty} ïåðåïîëíåíà");
MessageBox.Show(ex.Message);
}
});
form.AddEvent(airplaneDelegate);
@ -80,15 +89,26 @@ namespace ProjectAirplaneWithRadar
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
try
{
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
var q = obj - pos;
MessageBox.Show("Îáúåêò óäàëåí");
Log.Information($"Óäàëåí îáúåêò èç êîëëåêöèè {listBoxStorages.SelectedItem.ToString() ?? string.Empty} ïî íîìåðó {pos}");
pictureBoxAirplanesCollection.Image = obj.ShowAirplanes();
}
else
catch (AirplaneNotFoundException ex)
{
MessageBox.Show("Íå óäàëîñü óäàëèòü îáúåêò");
Log.Warning($"Íå ïîëó÷èëîñü óäàëèòü îáúåêò èç êîëëåêöèè {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show(ex.Message);
}
catch (FormatException ex)
{
Log.Warning($"Áûëî ââåäåíî íå ÷èñëî");
MessageBox.Show("Ââåäèòå ÷èñëî");
}
}
private void buttonUpdateCollection_Click(object sender, EventArgs e)
@ -115,7 +135,7 @@ namespace ProjectAirplaneWithRadar
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
ReloadObjects(); Log.Information($"Äîáàâëåí íàáîð: {textBoxStorageName.Text}");
}
private void buttonRemoveObject_Click(object sender, EventArgs e)
{
@ -125,9 +145,10 @@ namespace ProjectAirplaneWithRadar
}
if (MessageBox.Show($"Óäàëèòü îáúåêò {listBoxStorages.SelectedItem}?", "Óäàëåíèå", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString()
?? string.Empty);
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
_storage.DelSet(name);
ReloadObjects();
Log.Information($"Óäàëåí íàáîð: {name}");
}
}
@ -144,15 +165,17 @@ namespace ProjectAirplaneWithRadar
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.SaveData(saveFileDialog.FileName))
try
{
_storage.SaveData(saveFileDialog.FileName);
MessageBox.Show("Ñîõðàíåíèå ïðîøëî óñïåøíî",
"Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Ôàéë {saveFileDialog.FileName} óñïåøíî ñîõðàíåí");
}
else
catch (Exception ex)
{
MessageBox.Show("Íå ñîõðàíèëîñü", "Ðåçóëüòàò",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Log.Warning("Íå óäàëîñü ñîõðàíèòü");
MessageBox.Show($"Íå ñîõðàíèëîñü: {ex.Message}","Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
@ -160,19 +183,22 @@ namespace ProjectAirplaneWithRadar
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(openFileDialog.FileName))
try
{
_storage.LoadData(openFileDialog.FileName);
MessageBox.Show("Çàãðóçêà ïðîøëà óñïåøíî",
"Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Ôàéë {openFileDialog.FileName} óñïåøíî çàãðóæåí");
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
ReloadObjects();
}
else
catch (Exception ex)
{
MessageBox.Show("Íå çàãðóçèëîñü", "Ðåçóëüòàò",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Log.Warning("Íå óäàëîñü çàãðóçèòü");
MessageBox.Show($"Íå çàãðóçèëîñü: {ex.Message}","Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

View File

@ -1,3 +1,19 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Json;
using Serilog.Configuration;
//using Microsoft.VisualBasic.Logging;
Review

Закомментированного кода быть не должно

Закомментированного кода быть не должно
namespace ProjectAirplaneWithRadar
{
internal static class Program
@ -8,10 +24,25 @@ namespace ProjectAirplaneWithRadar
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
string[] path = Directory.GetCurrentDirectory().Split('\\');
string pathNeed = "";
for (int i = 0; i < path.Length - 3; i++)
{
pathNeed += path[i] + "\\";
}
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: $"{pathNeed}AppSettings.json", optional: false, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormAirplanesCollection());
}
}
}

View File

@ -8,4 +8,21 @@
<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.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="AppSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -1,4 +1,7 @@
using System.CodeDom;
using ProjectAirplaneWithRadar.Exceptions;
using System;
using System.Windows.Forms;
using System.CodeDom;
using System.ComponentModel;
using System.Numerics;
using System.Windows.Forms.VisualStyles;
@ -19,13 +22,15 @@ namespace ProjectAirplaneWithRadar.Generics
public bool Insert(T airplane)
{
if (_places.Count == _maxCount)
return false;
throw new StorageOverflowException(_maxCount);
Insert(airplane, 0);
return true;
}
public bool Insert(T airplane, int position)
{
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
return false;
_places.Insert(position, airplane);
return true;
@ -33,7 +38,7 @@ namespace ProjectAirplaneWithRadar.Generics
public bool Remove(int position)
{
if (!(position >= 0 && position < Count))
return false;
throw new AirplaneNotFoundException(position);
_places.RemoveAt(position);
return true;
}