Exemplo n.º 1
0
        public string GenerateReceipt(Order order)
        {
            var totalAmount = 0d;
            var result      = new StringBuilder("{" + $"\"order-receipt-for\":\"{order.Company}\",");

            if (order.OrderLines.Any())
            {
                result.Append("\"orderlines\":[");
                totalAmount = PriceCalculator.CalculateTotal(order.OrderLines,
                                                             (line, amount) => {
                    result.Append("{\"quantity\":");
                    result.Append($"\"{line.Quantity}\"");
                    result.Append($",\"product-type\":");
                    result.Append($"\"{line.Product.ProductType}\",");
                    result.Append("\"product\":{");
                    result.Append($"\"name\":\"{line.Product.ProductName}\",");
                    result.Append($"\"price\": \"{amount:C}\"");
                    result.Append("}},");
                }
                                                             );

                result.Insert(result.Length - 1, "]", 1);
            }
            result.Append($"\"subtotal\": \"{totalAmount:C}\",");
            var totalTax = totalAmount * Rates.Tax;

            result.Append($"\"mva\":\"{totalTax:C}\",");
            result.Append($"\"total\":\"{(totalAmount + totalTax):C}\"");
            result.Append("}");
            return(result.ToString());
        }
        public void CalculateTotal_WithNoDiscountRules_AndNoItems_ReturnsZeroTotal()
        {
            var discountRules = new HashSet <IDiscountRule>();
            var calculator    = new PriceCalculator(discountRules);
            var basketEntries = new List <BasketEntry>();

            var total = calculator.CalculateTotal(basketEntries);

            Assert.AreEqual(0, total);
        }
        public void CalculateTotal_WithNoDiscountRules_AndSingleEntry_ReturnsCorrectTotal()
        {
            var discountRules = new HashSet <IDiscountRule>();
            var calculator    = new PriceCalculator(discountRules);
            var basketEntries = new List <BasketEntry>(new[] {
                new BasketEntry(new Item(name: "Test 1", price: 1), quantity: 1)
            });

            var total = calculator.CalculateTotal(basketEntries);

            Assert.AreEqual(1, total);
        }
Exemplo n.º 4
0
        public string GenerateReceipt(Order order)
        {
            var result      = new StringBuilder($"Order receipt for '{order.Company}'{Environment.NewLine}");
            var totalAmount = PriceCalculator.CalculateTotal(order.OrderLines,
                                                             (line, amount) => result.AppendLine(
                                                                 $"\t{line.Quantity} x {line.Product.ProductType} {line.Product.ProductName} {line.Quantity} = {amount:C}"));

            result.AppendLine($"Subtotal: {totalAmount:C}");
            var totalTax = totalAmount * Rates.Tax;

            result.AppendLine($"MVA: {totalTax:C}");
            result.Append($"Total: {(totalAmount + totalTax):C}");
            return(result.ToString());
        }
        public void CalculateTotal_WithMockDiscountRule_AndDiscountGreaterThanTotalPriceOfEntries_ReturnsNoLessThanZeroTotal()
        {
            var discountRules = new HashSet <IDiscountRule>(new[] {
                new MockDiscountRule()
            });
            var calculator    = new PriceCalculator(discountRules);
            var basketEntries = new List <BasketEntry>(new[] {
                new BasketEntry(new Item(name: "Test", price: .5M))
            });

            var total = calculator.CalculateTotal(basketEntries);

            Assert.IsFalse(total < 0);
        }
Exemplo n.º 6
0
        public void Discount10PercentFor5Plus_GivesAppropriateDiscount()
        {
            List <OrderItem> items = new List <OrderItem>
            {
                new OrderItem {
                    Quantity = 5, Price = 10, DiscountType = DiscountTypes.Discount10PercentOn5Plus
                }                                                                                                 // should be 45
            };

            double expectedPrice = 45;

            double actualPrice = PriceCalculator.CalculateTotal(items);

            Assert.Equal(expectedPrice, actualPrice);
        }
Exemplo n.º 7
0
        public void Discount4For3_GivesAppropriateDiscount()
        {
            List <OrderItem> items = new List <OrderItem>
            {
                new OrderItem {
                    Quantity = 4, Price = 10, DiscountType = DiscountTypes.Discount4For3
                }                                                                                      // should be 3*10
            };

            double expectedPrice = 30;

            double actualPrice = PriceCalculator.CalculateTotal(items);

            Assert.Equal(expectedPrice, actualPrice);
        }
        public void CalculateTotal_WithMockDiscountRule_AndMultipleVaryingEntries_ReturnsCorrectTotal()
        {
            var discountRules = new HashSet <IDiscountRule>(new[] {
                new MockDiscountRule()
            });
            var calculator    = new PriceCalculator(discountRules);
            var basketEntries = new List <BasketEntry>(new[] {
                new BasketEntry(new Item(name: "Test 1", price: 2), quantity: 2),
                new BasketEntry(new Item(name: "Test 2", price: 2), quantity: 2)
            });

            var total = calculator.CalculateTotal(basketEntries);

            // Price: 2 items * 2 quantity * 2 each = 8.
            // Discount: 1 per item quantity * 2 items * 2 quantity = 4.
            // Total should be 8 - 4 = 4.
            Assert.AreEqual(4, total);
        }
Exemplo n.º 9
0
        public void NoDiscount_GivesFullPrice()
        {
            List <OrderItem> items = new List <OrderItem>
            {
                new OrderItem {
                    Quantity = 4, Price = 10
                },
                new OrderItem {
                    Quantity = 3, Price = 2
                },
            };

            double expectedPrice = 46;

            double actualPrice = PriceCalculator.CalculateTotal(items);

            Assert.Equal(expectedPrice, actualPrice);
        }
Exemplo n.º 10
0
        public string GenerateReceipt(Order order)
        {
            var totalAmount = 0d;
            var result      = new StringBuilder($"<html><body><h1>Order receipt for '{order.Company}'</h1>");

            if (order.OrderLines.Any())
            {
                result.Append("<ul>");
                totalAmount = PriceCalculator.CalculateTotal(order.OrderLines,
                                                             (line, amount) => result.Append(
                                                                 $"<li>{line.Quantity} x {line.Product.ProductType} {line.Product.ProductName} = {amount:C}</li>"));
                result.Append("</ul>");
            }
            result.Append($"<h3>Subtotal: {totalAmount:C}</h3>");
            var totalTax = totalAmount * Rates.Tax;

            result.Append($"<h3>MVA: {totalTax:C}</h3>");
            result.Append($"<h2>Total: {(totalAmount + totalTax):C}</h2>");
            result.Append("</body></html>");
            return(result.ToString());
        }
        public decimal CalculateTotal()
        {
            var products = _productService.Get(p => _scannedProducts.Keys.Any(key => key == p.Id)).ToList();

            //same as below but with Select instead of join
            //var calculationItems = products.Select(p =>
            //    new PriceCalculationItem(p.Id, _scannedProducts[p.Id], p.PerUnitPrice)
            //    {
            //        PerGroupPrice = p.PerGroupPrice
            //    })
            //.ToList();

            var calculationItems = products.Join(_scannedProducts,
                                                 p => p.Id, sp => sp.Key,
                                                 (product, scannedProduct) =>
                                                 new PriceCalculationItem(product.Id, scannedProduct.Value, product.PerUnitPrice)
            {
                PerGroupPrice = product.PerGroupPrice
            })
                                   .ToList();

            return(_priceCalculator.CalculateTotal(calculationItems));
        }