Shop File implement

This commit is contained in:
ShabOl 2024-04-09 23:44:27 +04:00
parent 2fa2dfe6bc
commit b4c8722cd4
5 changed files with 292 additions and 8 deletions

View File

@ -10,18 +10,19 @@ namespace AutoWorkshopFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string RepairFileName = "Repair.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
private readonly string ShopFileName = "Shop.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Repair> Repairs { get; private set; }
public List<Shop> Shops { get; private set; }
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Repairs = LoadData(RepairFileName, "Repair", x => Repair.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
}
public static DataFileSingleton GetInstance()
@ -37,6 +38,7 @@ namespace AutoWorkshopFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveRepairs() => SaveData(Repairs, RepairFileName, "Repairs", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
private static List<T>? LoadData<T>(string FileName, string XmlNodeName, Func<XElement, T> SelectFunction)
{

View File

@ -0,0 +1,155 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.SearchModels;
using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopFileImplement.Models;
using PizzeriaContracts.SearchModels;
namespace AutoWorkshopFileImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataFileSingleton _source;
public ShopStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ShopViewModel> GetFullList()
{
return _source.Shops.Select(x => x.GetViewModel).ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel Model)
{
if (string.IsNullOrEmpty(Model.ShopName))
return new();
return _source.Shops.Where(x => x.ShopName.Contains(Model.ShopName)).Select(x => x.GetViewModel).ToList();
}
public ShopViewModel? GetElement(ShopSearchModel Model)
{
if (string.IsNullOrEmpty(Model.ShopName) && !Model.Id.HasValue)
return null;
return _source.Shops.FirstOrDefault(x =>
(!string.IsNullOrEmpty(Model.ShopName) && x.ShopName == Model.ShopName) ||
(Model.Id.HasValue && x.Id == Model.Id))?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel Model)
{
Model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1;
var NewShop = Shop.Create(Model);
if (NewShop == null)
return null;
_source.Shops.Add(NewShop);
_source.SaveShops();
return NewShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel Model)
{
var Shop = _source.Shops.FirstOrDefault(x => x.Id == Model.Id);
if (Shop == null)
return null;
Shop.Update(Model);
_source.SaveShops();
return Shop.GetViewModel;
}
public ShopViewModel? Delete(ShopBindingModel Model)
{
var Shop = _source.Shops.FirstOrDefault(x => x.Id == Model.Id);
if (Shop == null)
return null;
_source.Shops.Remove(Shop);
_source.SaveShops();
return Shop.GetViewModel;
}
public bool Sale(SupplySearchModel Model)
{
if (Model == null || !Model.RepairId.HasValue || !Model.Count.HasValue)
return false;
int RemainingSpace = _source.Shops.Select(x => x.Repairs.ContainsKey(Model.RepairId.Value) ? x.Repairs[Model.RepairId.Value] : 0).Sum();
if (RemainingSpace < Model.Count)
return false;
var Shops = _source.Shops
.Where(x => x.Repairs.ContainsKey(Model.RepairId.Value))
.OrderByDescending(x => x.Repairs[Model.RepairId.Value]).ToList();
foreach (var Shop in Shops)
{
int Residue = Model.Count.Value - Shop.Repairs[Model.RepairId.Value];
if (Residue > 0)
{
Shop.Repairs.Remove(Model.RepairId.Value);
Shop.RepairsUpdate();
Model.Count = Residue;
}
else
{
if (Residue == 0)
Shop.Repairs.Remove(Model.RepairId.Value);
else
Shop.Repairs[Model.RepairId.Value] = -Residue;
Shop.RepairsUpdate();
_source.SaveShops();
return true;
}
}
_source.SaveShops();
return false;
}
public bool RestockingShops(SupplyBindingModel Model)
{
if (Model == null || _source.Shops.Select(x => x.RepairsMaxCount - x.ShopRepairs.Select(y => y.Value.Item2).Sum()).Sum() < Model.Count)
return false;
foreach (Shop shop in _source.Shops)
{
int FreeSpaceNum = shop.RepairsMaxCount - shop.ShopRepairs.Select(x => x.Value.Item2).Sum();
if (FreeSpaceNum <= 0)
continue;
FreeSpaceNum = Math.Min(FreeSpaceNum, Model.Count);
Model.Count -= FreeSpaceNum;
if (shop.Repairs.ContainsKey(Model.RepairId))
shop.Repairs[Model.RepairId] += FreeSpaceNum;
else
shop.Repairs.Add(Model.RepairId, FreeSpaceNum);
shop.RepairsUpdate();
if (Model.Count == 0)
{
_source.SaveShops();
return true;
}
}
return false;
}
}
}

