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

This commit is contained in:
zolotovart 2024-05-06 18:13:25 +03:00
parent f813e603f6
commit 85cf4068b2
18 changed files with 780 additions and 104 deletions

25
Kursach/Kursach.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34024.191
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kursach", "Kursach\Kursach.csproj", "{F7A92B07-FEB7-46DF-95FA-0942077ADE41}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F7A92B07-FEB7-46DF-95FA-0942077ADE41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7A92B07-FEB7-46DF-95FA-0942077ADE41}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7A92B07-FEB7-46DF-95FA-0942077ADE41}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7A92B07-FEB7-46DF-95FA-0942077ADE41}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0F654BCD-A24D-4152-A016-59A66A97ADC9}
EndGlobalSection
EndGlobal

55
Kursach/Kursach/Deque.cs Normal file
View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kursach
{
public class Deque
{
public LinkedList<int>? list;
public int numElems;
public Deque()
{
list = new LinkedList<int>();
numElems = 0;
}
public void InsertFirst(int value)
{
list.AddFirst(value);
}
public void InsertLast(int value)
{
list.AddLast(value);
}
public int PopFirst()
{
int temp = list.getHead();
list.RemoveFirst();
return temp;
}
public int PopLast()
{
int temp = list.getTail();
list.RemoveLast();
return temp;
}
public bool isEmpty()
{
return list.IsEmpty();
}
public void Clear()
{
list.Clear();
}
}
}

145
Kursach/Kursach/Form1.Designer.cs generated Normal file
View File

@ -0,0 +1,145 @@
namespace Kursach
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
textBoxInput = new TextBox();
buttonAddFirst = new Button();
buttonAddLast = new Button();
buttonDelFirst = new Button();
buttonDelLast = new Button();
buttonClear = new Button();
button = new Button();
groupBoxControls = new GroupBox();
groupBoxControls.SuspendLayout();
SuspendLayout();
//
// textBoxInput
//
textBoxInput.Location = new Point(6, 30);
textBoxInput.Name = "textBoxInput";
textBoxInput.Size = new Size(333, 31);
textBoxInput.TabIndex = 0;
//
// buttonAddFirst
//
buttonAddFirst.Location = new Point(6, 67);
buttonAddFirst.Name = "buttonAddFirst";
buttonAddFirst.Size = new Size(104, 62);
buttonAddFirst.TabIndex = 1;
buttonAddFirst.Text = "Добавить в начало";
buttonAddFirst.UseVisualStyleBackColor = true;
//
// buttonAddLast
//
buttonAddLast.Location = new Point(116, 67);
buttonAddLast.Name = "buttonAddLast";
buttonAddLast.Size = new Size(104, 62);
buttonAddLast.TabIndex = 2;
buttonAddLast.Text = "Добавить в конец";
buttonAddLast.UseVisualStyleBackColor = true;
//
// buttonDelFirst
//
buttonDelFirst.Location = new Point(253, 67);
buttonDelFirst.Name = "buttonDelFirst";
buttonDelFirst.Size = new Size(104, 62);
buttonDelFirst.TabIndex = 3;
buttonDelFirst.Text = "Извлечь из начала";
buttonDelFirst.UseVisualStyleBackColor = true;
//
// buttonDelLast
//
buttonDelLast.Location = new Point(363, 67);
buttonDelLast.Name = "buttonDelLast";
buttonDelLast.Size = new Size(104, 62);
buttonDelLast.TabIndex = 4;
buttonDelLast.Text = "Извлечь из конца";
buttonDelLast.UseVisualStyleBackColor = true;
//
// buttonClear
//
buttonClear.Location = new Point(497, 67);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(104, 62);
buttonClear.TabIndex = 5;
buttonClear.Text = "Очистить очередь";
buttonClear.UseVisualStyleBackColor = true;
//
// button
//
button.BackColor = Color.FromArgb(192, 0, 0);
button.Location = new Point(607, 67);
button.Name = "button";
button.Size = new Size(104, 62);
button.TabIndex = 6;
button.Text = "Очистить очередь";
button.UseVisualStyleBackColor = false;
//
// groupBoxControls
//
groupBoxControls.Controls.Add(textBoxInput);
groupBoxControls.Controls.Add(button);
groupBoxControls.Controls.Add(buttonAddFirst);
groupBoxControls.Controls.Add(buttonClear);
groupBoxControls.Controls.Add(buttonAddLast);
groupBoxControls.Controls.Add(buttonDelLast);
groupBoxControls.Controls.Add(buttonDelFirst);
groupBoxControls.Dock = DockStyle.Top;
groupBoxControls.Location = new Point(0, 0);
groupBoxControls.Name = "groupBoxControls";
groupBoxControls.Size = new Size(800, 146);
groupBoxControls.TabIndex = 7;
groupBoxControls.TabStop = false;
groupBoxControls.Text = "АТД дек";
//
// Form1
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 568);
Controls.Add(groupBoxControls);
Name = "Form1";
Text = "Form1";
groupBoxControls.ResumeLayout(false);
groupBoxControls.PerformLayout();
ResumeLayout(false);
}
#endregion
private TextBox textBoxInput;
private Button buttonAddFirst;
private Button buttonAddLast;
private Button buttonDelFirst;
private Button buttonDelLast;
private Button buttonClear;
private Button button;
private GroupBox groupBoxControls;
}
}

