diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs index 94b5609..9608211 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs @@ -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 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() diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs index 1fb465a..02a42a6 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -19,7 +19,7 @@ namespace HoistingCrane.CollectionGenericObjects /// /// /// Сравнение двух объектов /// - int Insert(T obj, IEqualityComparer? comparer = null); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление элемента в коллекцию на определенную позицию /// @@ -27,7 +27,7 @@ namespace HoistingCrane.CollectionGenericObjects /// /// Сравнение двух объектов /// - int Insert(T obj, int position, IEqualityComparer? comparer = null); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление элемента из коллекции по его позиции /// diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs index 66feeeb..99d6afb 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs @@ -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? comparer = null) + public int Insert(T obj, IEqualityComparer? 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? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? 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) diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs index f96e396..316bd4c 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs @@ -1,9 +1,10 @@ using HoistingCrane.Drawning; using HoistingCrane.Exceptions; +using System.Linq; namespace HoistingCrane.CollectionGenericObjects { - public class MassivGenericObjects : ICollectionGenericObjects where T : class + public class MassivGenericObjects : ICollectionGenericObjects 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? comparer = null) + public int Insert(T obj, IEqualityComparer? comparer = null) { - if (comparer != null) + try { - foreach (T? item in arr) + if (arr.Contains(obj, comparer)) { - if ((comparer as IEqualityComparer).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? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? 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).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().ToArray(); Array.Sort(notNullArr, comparer); + Array.Copy(notNullArr, 0, arr, 0, notNullArr.Length); } } } diff --git a/HoistingCrane/HoistingCrane/Drawning/DrawningCraneCompareByColor.cs b/HoistingCrane/HoistingCrane/Drawning/DrawningCraneCompareByColor.cs index 8e76d17..9902054 100644 --- a/HoistingCrane/HoistingCrane/Drawning/DrawningCraneCompareByColor.cs +++ b/HoistingCrane/HoistingCrane/Drawning/DrawningCraneCompareByColor.cs @@ -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); diff --git a/HoistingCrane/HoistingCrane/Drawning/StorageEqutables.cs b/HoistingCrane/HoistingCrane/Drawning/StorageEqutables.cs new file mode 100644 index 0000000..c4788f2 --- /dev/null +++ b/HoistingCrane/HoistingCrane/Drawning/StorageEqutables.cs @@ -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 + { + } +} diff --git a/HoistingCrane/HoistingCrane/Exceptions/ObjectIsPresentInTheCollectionException.cs b/HoistingCrane/HoistingCrane/Exceptions/ObjectIsPresentInTheCollectionException.cs index d659852..1175102 100644 --- a/HoistingCrane/HoistingCrane/Exceptions/ObjectIsPresentInTheCollectionException.cs +++ b/HoistingCrane/HoistingCrane/Exceptions/ObjectIsPresentInTheCollectionException.cs @@ -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) { } } diff --git a/HoistingCrane/HoistingCrane/FormCarCollection.cs b/HoistingCrane/HoistingCrane/FormCarCollection.cs index b4cf3d6..3287d4b 100644 --- a/HoistingCrane/HoistingCrane/FormCarCollection.cs +++ b/HoistingCrane/HoistingCrane/FormCarCollection.cs @@ -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) diff --git a/HoistingCrane/HoistingCrane/HoistingCrane.csproj b/HoistingCrane/HoistingCrane/HoistingCrane.csproj index 9b3b27e..780fadb 100644 --- a/HoistingCrane/HoistingCrane/HoistingCrane.csproj +++ b/HoistingCrane/HoistingCrane/HoistingCrane.csproj @@ -13,6 +13,7 @@ + diff --git a/HoistingCrane/HoistingCrane/Program.cs b/HoistingCrane/HoistingCrane/Program.cs index 562ee2d..b2baa34 100644 --- a/HoistingCrane/HoistingCrane/Program.cs +++ b/HoistingCrane/HoistingCrane/Program.cs @@ -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,12 +17,25 @@ namespace HoistingCrane } private static void ConfigureServices(ServiceCollection services) { - // Serilog - Log.Logger = new LoggerConfiguration().WriteTo.File("E:\\myLog.log").CreateLogger(); - services.AddSingleton().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() + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder() + .AddJsonFile($"{pathNeed}serilog.json") + .Build()) + .CreateLogger()); }); } } -} +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/serilog.json b/HoistingCrane/HoistingCrane/serilog.json new file mode 100644 index 0000000..6660c8d --- /dev/null +++ b/HoistingCrane/HoistingCrane/serilog.json @@ -0,0 +1,17 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log.log" + } + } + ], + "Properties": { + "Application": "Sample" + } + } +} \ No newline at end of file