Eliseev E.E. LabWork05_Hard #11

Closed
ElEgEv wants to merge 46 commits from LabWork05_Hard into LabWork05
4 changed files with 27 additions and 20 deletions
Showing only changes of commit e89900b24c - Show all commits

View File

@ -79,13 +79,18 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
_logger.LogInformation("AddManufacture. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id);
var shop = _shopStorage.GetElement(model);
if (shop == null || !CheckShopCount(shop, count))
if (shop == null)
{
_logger.LogWarning("Add Manufacture operation failed");
return false;
}
if (shop.MaxCountManufactures - shop.ShopManufactures.Select(x => x.Value.Item2).Sum() < count)
{
throw new ArgumentNullException("Слишком много изделий для одного магазина", nameof(count));
}
if (!shop.ShopManufactures.ContainsKey(manufacture.Id))
{
shop.ShopManufactures[manufacture.Id] = (manufacture, count);

View File

@ -98,23 +98,23 @@ namespace BlacksmithWorkshopFileImplement.Implements
public bool SellManufactures(IManufactureModel model, int count)
{
if (_source.Shops.Select(x => x.Manufactures.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count)
if (_source.Shops.Select(x => x.ShopManufactures.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count)
{
return false;
}
var list = _source.Shops.Where(x => x.Manufactures.ContainsKey(model.Id));
var list = _source.Shops.Where(x => x.ShopManufactures.ContainsKey(model.Id));
foreach (var shop in list)
{
if (shop.Manufactures[model.Id].Item2 < count)
if (shop.ShopManufactures[model.Id].Item2 < count)
{
count -= shop.Manufactures[model.Id].Item2;
shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, 0);
count -= shop.ShopManufactures[model.Id].Item2;
shop.ShopManufactures[model.Id] = (shop.ShopManufactures[model.Id].Item1, 0);
}
else
{
shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, shop.Manufactures[model.Id].Item2 - count);
shop.ShopManufactures[model.Id] = (shop.ShopManufactures[model.Id].Item1, shop.ShopManufactures[model.Id].Item2 - count);
count -= count;
}
@ -124,7 +124,7 @@ namespace BlacksmithWorkshopFileImplement.Implements
Address = shop.Address,
DateOpen = shop.DateOpen,
MaxCountManufactures = shop.MaxCountManufactures,
Manufactures = shop.Manufactures
ShopManufactures = shop.ShopManufactures
});
if (count == 0)

View File

@ -22,18 +22,18 @@ namespace BlacksmithWorkshopFileImplement.Models
public int MaxCountManufactures { get; set; }
public Dictionary<int, int> countManufacture { get; private set; } = new();
public Dictionary<int, int> Manufactures { get; private set; } = new();
public Dictionary<int, (IManufactureModel, int)> _manufactures = null;
public Dictionary<int, (IManufactureModel, int)> Manufactures
public Dictionary<int, (IManufactureModel, int)> ShopManufactures
{
get
{
if (_manufactures == null)
{
var source = DataFileSingleton.GetInstance();
_manufactures = countManufacture.ToDictionary(x => x.Key, y => ((source.Manufactures.FirstOrDefault(z => z.Id == y.Key) as IManufactureModel)!, y.Value));
_manufactures = Manufactures.ToDictionary(x => x.Key, y => ((source.Manufactures.FirstOrDefault(z => z.Id == y.Key) as IManufactureModel)!, y.Value));
}
return _manufactures;
@ -53,9 +53,10 @@ namespace BlacksmithWorkshopFileImplement.Models
Address = model.Address,
DateOpen = model.DateOpen,
MaxCountManufactures = model.MaxCountManufactures,
countManufacture = model.Manufactures.ToDictionary(x => x.Key, x => x.Value.Item2)
Manufactures = model.ShopManufactures.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Shop? Create(XElement element)
{
@ -70,7 +71,7 @@ namespace BlacksmithWorkshopFileImplement.Models
Address = element.Element("Address")!.Value,
DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value),
MaxCountManufactures = Convert.ToInt32(element.Element("MaxCountManufactures")!.Value),
countManufacture = element.Element("Manufactures")!.Elements("Manufactures").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
Manufactures = element.Element("Manufactures")!.Elements("Manufactures").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
@ -84,7 +85,7 @@ namespace BlacksmithWorkshopFileImplement.Models
Address = model.Address;
DateOpen = model.DateOpen;
MaxCountManufactures = model.MaxCountManufactures;
countManufacture = model.Manufactures.ToDictionary(x => x.Key, x => x.Value.Item2);
Manufactures = model.ShopManufactures.ToDictionary(x => x.Key, x => x.Value.Item2);
_manufactures = null;
}
@ -95,15 +96,16 @@ namespace BlacksmithWorkshopFileImplement.Models
Address = Address,
DateOpen = DateOpen,
MaxCountManufactures = MaxCountManufactures,
Manufactures = Manufactures
ShopManufactures = ShopManufactures
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("DateOpen", DateOpen.ToString()),
new XElement("MaxCountManufactures", MaxCountManufactures.ToString()),
new XElement("Manufactures", countManufacture.Select(x => new XElement("Manufactures",
new XElement("Manufactures", Manufactures.Select(x => new XElement("Manufactures",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}

View File

@ -21,7 +21,7 @@ namespace BlacksmithWorkshopListImplement.Models
public int MaxCountManufactures { get; set; }
public Dictionary<int, (IManufactureModel, int)> Manufactures { get; private set; } =
public Dictionary<int, (IManufactureModel, int)> ShopManufactures { get; private set; } =
new Dictionary<int, (IManufactureModel, int)>();
public static Shop? Create(ShopBindingModel? model)
@ -37,7 +37,7 @@ namespace BlacksmithWorkshopListImplement.Models
ShopName = model.ShopName,
Address = model.Address,
DateOpen = model.DateOpen,
Manufactures = model.Manufactures
ShopManufactures = model.ShopManufactures
};
}
@ -51,7 +51,7 @@ namespace BlacksmithWorkshopListImplement.Models
ShopName = model.ShopName;
Address = model.Address;
DateOpen = model.DateOpen;
Manufactures = model.Manufactures;
ShopManufactures = model.ShopManufactures;
}
public ShopViewModel GetViewModel => new()
@ -60,7 +60,7 @@ namespace BlacksmithWorkshopListImplement.Models
ShopName = ShopName,
Address = Address,
DateOpen = DateOpen,
Manufactures = Manufactures
ShopManufactures = ShopManufactures
};
}
}