10
Kursach/Kursach/Form1.cs Normal file
View File

@ -0,0 +1,10 @@
namespace Kursach
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}

120
Kursach/Kursach/Form1.resx Normal file
View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kursach
{
public class LinkedList<T>
{
private Node<T>? head;
private Node<T>? tail;
private int numElems;
public LinkedList()
{
head = null;
tail = null;
numElems = 0;
}
public void AddLast(T value)
{
Node<T> node = new Node<T>(value);
if (head == null)
{
head = node;
}
else
{
tail.next = node;
}
tail = node;
numElems++;
}
public void AddFirst(T value)
{
Node<T> node = new Node<T>(value);
if (numElems == 0)
{
head = node;
tail = head;
}
else
{
node.next = head;
}
head = node;
numElems++;
}
public bool RemoveFirst()
{
if (head == null)
{
return false;
}
if (head == tail)
{
head = tail = null;
}
else
{
head = head.next;
}
numElems--;
return true;
}
public bool RemoveLast()
{
if (head == null)
{
return false;
}
if (head == tail)
{
head = tail = null;
}
else
{
Node<T>? prev = null;
Node<T>? curr = head;
while (curr.next != null)
{
prev = curr;
curr = curr.next;
}
tail = prev;
tail.next = null;
}
numElems--;
return true;
}
public bool Contains(T value)
{
Node<T>? curr = head;
while (curr != null)
{
if (curr.data.Equals(value))
{
return true;
}
curr = curr.next;
}
return false;
}
public void Clear()
{
head = null;
tail = null;
numElems = 0;
}
public bool IsEmpty()
{
return numElems==0;
}
public T getHead()
{
return head.data;
}
public T getTail()
{
return tail.data;
}
}
unsafe public class Node<T>
{
public Node<T> next { get; set; }
public T data { get; set; }
public Node(T data)
{
this.data = data;
}
}
}

View File

@ -0,0 +1,17 @@
namespace Kursach
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using ProjectLinkor.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -41,47 +42,32 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
{
_collection = new();
}
public T? Get(int position)
public T Get(int position)
{
// TODO проверка позиции
if (position <= Count)
{
return _collection[position];
}
else
return null;
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
if (_collection[position] == null) throw new ObjectNotFoundException();
return _collection[position];
}
public int Insert(T obj)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO вставка в конец набора
if (Count + 1 > _maxCount)
return -1;
if (Count == _maxCount) throw new CollectionOverflowException(Count);
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO проверка позиции
// TODO вставка по позиции
if (Count + 1 > _maxCount)
return -1;
if (position < 0 || position > Count)
return -1;
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
_collection.Insert(position, obj);
return 1;
return position;
}
public T? Remove(int position)
public T Remove(int position)
{
// TODO проверка позиции
// TODO удаление объекта из списка
if (position < 0 || position > Count)
return null;
T? temp = _collection[position];
if (position >= _collection.Count || position < 0) throw new PositionOutOfCollectionException(position);
T obj = _collection[position];
_collection.RemoveAt(position);
return temp;
return obj;
}
public IEnumerable<T?> GetItems()

View File