View File

@ -0,0 +1,110 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.Xml.Linq;
namespace AutoWorkshopFileImplement.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 OpeningDate { get; private set; }
public Dictionary<int, int> Repairs { get; private set; } = new();
private Dictionary<int, (IRepairModel, int)>? _shopRepairs = null;
public Dictionary<int, (IRepairModel, int)> ShopRepairs
{
get
{
if (_shopRepairs == null)
{
var Source = DataFileSingleton.GetInstance();
_shopRepairs = Repairs.ToDictionary(x => x.Key, y => ((Source.Repairs.FirstOrDefault(z => z.Id == y.Key) as IRepairModel)!, y.Value));
}
return _shopRepairs;
}
}
public int RepairsMaxCount { get; private set; }
public static Shop? Create(ShopBindingModel? Model)
{
if (Model == null)
return null;
return new Shop()
{
Id = Model.Id,
ShopName = Model.ShopName,
Address = Model.Address,
OpeningDate = Model.OpeningDate,
Repairs = Model.ShopRepairs.ToDictionary(x => x.Key, x => x.Value.Item2),
RepairsMaxCount = Model.RepairsMaxCount
};
}
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,
OpeningDate = Convert.ToDateTime(Element.Element("OpeningDate")!.Value),
Repairs = Element.Element("ShopRepairs")!.Elements("ShopRepair")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value)),
RepairsMaxCount = Convert.ToInt32(Element.Element("RepairsMaxCount")!.Value)
};
}
public void Update(ShopBindingModel? Model)
{
if (Model == null)
return;
ShopName = Model.ShopName;
Address = Model.Address;
OpeningDate = Model.OpeningDate;
RepairsMaxCount = Model.RepairsMaxCount;
Repairs = Model.ShopRepairs.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopRepairs = null;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
OpeningDate = OpeningDate,
ShopRepairs = ShopRepairs,
RepairsMaxCount = RepairsMaxCount
};
public XElement GetXElement => new(
"Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("OpeningDate", OpeningDate.ToString()),
new XElement("ShopRepairs", Repairs.Select(
x => new XElement("ShopRepair", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()),
new XElement("RepairsMaxCount", RepairsMaxCount.ToString())
);
public void RepairsUpdate()
{
_shopRepairs = null;
}
}
}

View File

@ -3,6 +3,7 @@ using AutoWorkshopContracts.SearchModels;
using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopListImplement.Models;
using PizzeriaContracts.SearchModels;
namespace AutoWorkshopListImplement.Implements
{
@ -108,5 +109,15 @@ namespace AutoWorkshopListImplement.Implements
return null;
}
public bool Sale(SupplySearchModel Model)
{
throw new NotImplementedException();
}
public bool RestockingShops(SupplyBindingModel Model)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,6 +1,7 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.Reflection;
namespace AutoWorkshopListImplement.Models
{
@ -16,6 +17,8 @@ namespace AutoWorkshopListImplement.Models
public Dictionary<int, (IRepairModel, int)> ShopRepairs { get; private set; } = new();
public int RepairsMaxCount { get; private set; }
public static Shop? Create(ShopBindingModel? Model)
{
if (Model is null)
@ -26,7 +29,8 @@ namespace AutoWorkshopListImplement.Models
Id = Model.Id,
ShopName = Model.ShopName,
Address = Model.Address,
OpeningDate = Model.OpeningDate
OpeningDate = Model.OpeningDate,
RepairsMaxCount = Model.RepairsMaxCount,
};
}
@ -38,6 +42,7 @@ namespace AutoWorkshopListImplement.Models
ShopName = Model.ShopName;
Address = Model.Address;
OpeningDate = Model.OpeningDate;
RepairsMaxCount = Model.RepairsMaxCount;
}
public ShopViewModel GetViewModel => new()
@ -46,7 +51,8 @@ namespace AutoWorkshopListImplement.Models
ShopName = ShopName,
Address = Address,
OpeningDate = OpeningDate,
ShopRepairs = ShopRepairs
ShopRepairs = ShopRepairs,
RepairsMaxCount = Model.RepairsMaxCount,
};
}
}