From dc7cdf6cc213c1d5cd1c878122825fb9d89aa75c Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Mon, 26 Feb 2024 08:21:45 +0400 Subject: [PATCH] LabWork02_Hard Work in progress --- .../BusinessLogics/ShopLogic.cs | 28 +++ .../BusinessLogicsContracts/IShopLogic.cs | 8 - .../StoragesContracts/IShopStorage.cs | 8 + .../AircraftPlantFileImplement/Models/Shop.cs | 159 ++++++++++++++++++ .../Implements/ShopStorage.cs | 25 +++ .../AircraftPlantListImplement/Models/Shop.cs | 5 + 6 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 AircraftPlant/AircraftPlantFileImplement/Models/Shop.cs diff --git a/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/ShopLogic.cs b/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/ShopLogic.cs index 7a6f6d3..0f2302e 100644 --- a/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/ShopLogic.cs @@ -187,6 +187,34 @@ namespace AircraftPlantBusinessLogic.BusinessLogics return true; } + /// + /// Продажа изделий + /// + /// + /// + /// + /// + /// + public bool SellPlanes(IPlaneModel plane, int count) + { + if (plane == null) + { + throw new ArgumentNullException(nameof(plane)); + } + if (count <= 0) + { + throw new ArgumentException("Количество изделий должно быть больше 0", nameof(count)); + } + + if (_shopStorage.SellPlanes(plane, count)) + { + _logger.LogInformation("Selling sucsess"); + return true; + } + _logger.LogInformation("Selling failed"); + return false; + } + /// /// Проверка модели магазина /// diff --git a/AircraftPlant/AircraftPlantContracts/BusinessLogicsContracts/IShopLogic.cs b/AircraftPlant/AircraftPlantContracts/BusinessLogicsContracts/IShopLogic.cs index f23492d..2933e88 100644 --- a/AircraftPlant/AircraftPlantContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/AircraftPlant/AircraftPlantContracts/BusinessLogicsContracts/IShopLogic.cs @@ -59,14 +59,6 @@ namespace AircraftPlantContracts.BusinessLogicsContracts /// bool AddPlaneInShop(ShopSearchModel model, IPlaneModel plane, int count); - /// - /// Добавление изделий в магазины - /// - /// - /// - /// - bool AddPlanes(IPlaneModel plane, int count); - /// /// Продажа изделий /// diff --git a/AircraftPlant/AircraftPlantContracts/StoragesContracts/IShopStorage.cs b/AircraftPlant/AircraftPlantContracts/StoragesContracts/IShopStorage.cs index 9dbabac..c60c3ea 100644 --- a/AircraftPlant/AircraftPlantContracts/StoragesContracts/IShopStorage.cs +++ b/AircraftPlant/AircraftPlantContracts/StoragesContracts/IShopStorage.cs @@ -63,5 +63,13 @@ namespace AircraftPlantContracts.StoragesContracts /// /// bool SellPlanes(IPlaneModel plane, int count); + + /// + /// Проверка наличия изделий в магазинах в нужном количестве + /// + /// + /// + /// + bool Restock(IPlaneModel plane, int count); } } diff --git a/AircraftPlant/AircraftPlantFileImplement/Models/Shop.cs b/AircraftPlant/AircraftPlantFileImplement/Models/Shop.cs new file mode 100644 index 0000000..7762e08 --- /dev/null +++ b/AircraftPlant/AircraftPlantFileImplement/Models/Shop.cs @@ -0,0 +1,159 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AircraftPlantFileImplement.Models +{ + /// + /// Сущность "Магазин" + /// + public class Shop : IShopModel + { + /// + /// Идентификатор + /// + public int Id { get; private set; } + + /// + /// Название магазина + /// + public string ShopName { get; private set; } = string.Empty; + + /// + /// Адрес магазина + /// + public string Address { get; private set; } = string.Empty; + + /// + /// Дата открытия магазина + /// + public DateTime DateOpening { get; private set; } + + /// + /// Коллекция изделий магазина в виде + /// «идентификатор изделия – количество изделий» + /// + public Dictionary Planes { get; private set; } = new(); + + /// + /// Коллекция изделий в магазине + /// + private Dictionary? _shopPlanes = null; + public Dictionary ShopPlanes + { + get + { + if (_shopPlanes == null) + { + var source = DataFileSingleton.GetInstance(); + _shopPlanes = Planes.ToDictionary(x => x.Key, y => ((source.Planes.FirstOrDefault(z => z.Id == y.Key) as IPlaneModel)!, y.Value)); + } + return _shopPlanes; + } + } + + /// + /// Максимальное количество изделий + /// + public int MaxPlanes { get; private set; } + + /// + /// Создание модели магазина из данных файла + /// + /// + /// + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + Planes = element.Element("ShopPlanes")!.Elements("ShopPlanes")! + .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)), + MaxPlanes = Convert.ToInt32(element.Element("MaxPlanes")!.Value) + }; + } + + /// + /// Создание модели магазина + /// + /// + /// + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + Planes = model.ShopPlanes.ToDictionary(x => x.Key, x => x.Value.Item2), + MaxPlanes = model.MaxPlanes + }; + } + + /// + /// Изменение модели магазина + /// + /// + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + Planes = model.ShopPlanes.ToDictionary(x => x.Key, x => x.Value.Item2); + MaxPlanes = model.MaxPlanes; + _shopPlanes = null; + } + + /// + /// Получение модели магазина + /// + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopPlanes = ShopPlanes, + MaxPlanes = MaxPlanes + }; + + /// + /// Запись данных о модели магазина в файл + /// + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening.ToString()), + new XElement("ShopPlanes", Planes.Select(x => + new XElement("ShopPlanes", + new XElement("Key", x.Key), + new XElement("Value", x.Value)))), + new XElement("MaxPlanes", MaxPlanes.ToString() + .ToArray())); + } +} diff --git a/AircraftPlant/AircraftPlantListImplement/Implements/ShopStorage.cs b/AircraftPlant/AircraftPlantListImplement/Implements/ShopStorage.cs index 9ca1ff9..3c0baac 100644 --- a/AircraftPlant/AircraftPlantListImplement/Implements/ShopStorage.cs +++ b/AircraftPlant/AircraftPlantListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using AircraftPlantContracts.SearchModels; using AircraftPlantContracts.StoragesContracts; using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; using AircraftPlantListImplement.Models; using System; using System.Collections.Generic; @@ -150,5 +151,29 @@ namespace AircraftPlantListImplement.Implements } return null; } + + /// + /// Продажа изделий + /// + /// + /// + /// + /// + public bool SellPlanes(IPlaneModel plane, int count) + { + throw new NotImplementedException(); + } + + /// + /// Проверка наличия изделий в магазинах в нужном количестве + /// + /// + /// + /// + /// + public bool Restock(IPlaneModel plane, int count) + { + throw new NotImplementedException(); + } } } diff --git a/AircraftPlant/AircraftPlantListImplement/Models/Shop.cs b/AircraftPlant/AircraftPlantListImplement/Models/Shop.cs index 148979b..a73ba98 100644 --- a/AircraftPlant/AircraftPlantListImplement/Models/Shop.cs +++ b/AircraftPlant/AircraftPlantListImplement/Models/Shop.cs @@ -43,6 +43,11 @@ namespace AircraftPlantListImplement.Models private set; } = new Dictionary(); + /// + /// Максимальное количество изделий + /// + public int MaxPlanes { get; private set; } + /// /// Создание модели магазина ///