Готовая лабораторная работа 8

This commit is contained in:
sqdselo 2024-06-05 06:51:03 +04:00
parent f655c6b4bb
commit 49631f3dfa
12 changed files with 151 additions and 93 deletions

View File

@ -30,7 +30,7 @@ namespace HoistingCrane.CollectionGenericObjects
{ {
get get
{ {
return (pictureWidth * pictureHeight) / (_placeSizeHeight * _placeSizeWidth)-2; return (pictureWidth / _placeSizeWidth) * (pictureHeight * _placeSizeHeight);
} }
} }
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTrackedVehicle> array) public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTrackedVehicle> array)
@ -42,11 +42,11 @@ namespace HoistingCrane.CollectionGenericObjects
} }
public static int operator +(AbstractCompany company, DrawningTrackedVehicle car) public static int operator +(AbstractCompany company, DrawningTrackedVehicle car)
{ {
return company.arr?.Insert(car, new DrawningCraneEqutables()) ?? -1; return company.arr.Insert(car, new DrawningCraneEqutables());
} }
public static DrawningTrackedVehicle operator -(AbstractCompany company, int position) public static DrawningTrackedVehicle operator -(AbstractCompany company, int position)
{ {
return company.arr?.Remove(position) ?? null; return company.arr?.Remove(position);
} }
public DrawningTrackedVehicle? GetRandomObject() public DrawningTrackedVehicle? GetRandomObject()

View File

@ -19,7 +19,7 @@ namespace HoistingCrane.CollectionGenericObjects
/// <param name="obj"></param> /// <param name="obj"></param>
/// /// <param name="comparer">Сравнение двух объектов</param> /// /// <param name="comparer">Сравнение двух объектов</param>
/// <returns></returns> /// <returns></returns>
int Insert(T obj, IEqualityComparer<DrawningTrackedVehicle>? comparer = null); int Insert(T obj, IEqualityComparer<T>? comparer = null);
/// <summary> /// <summary>
/// Добавление элемента в коллекцию на определенную позицию /// Добавление элемента в коллекцию на определенную позицию
/// </summary> /// </summary>
@ -27,7 +27,7 @@ namespace HoistingCrane.CollectionGenericObjects
/// <param name="position"></param> /// <param name="position"></param>
/// <param name="comparer">Сравнение двух объектов</param> /// <param name="comparer">Сравнение двух объектов</param>
/// <returns></returns> /// <returns></returns>
int Insert(T obj, int position, IEqualityComparer<DrawningTrackedVehicle>? comparer = null); int Insert(T obj, int position, IEqualityComparer<T>? comparer = null);
/// <summary> /// <summary>
/// Удаление элемента из коллекции по его позиции /// Удаление элемента из коллекции по его позиции
/// </summary> /// </summary>

View File

