Готовая лабораторная работа 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
{
return (pictureWidth * pictureHeight) / (_placeSizeHeight * _placeSizeWidth)-2;
return (pictureWidth / _placeSizeWidth) * (pictureHeight * _placeSizeHeight);
}
}
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTrackedVehicle> array)
@ -42,11 +42,11 @@ namespace HoistingCrane.CollectionGenericObjects
}
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)
{
return company.arr?.Remove(position) ?? null;
return company.arr?.Remove(position);
}
public DrawningTrackedVehicle? GetRandomObject()

View File

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

View File

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

View File

@ -1,9 +1,10 @@
using HoistingCrane.Drawning;
using HoistingCrane.Exceptions;
using System.Linq;
namespace HoistingCrane.CollectionGenericObjects
{
public class MassivGenericObjects<T> : ICollectionGenericObjects<T> where T : class
public class MassivGenericObjects<T> : ICollectionGenericObjects<T> where T : DrawningTrackedVehicle
{
private T?[] arr;
public MassivGenericObjects()
@ -43,68 +44,83 @@ namespace HoistingCrane.CollectionGenericObjects
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position);
return arr[position];
}
public int Insert(T obj, IEqualityComparer<DrawningTrackedVehicle>? comparer = null)
public int Insert(T obj, IEqualityComparer<T?>? 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)
{
if (arr[i] == null)
{
arr[i] = obj;
return i;
}
}
throw new CollectionOverflowException(Count);
}
int index = 0;
while (index < arr.Length)
catch (ObjectIsPresentInTheCollectionException ex)
{
if (arr[index] == null)
{
arr[index] = obj;
return index;
}
index++;
MessageBox.Show(ex.Message);
return -1;
}
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position, IEqualityComparer<DrawningTrackedVehicle>? comparer = null)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
if (comparer != null)
try
{
foreach (T? item in arr)
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
if (comparer != null)
{
if ((comparer as IEqualityComparer<DrawningTrackedVehicle>).Equals(obj as DrawningTrackedVehicle, item as DrawningTrackedVehicle))
throw new ObjectIsPresentInTheCollectionException();
for (int i = 0; i < Count; i++)
{
if (comparer.Equals(arr[i], obj))
{
throw new ObjectIsPresentInTheCollectionException();
}
}
}
}
if (position >= arr.Length || position < 0) throw new PositionOutOfCollectionException(position);
if (arr[position] == null)
{
arr[position] = obj;
return position;
}
int index = position + 1;
while (index < arr.Length)
{
if (arr[index] == null)
if (arr[position] == null)
{
arr[index] = obj;
return index;
arr[position] = obj;
return position;
}
index++;
}
index = position - 1;
while (index >= 0)
{
if (arr[index] == null)
else
{
arr[index] = obj;
return index;
for (int i = 1; i < Count; ++i)
{
if (arr[position + i] == null)
{
arr[position + i] = obj;
return position + i;
}
for (i = position - 1; i >= 0; i--)
{
if (arr[i] == null)
{
arr[i] = obj;
return i;
}
}
}
}
index--;
throw new CollectionOverflowException(Count);
}
catch (PositionOutOfCollectionException ex)
{
MessageBox.Show(ex.Message);
return -1;
}
catch (ObjectIsPresentInTheCollectionException ex)
{
MessageBox.Show(ex.Message);
return -1;
}
throw new CollectionOverflowException(Count);
}
public T? Remove(int position)
@ -127,6 +143,7 @@ namespace HoistingCrane.CollectionGenericObjects
{
T[] notNullArr = arr.OfType<T>().ToArray();
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)
{
//TODO: Прописать логику сравнения по цветам, скорости и весу
if (x == null || x.EntityTrackedVehicle == null) return -1;
if (y == null || y.EntityTrackedVehicle == null) return 1;
var colorCompare = x.EntityTrackedVehicle.BodyColor.ToArgb().CompareTo(y.EntityTrackedVehicle.BodyColor.ToArgb());
if (colorCompare != 0) return colorCompare;
if (x.EntityTrackedVehicle.BodyColor.Name != y.EntityTrackedVehicle.BodyColor.Name)
return x.EntityTrackedVehicle.BodyColor.Name.CompareTo(y.EntityTrackedVehicle.BodyColor.Name);
var speedCompare = x.EntityTrackedVehicle.Speed.CompareTo(y.EntityTrackedVehicle.Speed);
if (speedCompare != 0) return speedCompare;
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.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace HoistingCrane.Exceptions
{
[Serializable]
public class ObjectIsPresentInTheCollectionException : ApplicationException
{
public ObjectIsPresentInTheCollectionException(object obj) : base("В коллекции уже присустствует объект " + obj) { }
public ObjectIsPresentInTheCollectionException(int objName) : base("В коллекции уже присустствует объект " + objName) { }
public ObjectIsPresentInTheCollectionException() : base() { }
public ObjectIsPresentInTheCollectionException(string message) : base(message) { }
public ObjectIsPresentInTheCollectionException(string message, Exception exception) : base(message, exception) { }
protected ObjectIsPresentInTheCollectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

View File

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

View File

@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<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.9" />
<PackageReference Include="Serilog" Version="3.1.1" />

View File

@ -1,8 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Extensions.Logging;
namespace HoistingCrane
{
internal static class Program
@ -18,11 +17,24 @@ namespace HoistingCrane
}
private static void ConfigureServices(ServiceCollection services)
{
// Èíèöèàëèçèðóåì Serilog
Log.Logger = new LoggerConfiguration().WriteTo.File("E:\\myLog.log").CreateLogger();
services.AddSingleton<FormCarCollection>().AddLogging(builder =>
string[] path = Directory.GetCurrentDirectory().Split('\\');
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"
}
}
}