Tsukanova_I.V. LabWork05_hard #13

Closed
Inohara wants to merge 29 commits from LabWork05_hard into LabWork05
4 changed files with 121 additions and 77 deletions
Showing only changes of commit 9e2fe8d0ec - Show all commits

View File

@ -140,7 +140,7 @@ namespace IceCreamBusinessLogic.BusinessLogics
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found.");
return false;
}
if (!_shopLogic.CheckSupply(icecream, model.Count))
if (CheckSupply(icecream, model.Count) == false)
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error.");
return false;
@ -157,5 +157,66 @@ namespace IceCreamBusinessLogic.BusinessLogics
}
return true;
}
public bool CheckSupply(IIceCreamModel iceCream, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. IceCream count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.IceCreamMaxCount;
foreach (var doc in shop.ShopIceCreams)
{
freeSpace -= doc.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.IceCreamMaxCount;
foreach (var doc in shop.ShopIceCreams)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, freeSpace)) count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
}
}

View File

@ -73,96 +73,50 @@ namespace IceCreamBusinessLogic.BusinessLogics
if (count <= 0)
{
throw new ArgumentNullException("Count of icecreams in supply must be more than 0", nameof(count));
throw new ArgumentNullException("Count of iceCreams in supply myst be more than 0", nameof(count));
}
_logger.LogInformation("SupplyIceCreams. ShopName:{0}.Id:{ 1}", model.Name, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
var shopElement = _shopStorage.GetElement(model);
if (shopElement == null)
{
_logger.LogWarning("Required shop element not found in storage");
return false;
}
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name);
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", element.Id, element.Name);
if(element.IceCreamMaxCount - element.ShopIceCreams.Select(x => x.Value.Item2).Sum() < count)
var countDocs = 0;
foreach (var doc in shopElement.ShopIceCreams)
{
throw new ArgumentNullException("Shop is full", nameof(count));
countDocs += doc.Value.Item2;
}
_logger.LogWarning("SupplyIceCreams find. Id:{Id}", element.Id);
if(element.ShopIceCreams.TryGetValue(iceCream.Id, out var pair))
if (shopElement.IceCreamMaxCount - countDocs >= count)
{
element.ShopIceCreams[iceCream.Id] = (iceCream, count + pair.Item2);
_logger.LogInformation(
"SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop",
count, iceCream.IceCreamName, element.Name);
}
else
{
element.ShopIceCreams[iceCream.Id] = (iceCream, count);
_logger.LogInformation("SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop", count, iceCream.IceCreamName, element.Name);
}
_shopStorage.Update(new()
{
Id = element.Id,
Name = element.Name,
Adress = element.Adress,
OpeningDate = element.OpeningDate,
ShopIceCreams = element.ShopIceCreams,
IceCreamMaxCount = element.IceCreamMaxCount
});
return true;
}
public bool CheckSupply(IIceCreamModel iceCream, int count)
{
if (iceCream == null) throw new ArgumentNullException(nameof(iceCream));
if (count <= 0)
{
throw new ArgumentException("count should be more then 0", nameof(count));
}
int freeSpace = _shopStorage.GetFullList().Select(x => x.IceCreamMaxCount - x.ShopIceCreams.Select(p => p.Value.Item2).Sum()).Sum() - count;
if(freeSpace < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
int countFree = shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum();
if (countFree < count)
if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameDocument))
{
if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, countFree))
{
_logger.LogWarning("SupplyIceCreams operation failed.");
return false;
}
count -= countFree;
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameDocument.Item2 + count);
_logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
}
else
{
if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, count))
{
_logger.LogWarning("SupplyIceCreams operation failed.");
return false;
}
count = 0;
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count);
_logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
}
if (count == 0)
_shopStorage.Update(new()
{
return true;
}
Id = shopElement.Id,
Name = shopElement.Name,
Adress = shopElement.Adress,
OpeningDate = shopElement.OpeningDate,
ShopIceCreams = shopElement.ShopIceCreams,
IceCreamMaxCount = shopElement.IceCreamMaxCount
});
}
return false;
else
{
_logger.LogWarning("Required shop is overflowed");
return false;
}
return true;
}
public bool SellIceCreams(IIceCreamModel iceCream, int count)

View File

@ -19,6 +19,5 @@ namespace IceCreamShopContracts.BusinessLogicsContracts
bool Delete(ShopBindingModel model);
bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count);
bool SellIceCreams(IIceCreamModel iceCream, int count);
bool CheckSupply(IIceCreamModel iceCream, int count);
}
}

View File

@ -83,12 +83,43 @@ namespace IceCreamShopFileImplement.Implements
public bool SellIceCreams(IIceCreamModel model, int count)
{
var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id);
var countStore = count;
if (iceCream == null)
{
return false;
}
foreach (var shop in source.Shops)
{
foreach (var icecream in shop.ShopIceCreams)
{
if (icecream.Value.Item1.Id == iceCream.Id)
{
count -= icecream.Value.Item2;
}
if (count <= 0)
{
break;
}
}
}
if (count > 0)
{
return false;
}
count = countStore;
for (int i = 0; i < source.Shops.Count; i++)
{
var shop = source.Shops[i];
var icecreams = shop.ShopIceCreams;
foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == model.Id))
foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id))
{
var min = Math.Min(icecream.Value.Item2, count);
icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min);
@ -111,7 +142,6 @@ namespace IceCreamShopFileImplement.Implements
});
source.SaveShops();
}
if (count > 0)
{
return false;