@ -46,37 +46,43 @@ namespace HoistingCrane.CollectionGenericObjects
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
return list[position]; return list[position];
} }
public int Insert(T obj, IEqualityComparer<T>? comparer = null)
public int Insert(T obj, IEqualityComparer<DrawningTrackedVehicle>? comparer = null)
{ {
// TODO выброс ошибки, если такой объект есть в коллекции try
if (comparer != null && list.Contains(obj))
{ {
throw new ObjectIsPresentInTheCollectionException(obj); if (list.Contains(obj, comparer)) throw new ObjectIsPresentInTheCollectionException(Count);
} if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (list.Count >= _maxCount)
{
throw new CollectionOverflowException(_maxCount);
}
list.Add(obj); list.Add(obj);
return _maxCount; return Count - 1;
}
catch (ObjectIsPresentInTheCollectionException ex)
{
MessageBox.Show(ex.Message);
return -1;
}
}
public int Insert(T obj, int position, IEqualityComparer<T>? comparer = null)
{
try
{
if (comparer != null && list.Contains(obj, comparer))
{
throw new ObjectIsPresentInTheCollectionException(Count);
} }
public int Insert(T obj, int position, IEqualityComparer<DrawningTrackedVehicle>? comparer = null) if (Count == _maxCount) throw new CollectionOverflowException(Count);
{
if (comparer != null && list.Contains(obj))
{
throw new ObjectIsPresentInTheCollectionException();
}
if (Count >= _maxCount)
throw new CollectionOverflowException(_maxCount);
if (position < 0 || position >= _maxCount) if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
throw new PositionOutOfCollectionException(position);
list.Insert(position, obj); list.Insert(position, obj);
return position; return position;
} }
catch (ObjectIsPresentInTheCollectionException ex)
{
Console.WriteLine(ex.Message);
return -1;
}
}
public T? Remove(int position) public T? Remove(int position)
{ {

View File

@ -1,9 +1,10 @@
using HoistingCrane.Drawning; using HoistingCrane.Drawning;
using HoistingCrane.Exceptions; using HoistingCrane.Exceptions;
using System.Linq;
namespace HoistingCrane.CollectionGenericObjects namespace HoistingCrane.CollectionGenericObjects
{ {
public class MassivGenericObjects<T> : ICollectionGenericObjects<T> where T : class public class MassivGenericObjects<T> : ICollectionGenericObjects<T> where T : DrawningTrackedVehicle
{ {
private T?[] arr; private T?[] arr;
public MassivGenericObjects() public MassivGenericObjects()
@ -43,69 +44,84 @@ namespace HoistingCrane.CollectionGenericObjects
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position);
return arr[position]; return arr[position];
} }
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
public int Insert(T obj, IEqualityComparer<DrawningTrackedVehicle>? comparer = null)
{ {
if (comparer != null) try
{ {
foreach (T? item in arr) if (arr.Contains(obj, comparer))
{ {
if ((comparer as IEqualityComparer<DrawningTrackedVehicle>).Equals(obj as DrawningTrackedVehicle, item as DrawningTrackedVehicle))
throw new ObjectIsPresentInTheCollectionException(); throw new ObjectIsPresentInTheCollectionException();
} }
} for (int i = 0; i < Count; ++i)
int index = 0;
while (index < arr.Length)
{ {
if (arr[index] == null) if (arr[i] == null)
{ {
arr[index] = obj; arr[i] = obj;
return index; return i;
} }
index++;
} }
throw new CollectionOverflowException(Count); throw new CollectionOverflowException(Count);
} }
public int Insert(T obj, int position, IEqualityComparer<DrawningTrackedVehicle>? comparer = null) catch (ObjectIsPresentInTheCollectionException ex)
{ {
MessageBox.Show(ex.Message);
return -1;
}
}
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
try
{
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
if (comparer != null) if (comparer != null)
{ {
foreach (T? item in arr) for (int i = 0; i < Count; i++)
{
if (comparer.Equals(arr[i], obj))
{ {
if ((comparer as IEqualityComparer<DrawningTrackedVehicle>).Equals(obj as DrawningTrackedVehicle, item as DrawningTrackedVehicle))
throw new ObjectIsPresentInTheCollectionException(); throw new ObjectIsPresentInTheCollectionException();
} }
} }
}
if (position >= arr.Length || position < 0) throw new PositionOutOfCollectionException(position);
if (arr[position] == null) if (arr[position] == null)
{ {
arr[position] = obj; arr[position] = obj;
return position; return position;
} }
int index = position + 1; else
while (index < arr.Length)
{ {
if (arr[index] == null) for (int i = 1; i < Count; ++i)
{ {
arr[index] = obj; if (arr[position + i] == null)
return index; {
arr[position + i] = obj;
return position + i;
} }
index++; for (i = position - 1; i >= 0; i--)
}
index = position - 1;
while (index >= 0)
{ {
if (arr[index] == null) if (arr[i] == null)
{ {
arr[index] = obj; arr[i] = obj;
return index; return i;
}
}
} }
index--;
} }
throw new CollectionOverflowException(Count); throw new CollectionOverflowException(Count);
} }
catch (PositionOutOfCollectionException ex)
{
MessageBox.Show(ex.Message);
return -1;
}
catch (ObjectIsPresentInTheCollectionException ex)
{
MessageBox.Show(ex.Message);
return -1;
}
}
public T? Remove(int position) public T? Remove(int position)
{ {
@ -127,6 +143,7 @@ namespace HoistingCrane.CollectionGenericObjects
{ {
T[] notNullArr = arr.OfType<T>().ToArray(); T[] notNullArr = arr.OfType<T>().ToArray();
Array.Sort(notNullArr, comparer); Array.Sort(notNullArr, comparer);
Array.Copy(notNullArr, 0, arr, 0, notNullArr.Length);
} }
} }
} }

View File

