public bool TryGetSumOnLot(Shop shop, ProductLot productLot, out decimal result) { result = 0.0m; if (shop.TryGetSumOnLot(productLot, out decimal sum)) { result = sum; return(true); } return(false); }
public bool TryGetSumOnLot(ProductLot productLot, out decimal sum) { sum = 0.0m; foreach (var item in productLot.Lot) { if (TryGetProduct(item, out ProductRequest product)) { sum += product.ProductStatus.Price * item.ProductStatus.Amount; } else { return(false); } } return(true); }
public Shop GetShopWithLowestSumOnLot(ProductLot lot) { decimal minSum = Decimal.MaxValue; Shop shopWithMinSum = null; foreach (var shop in Shops) { if (TryGetSumOnLot(shop, lot, out decimal sum)) { if (sum < minSum) { minSum = sum; shopWithMinSum = shop; } } } return(shopWithMinSum ?? throw new ShopNotFoundException()); }
public bool TryBuyLot(ProductLot lot, out decimal sum) { sum = 0.0m; foreach (var item in lot.Lot) { if (FindProductRequest(item) == null) { return(false); } } foreach (var item in lot.Lot) { var product = FindProductRequest(item); var newAmount = product.ProductStatus.Amount - item.ProductStatus.Amount; Products[Products.IndexOf(product)] = product.CopyWith(product.Product, product.ProductStatus.CopyWith(product.ProductStatus.Price, newAmount)); sum += item.ProductStatus.Amount * product.ProductStatus.Price; } return(true); }
static void Main(string[] args) { Product apple = new Product("Яблоко"); Product banana = new Product("Банан"); Product bread = new Product("Хлеб"); Product milk = new Product("Молоко"); Product tomato = new Product("Помидор"); Product shampoo = new Product("Шампунь"); Product toiletPaper = new Product("Туалетная бумага"); Product beer = new Product("Пиво"); Product pasta = new Product("Макароны"); Product redBull = new Product("РедБулл"); Shop shop1 = new Shop("Пятёрочка", "Хвостовая ул. д.239"); shop1.AddProduct(apple, new ProductStatus(25, 1836)); shop1.AddProduct(banana, new ProductStatus(34, 993)); shop1.AddProduct(milk, new ProductStatus(67, 378)); shop1.AddProduct(pasta, new ProductStatus(54, 392)); shop1.AddProduct(beer, new ProductStatus(89, 666)); shop1.AddProduct(toiletPaper, new ProductStatus(120, 426)); Shop shop2 = new Shop("Перекрёсток", "Денежная ул. д.98"); shop2.AddProduct(apple, new ProductStatus(30, 832)); shop2.AddProduct(banana, new ProductStatus(41, 892)); shop2.AddProduct(milk, new ProductStatus(65, 278)); shop2.AddProduct(pasta, new ProductStatus(69, 2953)); shop2.AddProduct(beer, new ProductStatus(47, 382)); shop2.AddProduct(redBull, new ProductStatus(127, 284)); shop2.AddProduct(bread, new ProductStatus(33, 1793)); Shop shop3 = new Shop("Дикси", "Лесной пр-кт д.9"); shop3.AddProduct(apple, new ProductStatus(19, 1938)); shop3.AddProduct(banana, new ProductStatus(41, 892)); shop3.AddProduct(milk, new ProductStatus(65, 278)); shop3.AddProduct(pasta, new ProductStatus(69, 2953)); shop3.AddProduct(shampoo, new ProductStatus(155, 329)); shop3.AddProduct(tomato, new ProductStatus(25, 394)); var shopList = new List <Shop> { shop1, shop2, shop3 }; ShopList shops = new ShopList(shopList); var list = shop1.GetProductsOnSum(100); Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); AnsiConsole.WriteLine("Список всех магазинов с товарами"); shops.PrintShops(); AnsiConsole.WriteLine("Список товаров, которые можно купить на 100 рублей в магазине " + shop1.Name); ShopPrinter.PrintProductList(list, shop1); var sum = shop1.BuyLotOfProducts(new ProductLot(new List <ProductRequest> { new ProductRequest(apple, new ProductStatus(45)), new ProductRequest(banana, new ProductStatus(103)), new ProductRequest(milk, new ProductStatus(15)), })); AnsiConsole.WriteLine("Сумма покупки составила: " + sum); AnsiConsole.WriteLine("Список всех магазинов с товарами"); shops.PrintShops(); var x = shops.GetShopWithLowestPriceOn("P1"); AnsiConsole.WriteLine("Магазин с наименьшей ценой на продукт с id P1: " + x.Name); var lot = new ProductLot(new List <ProductRequest> { new ProductRequest(apple, new ProductStatus(10)), new ProductRequest(banana, new ProductStatus(10)), }); var shop = shops.GetShopWithLowestSumOnLot(lot); AnsiConsole.WriteLine("Магазин с наименьшей стоимостью покупки набора товаров: " + shop.Name); var copiedShop1 = shop1.CopyWith(shop1.Id, shop1.Name, shop1.Address, shop1.Products); copiedShop1.PrintShop(); }
public decimal BuyLotOfProducts(ProductLot lot) => TryBuyLot(lot, out decimal sum) ? sum : throw new ImpossibleToBuyLotException();
public void AddProductLot(ProductLot lot) => Products.AddRange(lot.Lot);