コード例 #1
0
        public void Begin()
        {
            if (!_cartData.Any())
            {
                _consoleWriter.WriteLine("Error cart is empty");
                return;
            }

            var productCatalog = new ProductCatalog();
            foreach (var productCatalogLine in _productCatalogData)
            {
                var parts = productCatalogLine.Split(',');
                if (parts[0] == "PRODUCT")
                {
                    var barcode = parts[1].Trim();
                    var price = Decimal.Parse(parts[2]);
                    productCatalog.AddProduct(barcode, price);
                }
                else if (parts[0] == "QUANTITY_DISCOUNT")
                {
                    var barcode = parts[1].Trim();
                    var quantity = Convert.ToInt32(parts[2]);
                    var discountPrice = Convert.ToDecimal(parts[3]);
                    productCatalog.AddQuantityDiscount(barcode, quantity, discountPrice);
                }
                else if (parts[0] == "ADDITIONAL_ITEM_DISCOUNT")
                {
                    var barcode = parts[1].Trim();
                    var quantity = Convert.ToInt32(parts[2]);
                    var quantityDiscounted = Convert.ToInt32(parts[3]);
                    var dicountPercentage = Convert.ToDouble(parts[4]);
                    productCatalog.AddAdditionalItemDiscount(barcode, quantity, quantityDiscounted, dicountPercentage);
                }
            }

            var promotionalDiscountRules = new List<IPromotionalDiscountRule>() { new AdditionalItemDiscountRule(productCatalog), new QuantityDiscountRule(productCatalog) };
            var transaction = new CheckoutTransaction(productCatalog, new Promotions(promotionalDiscountRules));
            foreach (var cartLine in _cartData)
            {
                transaction.AddItem(cartLine);
            }

            var reportGenerator = new ReceiptGenerator();
            var receipt = reportGenerator.Generate(transaction);
            _consoleWriter.WriteLine(receipt);
        }
コード例 #2
0
        public void When_I_generate_a_receipt_with_quantity_discount()
        {
            var sut = new ReceiptGenerator();
            var productCatalog = new ProductCatalog();
            productCatalog.AddProduct("apple", 0.75m);
            productCatalog.AddProduct("banana", 1.00m);
            productCatalog.AddQuantityDiscount("apple", 3, 2.00m);
            var transaction = new CheckoutTransaction(productCatalog, new Promotions(new List<IPromotionalDiscountRule>() { new AdditionalItemDiscountRule(productCatalog), new QuantityDiscountRule(productCatalog) }));
            transaction.AddItem("apple");
            transaction.AddItem("banana");
            transaction.AddItem("apple");
            transaction.AddItem("apple");
            transaction.AddItem("apple");
            transaction.AddItem("apple");
            transaction.AddItem("apple");
            transaction.AddItem("apple");

            var actual = sut.Generate(transaction);
            Assert.That(actual, Is.EqualTo("Receipt:\n7 apple @ $0.75 is $5.25\n***Discount on apple: Buy 3 apple for $2.00, New Price $4.75, Savings $0.50\n1 banana @ $1.00 is $1.00\nTotal is $5.75"));
        }