@ -4,11 +4,10 @@
{ {
public int Compare(DrawningTrackedVehicle? x, DrawningTrackedVehicle? y) public int Compare(DrawningTrackedVehicle? x, DrawningTrackedVehicle? y)
{ {
//TODO: Прописать логику сравнения по цветам, скорости и весу
if (x == null || x.EntityTrackedVehicle == null) return -1; if (x == null || x.EntityTrackedVehicle == null) return -1;
if (y == null || y.EntityTrackedVehicle == null) return 1; if (y == null || y.EntityTrackedVehicle == null) return 1;
var colorCompare = x.EntityTrackedVehicle.BodyColor.ToArgb().CompareTo(y.EntityTrackedVehicle.BodyColor.ToArgb()); if (x.EntityTrackedVehicle.BodyColor.Name != y.EntityTrackedVehicle.BodyColor.Name)
if (colorCompare != 0) return colorCompare; return x.EntityTrackedVehicle.BodyColor.Name.CompareTo(y.EntityTrackedVehicle.BodyColor.Name);
var speedCompare = x.EntityTrackedVehicle.Speed.CompareTo(y.EntityTrackedVehicle.Speed); var speedCompare = x.EntityTrackedVehicle.Speed.CompareTo(y.EntityTrackedVehicle.Speed);
if (speedCompare != 0) return speedCompare; if (speedCompare != 0) return speedCompare;
return x.EntityTrackedVehicle.Weight.CompareTo(y.EntityTrackedVehicle.Weight); return x.EntityTrackedVehicle.Weight.CompareTo(y.EntityTrackedVehicle.Weight);

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HoistingCrane.Drawning
{
internal class StorageEqutables
{
}
}

View File

@ -1,17 +1,11 @@
using System; using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace HoistingCrane.Exceptions namespace HoistingCrane.Exceptions
{ {
[Serializable]
public class ObjectIsPresentInTheCollectionException : ApplicationException public class ObjectIsPresentInTheCollectionException : ApplicationException
{ {
public ObjectIsPresentInTheCollectionException(object obj) : base("В коллекции уже присустствует объект " + obj) { } public ObjectIsPresentInTheCollectionException(int objName) : base("В коллекции уже присустствует объект " + objName) { }
public ObjectIsPresentInTheCollectionException() : base() { } public ObjectIsPresentInTheCollectionException() : base() { }
public ObjectIsPresentInTheCollectionException(string message) : base(message) { }
public ObjectIsPresentInTheCollectionException(string message, Exception exception) : base(message, exception) { } public ObjectIsPresentInTheCollectionException(string message, Exception exception) : base(message, exception) { }
protected ObjectIsPresentInTheCollectionException(SerializationInfo info, StreamingContext context) : base(info, context) { } protected ObjectIsPresentInTheCollectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
} }

View File

@ -43,8 +43,8 @@ namespace HoistingCrane
} }
else else
{ {
MessageBox.Show("Не удалось добавить объект"); //MessageBox.Show("Не удалось добавить объект");
logger.LogInformation("Не удалось добавить корабль {ship} в коллекцию", drawningTrackedVehicle.GetType().Name); logger.LogInformation("Не удалось добавить кран {crane} в коллекцию", drawningTrackedVehicle.GetType().Name);
} }
} }
catch (CollectionOverflowException ex) catch (CollectionOverflowException ex)

View File

@ -13,6 +13,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.9" /> <PackageReference Include="NLog.Extensions.Logging" Version="5.3.9" />
<PackageReference Include="Serilog" Version="3.1.1" /> <PackageReference Include="Serilog" Version="3.1.1" />

View File

@ -1,8 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Serilog; using Serilog;
using Serilog.Extensions.Logging;
namespace HoistingCrane namespace HoistingCrane
{ {
internal static class Program internal static class Program
@ -18,11 +17,24 @@ namespace HoistingCrane
} }
private static void ConfigureServices(ServiceCollection services) private static void ConfigureServices(ServiceCollection services)
{ {
// Èíèöèàëèçèðóåì Serilog
Log.Logger = new LoggerConfiguration().WriteTo.File("E:\\myLog.log").CreateLogger(); string[] path = Directory.GetCurrentDirectory().Split('\\');
services.AddSingleton<FormCarCollection>().AddLogging(builder => string pathNeed = "";
for (int i = 0; i < path.Length - 3; i++)
{ {
builder.AddSerilog(); pathNeed += path[i] + "\\";
}
services.AddSingleton<FormCarCollection>()
.AddLogging(option =>
{
option.SetMinimumLevel(LogLevel.Information);
option.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(new ConfigurationBuilder()
.AddJsonFile($"{pathNeed}serilog.json")
.Build())
.CreateLogger());
}); });
} }
} }

View File

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