@ -1,4 +1,5 @@
using System;
using ProjectLinkor.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@ -48,12 +49,8 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
public T? Get(int position)
{
if (position <= Count)
{
return _collection[position];
}
else
return null;
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException();
return _collection[position];
}
public int Insert(T obj)
{
@ -65,47 +62,47 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return i;
}
}
return -1;
throw new CollectionOverflowException();
}
public int Insert(T obj, int position)
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// ищется свободное место после этой позиции и идет вставка туда
// если нет после, ищем до
// TODO вставка
if (position < Count)
if (position < 0 || position >= Count)
{
if (position < 0 || position >= Count)
throw new PositionOutOfCollectionException();
}
if (_collection[position] == null)
{
_collection[position] = obj;
return position;
}
int temp = position + 1;
while (temp < Count)
{
if (_collection[temp] == null)
{
return -1;
_collection[temp] = obj;
return temp;
}
if (_collection[position] == null)
++temp;
}
temp = position - 1;
while (temp >= 0)
{
if (_collection[temp] == null)
{
_collection[position] = obj;
return position;
}
else
{
for (int i = 0; i < Count; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
_collection[temp] = obj;
return temp;
}
--temp;
}
return -1;
}
public T? Remove(int position)
public T Remove(int position)
{
// TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива значение null
if (position >= Count || position < 0) return null;
T? myObject = _collection[position];
if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
if (_collection[position] == null) throw new ObjectNotFoundException();
T myObject = _collection[position];
_collection[position] = null;
return myObject;
}

View File

@ -1,4 +1,5 @@
using ProjectLinkor.Drawnings;
using ProjectLinkor.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
@ -91,11 +92,11 @@ public class StorageCollection<T>
return _storages[name];
}
}
public bool SaveData(string filename)
public void SaveData(string filename)
{
if (_storages.Count == 0)
{
return false;
throw new ArgumentException("В хранилище отсутствуют коллекции для сохранения");
}
if (File.Exists(filename))
{
@ -131,30 +132,27 @@ public class StorageCollection<T>
}
}
}
return true;
}
/// <summary>
/// Загрузка информации по кораблям в хранилище из файла
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
public bool LoadData(string filename)
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
throw new FileNotFoundException("Файл не существует");
}
using (StreamReader reader = new(filename))
{
string line = reader.ReadLine();
if (line == null || line.Length == 0)
{
return false;
throw new ArgumentException("В файле нет данных");
}
if (!line.Equals(_collectionKey))
{
//если нет такой записи, то это не те данные
return false;
throw new InvalidDataException("В файле неверные данные");
}
_storages.Clear();
while ((line = reader.ReadLine()) != null)
@ -166,11 +164,9 @@ public class StorageCollection<T>
continue;
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{
return false;
}
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType) ??
throw new InvalidCastException("Не удалось определить тип коллекции:" + record[1]); ;
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems,
StringSplitOptions.RemoveEmptyEntries);
@ -178,16 +174,22 @@ public class StorageCollection<T>
{
if (elem?.CreateDrawingShip() is T ship)
{
if (collection.Insert(ship) == -1)
try
{
return false;
if (collection.Insert(ship) == -1)
{
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
}
}
catch (CollectionOverflowException ex)
{
throw new CollectionOverflowException("Коллекция переполнена", ex);
}
}
}
_storages.Add(record[0], collection);
}
}
return true;
}
/// <summary>
/// Создание коллекции по типу

View File

