Ejemplo n.º 1
0
        public void CalculatePrice(ViewModelBooks books)
        {
            int oriPrice = 100;

            //Get min Buy Qty
            int minBuyQty = books.BookQty
                .Select((v, i) => new { Index = i, Value = v })
                .Where(r => r.Value > 0)
                .Select(r => r.Value)
                .FirstOrDefault();

            if (minBuyQty != 0)
            {
                //Sort
                books.BookQty = books.BookQty.OrderBy(r => r).ToArray();

                var calcBuyDiscountQty = 0;
                for (int i = 0; i < books.BookQty.Count(); i++)
                {
                    int qty = books.BookQty[i];
                    if (qty < minBuyQty) continue;

                    books.BookQty[i] = books.BookQty[i] - minBuyQty;
                    calcBuyDiscountQty++;
                }

                double discount = GetDiscount(calcBuyDiscountQty);
                books.Price += Convert.ToInt32(oriPrice * calcBuyDiscountQty * discount);

                CalculatePrice(books);
            }
        }
        public void CalculateTest_Book1_Buy_one_Book2_Buy_one_Book3_Buy_two_Price_370()
        {
            //arrange
            var target = new Books();
            var booksInfo = new ViewModelBooks
            {
                Price = 0,
                BookQty = new int[] { 1, 1, 2 }
            };

            //act
            target.CalculatePrice(booksInfo);

            //assert
            var expected = 370;
            Assert.AreEqual(expected, booksInfo.Price);
        }
        public void CalculateTest_Book1_Buy_two_Book2_Buy_five_Book3_Buy_three_Book4_Buy_Four_Book5_Buy_Two_Price_935()
        {
            //arrange
            var target = new Books();
            var booksInfo = new ViewModelBooks
            {
                Price = 0,
                BookQty = new int[] { 2, 5, 3, 4, 2 }
            };

            //act
            target.CalculatePrice(booksInfo);

            //assert
            var expected = 935;
            Assert.AreEqual(expected, booksInfo.Price);
        }