From 1cf8894318d5e124155a8fdfdcb8689254f2859c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Wed, 19 Apr 2023 17:16:14 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20+?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AutomobilePlant/AutomobilePlant.sln | 12 ++- .../AutomomilePlantFileImplement.csproj | 14 +++ .../DataFileSingleton.cs | 62 +++++++++++ .../Models/Car.cs | 100 ++++++++++++++++++ .../Models/Component.cs | 65 ++++++++++++ .../Models/Order.cs | 100 ++++++++++++++++++ 6 files changed, 350 insertions(+), 3 deletions(-) create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/AutomomilePlantFileImplement.csproj create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Models/Car.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Models/Component.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Models/Order.cs diff --git a/AutomobilePlant/AutomobilePlant.sln b/AutomobilePlant/AutomobilePlant.sln index e1f3e29..97cd30b 100644 --- a/AutomobilePlant/AutomobilePlant.sln +++ b/AutomobilePlant/AutomobilePlant.sln @@ -7,11 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantDataModels", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantContracts", "AbstractAutoContracts\AutomobilePlantContracts.csproj", "{4B515980-8AD9-48F3-886E-3C2A6B1E9D90}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantBusinessLogic", "AbstractAutoBusinessLogic\AutomobilePlantBusinessLogic.csproj", "{D57C726F-E5B4-4CB3-A754-D5F93295CE1E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantBusinessLogic", "AbstractAutoBusinessLogic\AutomobilePlantBusinessLogic.csproj", "{D57C726F-E5B4-4CB3-A754-D5F93295CE1E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantListImplement", "AbstractAutoListImplement\AutomobilePlantListImplement.csproj", "{305978E5-BFB5-47B0-94C9-2C5D06748BD6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantListImplement", "AbstractAutoListImplement\AutomobilePlantListImplement.csproj", "{305978E5-BFB5-47B0-94C9-2C5D06748BD6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlant", "AutomobilePlant\AutomobilePlant.csproj", "{8DB13E4F-D083-40DD-B1C2-F3CF74B762C2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlant", "AutomobilePlant\AutomobilePlant.csproj", "{8DB13E4F-D083-40DD-B1C2-F3CF74B762C2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomomilePlantFileImplement", "AutomomilePlantFileImplement\AutomomilePlantFileImplement.csproj", "{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {8DB13E4F-D083-40DD-B1C2-F3CF74B762C2}.Debug|Any CPU.Build.0 = Debug|Any CPU {8DB13E4F-D083-40DD-B1C2-F3CF74B762C2}.Release|Any CPU.ActiveCfg = Release|Any CPU {8DB13E4F-D083-40DD-B1C2-F3CF74B762C2}.Release|Any CPU.Build.0 = Release|Any CPU + {3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AutomobilePlant/AutomomilePlantFileImplement/AutomomilePlantFileImplement.csproj b/AutomobilePlant/AutomomilePlantFileImplement/AutomomilePlantFileImplement.csproj new file mode 100644 index 0000000..43870e5 --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/AutomomilePlantFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs b/AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..65f994f --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs @@ -0,0 +1,62 @@ +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomomilePlantFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string CarFileName = "Car.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Cars { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, + "Components", x => x.GetXElement); + public void SaveCars() => SaveData(Cars, CarFileName, + "Cars", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x + => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => + Component.Create(x)!)!; + Cars = LoadData(CarFileName, "Car", x => + Car.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, + Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string + xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, + data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Models/Car.cs b/AutomobilePlant/AutomomilePlantFileImplement/Models/Car.cs new file mode 100644 index 0000000..7c0616c --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Models/Car.cs @@ -0,0 +1,100 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomomilePlantFileImplement.Models +{ + public class Car : ICarModel + { + public int Id { get; private set; } + public string CarName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _carComponents = + null; + public Dictionary CarComponents + { + get + { + if (_carComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _carComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _carComponents; + } + } + public static Car? Create(CarBindingModel model) + { + if (model == null) + { + return null; + } + return new Car() + { + Id = model.Id, + CarName = model.CarName, + Price = model.Price, + Components = model.CarComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Car? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Car() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + CarName = element.Element("CarName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = + element.Element("CarComponents")!.Elements("CarComponent") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(CarBindingModel model) + { + if (model == null) + { + return; + } + CarName = model.CarName; + Price = model.Price; + Components = model.CarComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _carComponents = null; + } + public CarViewModel GetViewModel => new() + { + Id = Id, + CarName = CarName, + Price = Price, + CarComponents = CarComponents + }; + public XElement GetXElement => new("Car", + new XAttribute("Id", Id), + new XElement("CarName", CarName), + new XElement("Price", Price.ToString()), + new XElement("CarComponents", Components.Select(x => + new XElement("CarComponent", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + } +} diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Models/Component.cs b/AutomobilePlant/AutomomilePlantFileImplement/Models/Component.cs new file mode 100644 index 0000000..86cc04b --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Models/Component.cs @@ -0,0 +1,65 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomomilePlantFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString()) + ); + } +} diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Models/Order.cs b/AutomobilePlant/AutomomilePlantFileImplement/Models/Order.cs new file mode 100644 index 0000000..e5b86e3 --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Models/Order.cs @@ -0,0 +1,100 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomomilePlantFileImplement.Models +{ + public class Order : IOrderModel + { + public int CarId { get; private set; } + + public string CarName { get; private set; } = string.Empty; + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } = null; + + public int Id { get; private set; } + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + CarId = model.CarId, + CarName = model.CarName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + CarId = Convert.ToInt32(element.Element("CarId")!.Value), + CarName= element.Element("CarName")!.Value, + Count= Convert.ToInt32(element.Element("Count")!.Value), + Sum= Convert.ToDouble(element.Element("Sum")!.Value), + Status= (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate=DateTime.Parse(element.Element("DateCreate")!.Value), + DateImplement= string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : DateTime.Parse(element.Element("DateImplement")!.Value) + + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + CarId = CarId, + CarName = CarName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("CarId", CarId.ToString()), + new XElement("CarName", CarName), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +} -- 2.25.1 From 07dc91676ceddd71a241fa2fc5650a0de89828f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Wed, 19 Apr 2023 17:22:41 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AutomobilePlant/AutomobilePlant.csproj | 2 +- AutomobilePlant/AutomobilePlant/Program.cs | 2 +- .../Implements/CarStorage.cs | 94 +++++++++++++++++++ .../Implements/ComponentStorage.cs | 90 ++++++++++++++++++ .../Implements/OrderStorage.cs | 92 ++++++++++++++++++ 5 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs diff --git a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj index b9586a3..b635bae 100644 --- a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj +++ b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj @@ -24,7 +24,7 @@ - + \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/Program.cs b/AutomobilePlant/AutomobilePlant/Program.cs index f8f9e03..8a8efc6 100644 --- a/AutomobilePlant/AutomobilePlant/Program.cs +++ b/AutomobilePlant/AutomobilePlant/Program.cs @@ -1,7 +1,7 @@ using AutomobilePlantBusinessLogic.BusinessLogics; using AutomobilePlantContracts.BusinessLogicsContracts; using AutomobilePlantContracts.StoragesContracts; -using AutomobilePlantListImplement.Implements; +using AutomomilePlantFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs new file mode 100644 index 0000000..78d430f --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs @@ -0,0 +1,94 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomomilePlantFileImplement.Implements +{ + public class CarStorage : ICarStorage + { + private readonly DataFileSingleton source; + public CarStorage() + { + source = DataFileSingleton.GetInstance(); + } + public CarViewModel? Delete(CarBindingModel model) + { + var element = source.Cars.FirstOrDefault(x => x.Id == + model.Id); + if (element != null) + { + source.Cars.Remove(element); + source.SaveCars(); + return element.GetViewModel; + } + return null; + } + + public CarViewModel? GetElement(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue) + { + return null; + } + return source.Cars + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.CarName) && x.CarName == + model.CarName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName)) + { + return new(); + } + return source.Cars + .Where(x => x.CarName.Contains(model.CarName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Cars + .Select(x => x.GetViewModel) + .ToList(); + } + + public CarViewModel? Insert(CarBindingModel model) + { + model.Id = source.Cars.Count > 0 ? source.Cars.Max(x => + x.Id) + 1 : 1; + var newCar = Car.Create(model); + if (newCar == null) + { + return null; + } + source.Cars.Add(newCar); + source.SaveCars(); + return newCar.GetViewModel; + } + + public CarViewModel? Update(CarBindingModel model) + { + var car = source.Cars.FirstOrDefault(x => x.Id == + model.Id); + if (car == null) + { + return null; + } + car.Update(model); + source.SaveCars(); + return car.GetViewModel; + } + } +} diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..f12c9c4 --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,90 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomomilePlantFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == + model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => + x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == + model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..e54c17a --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,92 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomomilePlantFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Orders + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => + x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + } +} -- 2.25.1 From 3923eacebdcd121a8d0683fee27f5507be7bf46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Mon, 1 May 2023 14:18:15 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/CarShopBindingModel.cs | 1 + .../StoragesContracts/ICarShopStorage.cs | 3 + .../ViewModel/CarShopViewModel.cs | 2 + .../AbstractAutoDataModels/Models/ICarShop.cs | 1 + .../Implements/CarShopStorege.cs | 11 ++ .../Models/CarShop.cs | 1 + .../AutomobilePlant/FormShopCar.Designer.cs | 80 +++++++----- .../AutomobilePlant/FormShopCar.cs | 1 + AutomobilePlant/AutomobilePlant/Program.cs | 2 +- .../DataFileSingleton.cs | 5 + .../Implements/CarShopStorage.cs | 96 +++++++++++++++ .../Models/CarShop.cs | 116 ++++++++++++++++++ 12 files changed, 290 insertions(+), 29 deletions(-) create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs diff --git a/AutomobilePlant/AbstractAutoContracts/BindingModels/CarShopBindingModel.cs b/AutomobilePlant/AbstractAutoContracts/BindingModels/CarShopBindingModel.cs index 1e0026b..f803cb3 100644 --- a/AutomobilePlant/AbstractAutoContracts/BindingModels/CarShopBindingModel.cs +++ b/AutomobilePlant/AbstractAutoContracts/BindingModels/CarShopBindingModel.cs @@ -14,6 +14,7 @@ namespace AutomobilePlantContracts.BindingModels public string Adress { get; set; } = String.Empty ; public DateTime DateOpen { get; set; } = new(); + public int Fullness { get; set; } public Dictionary Cars { get; set; } = new(); diff --git a/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs b/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs index 51504b2..1cc791c 100644 --- a/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs +++ b/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs @@ -1,6 +1,7 @@ using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.SearchModel; using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -13,6 +14,8 @@ namespace AutomobilePlantContracts.StoragesContracts { List GetFullList(); List GetFilteredList(CarShopSearchModel model); + bool CheckShop (CarShopSearchModel model); + bool TrySell (CarShopSearchModel model, ICarModel car); CarShopViewModel? GetElement(CarShopSearchModel model); CarShopViewModel? Insert(CarShopBindingModel model); CarShopViewModel? Update(CarShopBindingModel model); diff --git a/AutomobilePlant/AbstractAutoContracts/ViewModel/CarShopViewModel.cs b/AutomobilePlant/AbstractAutoContracts/ViewModel/CarShopViewModel.cs index fc409af..d0dba5f 100644 --- a/AutomobilePlant/AbstractAutoContracts/ViewModel/CarShopViewModel.cs +++ b/AutomobilePlant/AbstractAutoContracts/ViewModel/CarShopViewModel.cs @@ -16,6 +16,8 @@ namespace AutomobilePlantContracts.ViewModel public string Adress { get; set; } = String.Empty; [DisplayName("Дата открытия")] public DateTime DateOpen { get; set; } = new(); + [DisplayName("Наполненность")] + public int Fullness { get; set; } public Dictionary Cars { get; set; } = new(); diff --git a/AutomobilePlant/AbstractAutoDataModels/Models/ICarShop.cs b/AutomobilePlant/AbstractAutoDataModels/Models/ICarShop.cs index 38d80ea..f75a89f 100644 --- a/AutomobilePlant/AbstractAutoDataModels/Models/ICarShop.cs +++ b/AutomobilePlant/AbstractAutoDataModels/Models/ICarShop.cs @@ -11,6 +11,7 @@ namespace AutomobilePlantDataModels.Models string ShopName { get; } string Adress { get; } DateTime DateOpen { get; } + int Fullness { get; } Dictionary Cars { get; } } } diff --git a/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs b/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs index 7f54820..8077855 100644 --- a/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs +++ b/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs @@ -2,6 +2,7 @@ using AutomobilePlantContracts.SearchModel; using AutomobilePlantContracts.StoragesContracts; using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Models; using AutomobilePlantListImplement.Models; using System; using System.Collections.Generic; @@ -117,5 +118,15 @@ namespace AutomobilePlantListImplement.Implements return null; } + + public bool CheckShop(CarShopSearchModel model) + { + throw new NotImplementedException(); + } + + public bool TrySell(CarShopSearchModel model, ICarModel car) + { + throw new NotImplementedException(); + } } } diff --git a/AutomobilePlant/AbstractAutoListImplement/Models/CarShop.cs b/AutomobilePlant/AbstractAutoListImplement/Models/CarShop.cs index 31cf0cc..005394b 100644 --- a/AutomobilePlant/AbstractAutoListImplement/Models/CarShop.cs +++ b/AutomobilePlant/AbstractAutoListImplement/Models/CarShop.cs @@ -18,6 +18,7 @@ namespace AutomobilePlantListImplement.Models public string Adress { get; private set; } = String.Empty; public DateTime DateOpen { get; private set; } + public int Fullness { get; set; } public Dictionary Cars { get; private set; } = new (); diff --git a/AutomobilePlant/AutomobilePlant/FormShopCar.Designer.cs b/AutomobilePlant/AutomobilePlant/FormShopCar.Designer.cs index 38ae461..ae029dc 100644 --- a/AutomobilePlant/AutomobilePlant/FormShopCar.Designer.cs +++ b/AutomobilePlant/AutomobilePlant/FormShopCar.Designer.cs @@ -28,9 +28,9 @@ /// private void InitializeComponent() { - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle(); this.ShopNameLabel = new System.Windows.Forms.Label(); this.AdressLabel = new System.Windows.Forms.Label(); this.ShopNameTextBox = new System.Windows.Forms.TextBox(); @@ -44,8 +44,11 @@ this.ComponentNameField = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.CountField = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.DateOpenPicker = new System.Windows.Forms.DateTimePicker(); + this.FullnessnumericUpDown = new System.Windows.Forms.NumericUpDown(); + this.Fullness = new System.Windows.Forms.Label(); this.ComponentsGroupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.FullnessnumericUpDown)).BeginInit(); this.SuspendLayout(); // // ShopNameLabel @@ -132,38 +135,38 @@ // // DataGridView // - dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; - dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control; - dataGridViewCellStyle1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText; - dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight; - dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText; - dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True; - this.DataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; + dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle4.BackColor = System.Drawing.SystemColors.Control; + dataGridViewCellStyle4.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.WindowText; + dataGridViewCellStyle4.SelectionBackColor = System.Drawing.SystemColors.Highlight; + dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.HighlightText; + dataGridViewCellStyle4.WrapMode = System.Windows.Forms.DataGridViewTriState.True; + this.DataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle4; this.DataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.DataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.ID, this.ComponentNameField, this.CountField}); - dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; - dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window; - dataGridViewCellStyle2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText; - dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight; - dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText; - dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False; - this.DataGridView.DefaultCellStyle = dataGridViewCellStyle2; + dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle5.BackColor = System.Drawing.SystemColors.Window; + dataGridViewCellStyle5.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + dataGridViewCellStyle5.ForeColor = System.Drawing.SystemColors.ControlText; + dataGridViewCellStyle5.SelectionBackColor = System.Drawing.SystemColors.Highlight; + dataGridViewCellStyle5.SelectionForeColor = System.Drawing.SystemColors.HighlightText; + dataGridViewCellStyle5.WrapMode = System.Windows.Forms.DataGridViewTriState.False; + this.DataGridView.DefaultCellStyle = dataGridViewCellStyle5; this.DataGridView.Location = new System.Drawing.Point(7, 29); this.DataGridView.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.DataGridView.Name = "DataGridView"; - dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; - dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control; - dataGridViewCellStyle3.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText; - dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight; - dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText; - dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True; - this.DataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle3; + dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle6.BackColor = System.Drawing.SystemColors.Control; + dataGridViewCellStyle6.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + dataGridViewCellStyle6.ForeColor = System.Drawing.SystemColors.WindowText; + dataGridViewCellStyle6.SelectionBackColor = System.Drawing.SystemColors.Highlight; + dataGridViewCellStyle6.SelectionForeColor = System.Drawing.SystemColors.HighlightText; + dataGridViewCellStyle6.WrapMode = System.Windows.Forms.DataGridViewTriState.True; + this.DataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle6; this.DataGridView.RowHeadersWidth = 51; this.DataGridView.RowTemplate.Height = 25; this.DataGridView.Size = new System.Drawing.Size(641, 429); @@ -193,16 +196,34 @@ // // DateOpenPicker // - this.DateOpenPicker.Location = new System.Drawing.Point(471, 8); + this.DateOpenPicker.Location = new System.Drawing.Point(495, 12); this.DateOpenPicker.Name = "DateOpenPicker"; this.DateOpenPicker.Size = new System.Drawing.Size(250, 27); this.DateOpenPicker.TabIndex = 6; // + // FullnessnumericUpDown + // + this.FullnessnumericUpDown.Location = new System.Drawing.Point(595, 57); + this.FullnessnumericUpDown.Name = "FullnessnumericUpDown"; + this.FullnessnumericUpDown.Size = new System.Drawing.Size(150, 27); + this.FullnessnumericUpDown.TabIndex = 7; + // + // Fullness + // + this.Fullness.AutoSize = true; + this.Fullness.Location = new System.Drawing.Point(469, 64); + this.Fullness.Name = "Fullness"; + this.Fullness.Size = new System.Drawing.Size(103, 20); + this.Fullness.TabIndex = 8; + this.Fullness.Text = "Ограничение"; + // // FormShopCar // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(853, 664); + this.Controls.Add(this.Fullness); + this.Controls.Add(this.FullnessnumericUpDown); this.Controls.Add(this.DateOpenPicker); this.Controls.Add(this.ComponentsGroupBox); this.Controls.Add(this.AdressTextBox); @@ -215,6 +236,7 @@ this.Load += new System.EventHandler(this.FormCar_Load); this.ComponentsGroupBox.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.FullnessnumericUpDown)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -235,5 +257,7 @@ private DataGridViewTextBoxColumn ComponentNameField; private DataGridViewTextBoxColumn CountField; private DateTimePicker DateOpenPicker; + private NumericUpDown FullnessnumericUpDown; + private Label Fullness; } } \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/FormShopCar.cs b/AutomobilePlant/AutomobilePlant/FormShopCar.cs index ba18cef..1b24873 100644 --- a/AutomobilePlant/AutomobilePlant/FormShopCar.cs +++ b/AutomobilePlant/AutomobilePlant/FormShopCar.cs @@ -110,6 +110,7 @@ namespace AutomobilePlant ShopName = ShopNameTextBox.Text, DateOpen = DateOpenPicker.Value, Adress = AdressTextBox.Text, + Fullness = (int)FullnessnumericUpDown.Value, Cars = _cars }; var operationResult = _id.HasValue ? _logic.Update(model) : diff --git a/AutomobilePlant/AutomobilePlant/Program.cs b/AutomobilePlant/AutomobilePlant/Program.cs index 187e842..20f4ab1 100644 --- a/AutomobilePlant/AutomobilePlant/Program.cs +++ b/AutomobilePlant/AutomobilePlant/Program.cs @@ -37,7 +37,7 @@ namespace AutomobilePlant services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs b/AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs index 65f994f..217b935 100644 --- a/AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs +++ b/AutomobilePlant/AutomomilePlantFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace AutomomilePlantFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string CarFileName = "Car.xml"; + private readonly string CarShopFileName = "CarShop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Cars { get; private set; } + public List CarShops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -29,6 +31,8 @@ namespace AutomomilePlantFileImplement "Components", x => x.GetXElement); public void SaveCars() => SaveData(Cars, CarFileName, "Cars", x => x.GetXElement); + public void SaveCarShops() => SaveData(CarShops, CarShopFileName, + "CarShops", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); private DataFileSingleton() @@ -38,6 +42,7 @@ namespace AutomomilePlantFileImplement Cars = LoadData(CarFileName, "Car", x => Car.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + CarShops = LoadData(CarShopFileName, "CarShop", x => CarShop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs new file mode 100644 index 0000000..d86068b --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs @@ -0,0 +1,96 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace AutomomilePlantFileImplement.Implements +{ + public class CarShopStorage : ICarShopStorage + { + private readonly DataFileSingleton source; + public CarShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public CarShopViewModel? Delete(CarShopBindingModel model) + { + var element = source.CarShops.FirstOrDefault(x => x.Id == + model.Id); + if (element != null) + { + source.CarShops.Remove(element); + source.SaveCarShops(); + return element.GetViewModel; + } + return null; + } + + public CarShopViewModel? GetElement(CarShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.CarShops + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == + model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(CarShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.CarShops + .Where(x => x.ShopName.Contains(model.ShopName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.CarShops + .Select(x => x.GetViewModel) + .ToList(); + + } + + public CarShopViewModel? Insert(CarShopBindingModel model) + { + model.Id = source.CarShops.Count > 0 ? source.CarShops.Max(x => + x.Id) + 1 : 1; + var newCarShop = CarShop.Create(model); + if (newCarShop == null) + { + return null; + } + source.CarShops.Add(newCarShop); + source.SaveCarShops(); + return newCarShop.GetViewModel; + } + + public CarShopViewModel? Update(CarShopBindingModel model) + { + var shop = source.CarShops.FirstOrDefault(x => x.Id == + model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveCarShops(); + return shop.GetViewModel; + } + } +} diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs b/AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs new file mode 100644 index 0000000..437e5b9 --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs @@ -0,0 +1,116 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomomilePlantFileImplement.Models +{ + public class CarShop : ICarShop + + { + public string ShopName {get; set;} = String.Empty; + + public string Adress { get; set; } = String.Empty; + + public DateTime DateOpen { get; set; } + + public int Fullness { get; set; } + + public Dictionary ShopCars { get; set; } = new(); + private Dictionary _cars = null; + + public int Id { get; set; } + public Dictionary Cars + { + get + { + if (_cars == null) + { + var source = DataFileSingleton.GetInstance(); + _cars = ShopCars.ToDictionary(x => x.Key, y => + ((source.Cars.FirstOrDefault(z => z.Id == y.Key) as ICarModel)!, + y.Value)); + } + return _cars; + } + } + public static CarShop? Create(CarShopBindingModel model) + { + if (model == null) + { + return null; + } + return new CarShop() + { + Id = model.Id, + ShopName = model.ShopName, + Fullness = model.Fullness, + Adress = model.Adress, + DateOpen = model.DateOpen, + ShopCars = model.Cars.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static CarShop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new CarShop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Adress = element.Element("Adress")!.Value, + DateOpen = DateTime.Parse(element.Element("DateOpen")!.Value), + Fullness = Convert.ToInt32(element.Element("Fullness")!.Value), + ShopCars = + element.Element("Cars")!.Elements("ShopCar") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(CarShopBindingModel model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Adress = model.Adress; + ShopCars = model.Cars.ToDictionary(x => x.Key, x => + x.Value.Item2); + _cars = null; + } + public CarShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + DateOpen = DateOpen, + Fullness = Fullness, + Cars = Cars + }; + public XElement GetXElement => new("CarShop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Adress", Adress), + new XElement("DateOpen", DateOpen.ToString()), + new XElement("Fullness", Fullness.ToString()), + new XElement("Cars", ShopCars.Select(x => + new XElement("ShopCar", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + } +} -- 2.25.1 From 313cd7b2c15e8f00146693e4f8303c1836cfdeb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Mon, 1 May 2023 20:58:42 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/CarShopLogic.cs | 53 ++++++++ .../BusinessLogics/OrderLogic.cs | 15 ++- .../BusinessLogicsContracts/ICarShopLogic.cs | 2 + .../StoragesContracts/ICarShopStorage.cs | 3 +- .../Implements/CarShopStorege.cs | 8 +- .../AutomobilePlant/FormMain.Designer.cs | 19 ++- AutomobilePlant/AutomobilePlant/FormMain.cs | 11 ++ AutomobilePlant/AutomobilePlant/FormShop.cs | 1 + .../AutomobilePlant/FormShopAdd.cs | 2 +- .../AutomobilePlant/FormShopCar.cs | 1 + .../AutomobilePlant/FormShopSell.Designer.cs | 118 ++++++++++++++++++ .../AutomobilePlant/FormShopSell.cs | 50 ++++++++ .../AutomobilePlant/FormShopSell.resx | 60 +++++++++ AutomobilePlant/AutomobilePlant/Program.cs | 1 + .../Implements/CarShopStorage.cs | 45 +++++++ .../Models/CarShop.cs | 1 + 16 files changed, 377 insertions(+), 13 deletions(-) create mode 100644 AutomobilePlant/AutomobilePlant/FormShopSell.Designer.cs create mode 100644 AutomobilePlant/AutomobilePlant/FormShopSell.cs create mode 100644 AutomobilePlant/AutomobilePlant/FormShopSell.resx diff --git a/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/CarShopLogic.cs b/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/CarShopLogic.cs index a408796..a478555 100644 --- a/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/CarShopLogic.cs +++ b/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/CarShopLogic.cs @@ -43,6 +43,14 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics _logger.LogWarning("AddCarInShop element not found"); return false; } + var quantityCars = 0; + foreach (var ShopCar in element.Cars) + quantityCars += ShopCar.Value.Item2; + if((quantityCars + quantity) > element.Fullness) + { + throw new ArgumentException("Превышена максимальная вместимость", nameof(quantity)); + } + _logger.LogInformation("AddCarInShop find. Id:{Id}", element.Id); @@ -63,11 +71,56 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics Adress = element.Adress, ShopName = element.ShopName, DateOpen = element.DateOpen, + Fullness = element.Fullness, Cars = element.Cars }); return true; } + public bool AddCar(ICarModel car, int quantity) + { + if (CheckShops(quantity)) + { + foreach(var shop in ReadList(null)) + { + if(quantity==0) return true; + int OccupiedPlaces = 0; + foreach(var ShopCar in shop.Cars) + { + OccupiedPlaces += ShopCar.Value.Item2; + } + int EmptyPlaces = shop.Fullness - OccupiedPlaces; + if(EmptyPlaces > 0) + { + EmptyPlaces = EmptyPlaces > quantity ? quantity : EmptyPlaces; + AddCar(new CarShopSearchModel { Id = shop.Id}, car, EmptyPlaces); + quantity -= EmptyPlaces; + if (quantity == 0) return true; + } + } + + } + return false; + } + + public bool CheckShops( int quantity) + { + foreach(var shop in ReadList(null)) + { + int OccupiedPlaces = 0; + foreach(var car in shop.Cars) + { + OccupiedPlaces += car.Value.Item2; + } + int EmptyPlaces = shop.Fullness - OccupiedPlaces; + quantity -= EmptyPlaces; + } + if(quantity <= 0) + return true; + else return false; + + } + public bool Create(CarShopBindingModel model) { CheckModel(model); diff --git a/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/OrderLogic.cs b/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/OrderLogic.cs index 5a5ac5c..a054275 100644 --- a/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AutomobilePlant/AbstractAutoBusinessLogic/BusinessLogics/OrderLogic.cs @@ -18,10 +18,14 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly ICarShopLogic _carShopLogic; + private readonly ICarStorage _carStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, ICarShopLogic carShopLogic, ICarStorage carStorage) { _logger = logger; _orderStorage = orderStorage; + _carShopLogic = carShopLogic; + _carStorage = carStorage; } public bool CreateOrder(OrderBindingModel model) @@ -58,7 +62,16 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics model.Status = newStatus; if (model.Status == OrderStatus.Выдан) + { model.DateImplement = DateTime.Now; + if (!_carShopLogic.AddCar(_carStorage.GetElement(new CarSearchModel { Id = model.CarId }), model.Count)) + { + model.Status--; + _logger.LogWarning("Small places in shops"); + return false; + } + } + if (_orderStorage.Update(model) == null) { diff --git a/AutomobilePlant/AbstractAutoContracts/BusinessLogicsContracts/ICarShopLogic.cs b/AutomobilePlant/AbstractAutoContracts/BusinessLogicsContracts/ICarShopLogic.cs index f7a5d85..2f174a5 100644 --- a/AutomobilePlant/AbstractAutoContracts/BusinessLogicsContracts/ICarShopLogic.cs +++ b/AutomobilePlant/AbstractAutoContracts/BusinessLogicsContracts/ICarShopLogic.cs @@ -18,5 +18,7 @@ namespace AutomobilePlantContracts.BusinessLogicsContracts bool Update(CarShopBindingModel model); bool Delete(CarShopBindingModel model); bool AddCar(CarShopSearchModel model, ICarModel car, int quantity); + bool CheckShops(int quantity); + bool AddCar(ICarModel car, int quantity); } } diff --git a/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs b/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs index 1cc791c..af7ed2a 100644 --- a/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs +++ b/AutomobilePlant/AbstractAutoContracts/StoragesContracts/ICarShopStorage.cs @@ -14,8 +14,7 @@ namespace AutomobilePlantContracts.StoragesContracts { List GetFullList(); List GetFilteredList(CarShopSearchModel model); - bool CheckShop (CarShopSearchModel model); - bool TrySell (CarShopSearchModel model, ICarModel car); + bool TrySell ( ICarModel car, int quantity); CarShopViewModel? GetElement(CarShopSearchModel model); CarShopViewModel? Insert(CarShopBindingModel model); CarShopViewModel? Update(CarShopBindingModel model); diff --git a/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs b/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs index 8077855..ef16b7b 100644 --- a/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs +++ b/AutomobilePlant/AbstractAutoListImplement/Implements/CarShopStorege.cs @@ -119,12 +119,8 @@ namespace AutomobilePlantListImplement.Implements return null; } - public bool CheckShop(CarShopSearchModel model) - { - throw new NotImplementedException(); - } - - public bool TrySell(CarShopSearchModel model, ICarModel car) + + public bool TrySell(ICarModel car, int quantity) { throw new NotImplementedException(); } diff --git a/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs b/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs index a3c73ac..c075f02 100644 --- a/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs +++ b/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.IssuedOrderButton = new System.Windows.Forms.Button(); this.UpdateListButton = new System.Windows.Forms.Button(); this.AddShopCarButton = new System.Windows.Forms.Button(); + this.Sellbutton = new System.Windows.Forms.Button(); this.MenuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit(); this.SuspendLayout(); @@ -69,21 +70,21 @@ // ИзделияToolStripMenuItem // this.ИзделияToolStripMenuItem.Name = "ИзделияToolStripMenuItem"; - this.ИзделияToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.ИзделияToolStripMenuItem.Size = new System.Drawing.Size(182, 26); this.ИзделияToolStripMenuItem.Text = "Изделия"; this.ИзделияToolStripMenuItem.Click += new System.EventHandler(this.ИзделияToolStripMenuItem_Click); // // КомпонентыToolStripMenuItem // this.КомпонентыToolStripMenuItem.Name = "КомпонентыToolStripMenuItem"; - this.КомпонентыToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.КомпонентыToolStripMenuItem.Size = new System.Drawing.Size(182, 26); this.КомпонентыToolStripMenuItem.Text = "Компоненты"; this.КомпонентыToolStripMenuItem.Click += new System.EventHandler(this.КомпонентыToolStripMenuItem_Click); // // магазиныToolStripMenuItem // this.магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(182, 26); this.магазиныToolStripMenuItem.Text = "Магазины"; this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.магазиныToolStripMenuItem_Click); // @@ -163,11 +164,22 @@ this.AddShopCarButton.UseVisualStyleBackColor = true; this.AddShopCarButton.Click += new System.EventHandler(this.AddShopCarButton_Click); // + // Sellbutton + // + this.Sellbutton.Location = new System.Drawing.Point(832, 507); + this.Sellbutton.Name = "Sellbutton"; + this.Sellbutton.Size = new System.Drawing.Size(143, 56); + this.Sellbutton.TabIndex = 8; + this.Sellbutton.Text = "Продать"; + this.Sellbutton.UseVisualStyleBackColor = true; + this.Sellbutton.Click += new System.EventHandler(this.Sellbutton_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(989, 600); + this.Controls.Add(this.Sellbutton); this.Controls.Add(this.AddShopCarButton); this.Controls.Add(this.UpdateListButton); this.Controls.Add(this.IssuedOrderButton); @@ -203,5 +215,6 @@ private Button UpdateListButton; private ToolStripMenuItem магазиныToolStripMenuItem; private Button AddShopCarButton; + private Button Sellbutton; } } \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/FormMain.cs b/AutomobilePlant/AutomobilePlant/FormMain.cs index b254c1d..9cd539d 100644 --- a/AutomobilePlant/AutomobilePlant/FormMain.cs +++ b/AutomobilePlant/AutomobilePlant/FormMain.cs @@ -217,5 +217,16 @@ namespace AutomobilePlant LoadData(); } } + + private void Sellbutton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopSell)); + + if (service is FormShopSell form) + { + form.ShowDialog(); + LoadData(); + } + } } } diff --git a/AutomobilePlant/AutomobilePlant/FormShop.cs b/AutomobilePlant/AutomobilePlant/FormShop.cs index 275d64d..c4a0cf1 100644 --- a/AutomobilePlant/AutomobilePlant/FormShop.cs +++ b/AutomobilePlant/AutomobilePlant/FormShop.cs @@ -46,6 +46,7 @@ namespace AutomobilePlant _carComponents = view.Cars ?? new Dictionary(); + LoadData(); } } diff --git a/AutomobilePlant/AutomobilePlant/FormShopAdd.cs b/AutomobilePlant/AutomobilePlant/FormShopAdd.cs index 2c14031..fb1cff2 100644 --- a/AutomobilePlant/AutomobilePlant/FormShopAdd.cs +++ b/AutomobilePlant/AutomobilePlant/FormShopAdd.cs @@ -75,7 +75,7 @@ namespace AutomobilePlant var resultOperation = _shopLogic.AddCar( model: new() { Id = (int)ShopNameComboBox.SelectedValue }, - car: car, + car: car, quantity: Convert.ToInt32(CountTextBox.Text) ); diff --git a/AutomobilePlant/AutomobilePlant/FormShopCar.cs b/AutomobilePlant/AutomobilePlant/FormShopCar.cs index 1b24873..2bf2df1 100644 --- a/AutomobilePlant/AutomobilePlant/FormShopCar.cs +++ b/AutomobilePlant/AutomobilePlant/FormShopCar.cs @@ -46,6 +46,7 @@ namespace AutomobilePlant ShopNameTextBox.Text = view.ShopName; AdressTextBox.Text = view.Adress; DateOpenPicker.Value = view.DateOpen; + FullnessnumericUpDown.Value = view.Fullness; _cars = view.Cars ?? new Dictionary(); LoadData(); diff --git a/AutomobilePlant/AutomobilePlant/FormShopSell.Designer.cs b/AutomobilePlant/AutomobilePlant/FormShopSell.Designer.cs new file mode 100644 index 0000000..7d2c2da --- /dev/null +++ b/AutomobilePlant/AutomobilePlant/FormShopSell.Designer.cs @@ -0,0 +1,118 @@ +namespace AutomobilePlant +{ + partial class FormShopSell + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.Carlabel = new System.Windows.Forms.Label(); + this.Countlabel = new System.Windows.Forms.Label(); + this.CarComboBox = new System.Windows.Forms.ComboBox(); + this.CountnumericUpDown = new System.Windows.Forms.NumericUpDown(); + this.Sellbutton = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.CountnumericUpDown)).BeginInit(); + this.SuspendLayout(); + // + // Carlabel + // + this.Carlabel.AutoSize = true; + this.Carlabel.Location = new System.Drawing.Point(42, 41); + this.Carlabel.Name = "Carlabel"; + this.Carlabel.Size = new System.Drawing.Size(68, 20); + this.Carlabel.TabIndex = 0; + this.Carlabel.Text = "Машина"; + // + // Countlabel + // + this.Countlabel.AutoSize = true; + this.Countlabel.Location = new System.Drawing.Point(42, 101); + this.Countlabel.Name = "Countlabel"; + this.Countlabel.Size = new System.Drawing.Size(90, 20); + this.Countlabel.TabIndex = 1; + this.Countlabel.Text = "Количество"; + // + // CarComboBox + // + this.CarComboBox.FormattingEnabled = true; + this.CarComboBox.Location = new System.Drawing.Point(161, 38); + this.CarComboBox.Name = "CarComboBox"; + this.CarComboBox.Size = new System.Drawing.Size(151, 28); + this.CarComboBox.TabIndex = 2; + // + // CountnumericUpDown + // + this.CountnumericUpDown.Location = new System.Drawing.Point(175, 99); + this.CountnumericUpDown.Minimum = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.CountnumericUpDown.Name = "CountnumericUpDown"; + this.CountnumericUpDown.Size = new System.Drawing.Size(137, 27); + this.CountnumericUpDown.TabIndex = 3; + this.CountnumericUpDown.Value = new decimal(new int[] { + 1, + 0, + 0, + 0}); + // + // Sellbutton + // + this.Sellbutton.Location = new System.Drawing.Point(123, 160); + this.Sellbutton.Name = "Sellbutton"; + this.Sellbutton.Size = new System.Drawing.Size(94, 66); + this.Sellbutton.TabIndex = 4; + this.Sellbutton.Text = "Продать"; + this.Sellbutton.UseVisualStyleBackColor = true; + this.Sellbutton.Click += new System.EventHandler(this.Sellbutton_Click); + // + // FormShopSell + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(362, 238); + this.Controls.Add(this.Sellbutton); + this.Controls.Add(this.CountnumericUpDown); + this.Controls.Add(this.CarComboBox); + this.Controls.Add(this.Countlabel); + this.Controls.Add(this.Carlabel); + this.Name = "FormShopSell"; + this.Text = "FormShopSell"; + ((System.ComponentModel.ISupportInitialize)(this.CountnumericUpDown)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label Carlabel; + private Label Countlabel; + private ComboBox CarComboBox; + private NumericUpDown CountnumericUpDown; + private Button Sellbutton; + } +} \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/FormShopSell.cs b/AutomobilePlant/AutomobilePlant/FormShopSell.cs new file mode 100644 index 0000000..379cf17 --- /dev/null +++ b/AutomobilePlant/AutomobilePlant/FormShopSell.cs @@ -0,0 +1,50 @@ +using AutomobilePlantContracts.BusinessLogicsContracts; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AutomobilePlant +{ + public partial class FormShopSell : Form + { + private readonly ILogger _logger; + private readonly ICarLogic _carLogic; + private readonly List? _listCars; + private readonly ICarShopStorage _carShopStorage; + public FormShopSell(ILogger logger, ICarLogic carLogic, ICarShopStorage carShopStorage) + { + InitializeComponent(); + _carLogic = carLogic; + _logger = logger; + _carShopStorage = carShopStorage; + _listCars = carLogic.ReadList(null); + if (_listCars != null) + { + CarComboBox.DisplayMember = "CarName"; + CarComboBox.ValueMember = "Id"; + CarComboBox.DataSource = _listCars; + CarComboBox.SelectedItem = null; + } + } + + private void Sellbutton_Click(object sender, EventArgs e) + { + var car = _carLogic.ReadElement(new CarSearchModel { Id = (int)CarComboBox.SelectedValue }); + var Faith = _carShopStorage.TrySell(car, (int)CountnumericUpDown.Value); + if (Faith) + MessageBox.Show("Продажа прошла успешно"); + else + MessageBox.Show("Продажа прошла неудачно"); + } + } +} diff --git a/AutomobilePlant/AutomobilePlant/FormShopSell.resx b/AutomobilePlant/AutomobilePlant/FormShopSell.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/AutomobilePlant/AutomobilePlant/FormShopSell.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/Program.cs b/AutomobilePlant/AutomobilePlant/Program.cs index 20f4ab1..5513bc6 100644 --- a/AutomobilePlant/AutomobilePlant/Program.cs +++ b/AutomobilePlant/AutomobilePlant/Program.cs @@ -56,6 +56,7 @@ namespace AutomobilePlant services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs index d86068b..4b97de6 100644 --- a/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarShopStorage.cs @@ -2,6 +2,7 @@ using AutomobilePlantContracts.SearchModel; using AutomobilePlantContracts.StoragesContracts; using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Models; using AutomomilePlantFileImplement.Models; using System; using System.Collections.Generic; @@ -19,6 +20,9 @@ namespace AutomomilePlantFileImplement.Implements { source = DataFileSingleton.GetInstance(); } + + + public CarShopViewModel? Delete(CarShopBindingModel model) { var element = source.CarShops.FirstOrDefault(x => x.Id == @@ -80,6 +84,47 @@ namespace AutomomilePlantFileImplement.Implements return newCarShop.GetViewModel; } + public bool TrySell(ICarModel car, int quantity) + { + List carShops = new List(); + int fakeQuantity = quantity; + foreach (var shop in GetFullList()) + { + if (shop.Cars.ContainsKey(car.Id) ) + { + carShops.Add(shop); + fakeQuantity -= shop.Cars[car.Id].Item2; + if(fakeQuantity < 0) + break; + } + } + if (fakeQuantity > 0) + return false; + foreach(var shop in carShops) + { + if(quantity-shop.Cars[car.Id].Item2 < 0) + { + shop.Cars[car.Id] = (shop.Cars[car.Id].Item1, shop.Cars[car.Id].Item2 - quantity); + + } + else + { + quantity -= shop.Cars[car.Id].Item2; + shop.Cars.Remove(car.Id); + } + Update(new CarShopBindingModel + { + ShopName = shop.ShopName, + Adress = shop.Adress, + DateOpen = shop.DateOpen, + Fullness = shop.Fullness, + Id = shop.Id, + Cars = shop.Cars + }); + } +; return true; + } + public CarShopViewModel? Update(CarShopBindingModel model) { var shop = source.CarShops.FirstOrDefault(x => x.Id == diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs b/AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs index 437e5b9..56f1b18 100644 --- a/AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs +++ b/AutomobilePlant/AutomomilePlantFileImplement/Models/CarShop.cs @@ -85,6 +85,7 @@ namespace AutomomilePlantFileImplement.Models } ShopName = model.ShopName; Adress = model.Adress; + Fullness = model.Fullness; ShopCars = model.Cars.ToDictionary(x => x.Key, x => x.Value.Item2); _cars = null; -- 2.25.1