@ -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 ProjectLinkor.Exceptions;
/// <summary>
/// Класс, описывающий ошибку переполнения коллекции
/// </summary>
[Serializable]
public 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,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLinkor.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,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLinkor.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 ProjectLinkor.CollectionGenericObjects;
using Microsoft.Extensions.Logging;
using ProjectLinkor.CollectionGenericObjects;
using ProjectLinkor.Drawnings;
using ProjectLinkor.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -9,6 +11,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace ProjectLinkor;
/// <summary>
@ -22,13 +25,16 @@ public partial class FormShipCollection : Form
/// Компания
/// </summary>
private AbstractCompany? _company = null;
private readonly ILogger _logger;
/// <summary>
/// Конструктор
/// </summary>
public FormShipCollection()
public FormShipCollection(ILogger<FormShipCollection> logger)
{
InitializeComponent();
_storageCollection = new();
_logger = logger;
}
/// <summary>
/// Выбор компании
@ -56,14 +62,17 @@ public partial class FormShipCollection : Form
{
return;
}
if (_company + ship != -1)
try
{
int addingObject = (_company + ship);
MessageBox.Show("Объект добавлен");
_logger.LogInformation($"Добавлен объект {ship.GetDataForSave()}");
pictureBox.Image = _company.Show();
}
else
catch (CollectionOverflowException ex)
{
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
}
}
@ -71,6 +80,7 @@ public partial class FormShipCollection : Form
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null)
{
_logger.LogWarning("Удаление объекта из несуществующей коллекции");
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
@ -79,16 +89,23 @@ public partial class FormShipCollection : Form
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_company - pos != null)
try
{
object decrementObject = _company - pos;
MessageBox.Show("Объект удален");
_logger.LogInformation($"Удален по позиции {pos}");
pictureBox.Image = _company.Show();
}
else
catch (ObjectNotFoundException ex)
{
MessageBox.Show("Не удалось удалить объект");
MessageBox.Show("Объект не найден ");
_logger.LogWarning($"Удаление не найденного объекта в позиции {pos} ");
}
catch (PositionOutOfCollectionException ex)
{
MessageBox.Show("Удаление вне рамках коллекции");
_logger.LogWarning($"Удаление объекта за пределами коллекции {pos} ");
}
}
/// <summary>
/// Передача объекта в другую форму
@ -106,11 +123,17 @@ public partial class FormShipCollection : Form
int counter = 100;
while (ship == null)
{
ship = _company.GetRandomObject();
counter--;
if (counter <= 0)
try
{
break;
ship = _company.GetRandomObject();
}
catch (ObjectNotFoundException)
{
counter--;
if (counter <= 0)
{
break;
}
}
}
if (ship == null)
@ -150,6 +173,7 @@ public partial class FormShipCollection : Form
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("Не заполненная коллекция");
return;
}
CollectionType collectionType = CollectionType.None;
@ -162,6 +186,7 @@ public partial class FormShipCollection : Form
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
_logger.LogInformation($"Добавлена коллекция: {textBoxCollectionName.Text}");
RerfreshListBoxItems();
}
/// <summary>
@ -178,13 +203,16 @@ public partial class FormShipCollection : Form
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
_logger.LogWarning("Удаление невыбранной коллекции");
return;
}
string name = listBoxCollection.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
_logger.LogInformation($"Удалена коллекция: {name}");
RerfreshListBoxItems();
}
/// <summary>
@ -213,6 +241,7 @@ public partial class FormShipCollection : Form
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
_logger.LogWarning("Создание компании невыбранной коллекции");
return;
}
ICollectionGenericObjects<DrawingShip>? collection =
@ -220,6 +249,7 @@ public partial class FormShipCollection : Form
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
_logger.LogWarning("Не удалось инициализировать коллекцию");
return;
}
switch (comboBoxSelectorCompany.Text)
@ -242,15 +272,16 @@ public partial class FormShipCollection : Form
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storageCollection.SaveData(saveFileDialog.FileName))
try
{
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_storageCollection.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
}
else
catch (Exception ex)
{
MessageBox.Show("Не сохранилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("Ошибка: {Message}", ex.Message);
}
}
}
@ -263,14 +294,17 @@ public partial class FormShipCollection : Form
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storageCollection.LoadData(openFileDialog.FileName))
try
{
_storageCollection.LoadData(openFileDialog.FileName);
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Загрузка из файла: {filename}", saveFileDialog.FileName);
RerfreshListBoxItems();
}
else
catch (Exception ex)
{
MessageBox.Show("Не удалось сохранить", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(ex.Message, "Результат", 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 ProjectLinkor
{
internal static class Program
@ -11,7 +16,30 @@ namespace ProjectLinkor
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormShipCollection());
ServiceCollection services = new();
ConfigureServices(services);
using ServiceProvider serviceProvider =
services.BuildServiceProvider();
Application.Run(serviceProvider.GetRequiredService<FormShipCollection>());
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormShipCollection>()
.AddLogging(option =>
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "serilogConfig.json", optional: false, reloadOnChange: true)
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
option.SetMinimumLevel(LogLevel.Information);
option.AddSerilog(logger);
});
}
}
}

View File

@ -8,6 +8,18 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
@ -23,4 +35,10 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="serilogConfig.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

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": "Linkor"
}
}
}