示例#1
0
 protected void dgvCart_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         BookCartItem item = (BookCartItem)e.Row.DataItem;
         sumFooterValue += item.Quantity * item.Book.Price;
     }
     else if (e.Row.RowType == DataControlRowType.Footer)
     {
         Label lbl = (Label)e.Row.FindControl("lblTotal");
         lbl.Text = sumFooterValue.ToString();
     }
 }
示例#2
0
        public async Task AddRemoveUpdateClearBook()
        {
            var bookAsync1 = await new BookStoreHelper().SearchBooksAsync("title eq 'Mastering едц'");
            var bookAsync2 = await new BookStoreHelper().SearchBooksAsync("author eq 'Cunning Bastard'");
            var bookAsync3 = await new BookStoreHelper().SearchBooksAsync("title eq 'Generic Title'");

            var book1 = bookAsync1.First();
            var book2 = bookAsync2.First();
            var book3 = bookAsync3.First();

            var bookCartItem1 = new BookCartItem {
                Book = book1, Quantity = 5
            };
            var bookCartItem2 = new BookCartItem {
                Book = book2, Quantity = 2
            };
            var bookCartItem3 = new BookCartItem {
                Book = book3, Quantity = 3
            };

            // Create book cart
            var bookCart = new BookCart();

            Assert.AreEqual(0, bookCart.BookCartItems.Count);
            Assert.AreEqual(0, bookCart.TotalQuantity);
            Assert.AreEqual(0, bookCart.TotalPrice);

            // Add 1st book cart item to the cart
            bookCart.AddBook(bookCartItem1);
            Assert.AreEqual(1, bookCart.BookCartItems.Count);
            Assert.AreEqual(5, bookCart.TotalQuantity);
            Assert.AreEqual(3810, bookCart.TotalPrice);

            // Add 2nd book cart item to the cart
            bookCart.AddBook(bookCartItem2);
            Assert.AreEqual(2, bookCart.BookCartItems.Count);
            Assert.AreEqual(7, bookCart.TotalQuantity);
            Assert.AreEqual(5808, bookCart.TotalPrice);

            // Add 3rd book cart item to the cart
            bookCart.AddBook(bookCartItem3);
            Assert.AreEqual(3, bookCart.BookCartItems.Count);
            Assert.AreEqual(10, bookCart.TotalQuantity);
            Assert.AreEqual(Convert.ToDecimal(6364.5), bookCart.TotalPrice);

            // Remove 2nd book cart item from the cart
            bookCart.RemoveBook(bookCartItem2);
            Assert.AreEqual(2, bookCart.BookCartItems.Count);
            Assert.AreEqual(8, bookCart.TotalQuantity);
            Assert.AreEqual(Convert.ToDecimal(4366.5), bookCart.TotalPrice);

            // Update quantity of 2nd book cart item
            bookCart.UpdateQuantity(bookCartItem2, 4);
            Assert.AreEqual(2, bookCart.BookCartItems.Count);
            Assert.AreEqual(8, bookCart.TotalQuantity);
            Assert.AreEqual(Convert.ToDecimal(4366.5), bookCart.TotalPrice);

            // Update quantity of 1st book cart item
            bookCart.UpdateQuantity(bookCartItem1, 1);
            Assert.AreEqual(2, bookCart.BookCartItems.Count);
            Assert.AreEqual(4, bookCart.TotalQuantity);
            Assert.AreEqual(Convert.ToDecimal(1318.5), bookCart.TotalPrice);

            // Empty the cart
            bookCart.Clear();
            Assert.AreEqual(0, bookCart.BookCartItems.Count);
            Assert.AreEqual(0, bookCart.TotalQuantity);
            Assert.AreEqual(0, bookCart.TotalPrice);
        }
示例#3
0
        public async Task PlaceOrder()
        {
            // Clear the cache
            Cache.Clear();

            var bookStoreHelper = new BookStoreHelper();
            var bookAsync1      = await bookStoreHelper.SearchBooksAsync("title eq 'Mastering едц'");

            var bookAsync2 = await bookStoreHelper.SearchBooksAsync("author eq 'Cunning Bastard'");

            var bookAsync3 = await bookStoreHelper.SearchBooksAsync("title eq 'Generic Title'");

            var bookAsync4 = await bookStoreHelper.SearchBooksAsync("title eq 'How To Spend Money'");

            var book1 = bookAsync1.First();
            var book2 = bookAsync2.First();
            var book3 = bookAsync3.First();
            var book4 = bookAsync4.First();

            var bookCartItem1 = new BookCartItem {
                Book = book1, Quantity = 5
            };
            var bookCartItem2 = new BookCartItem {
                Book = book2, Quantity = 2
            };
            var bookCartItem3 = new BookCartItem {
                Book = book3, Quantity = 3
            };
            var bookCartItem4 = new BookCartItem {
                Book = book4, Quantity = 2
            };

            // Add books to the cart
            var bookCart = new BookCart();

            bookCart.AddBook(bookCartItem1);
            bookCart.AddBook(bookCartItem2);
            bookCart.AddBook(bookCartItem3);
            bookCart.AddBook(bookCartItem4);

            // Test empty cart
            var customer = bookStoreHelper.GetCustomer("dummy");
            await Assert.ThrowsExceptionAsync <DataException>(() => customer.PlaceOrder());

            // Create customer order (combination of In-stock and not In-stock)
            customer          = bookStoreHelper.GetCustomer("dummy");
            customer.BookCart = bookCart;
            var orderDetails = await customer.PlaceOrder();

            Assert.AreEqual(4, orderDetails.PurchasedBooks.Count);
            Assert.AreEqual(1, orderDetails.NotInStockBooks.Count);
            Assert.AreEqual(11, orderDetails.TotalQuantity);
            Assert.AreEqual(Convert.ToDecimal(1006364.5), orderDetails.TotalPrice);
            Assert.AreEqual(0, customer.BookCart.BookCartItems.Count);
            Assert.IsTrue(customer.Orders.IsNotEmpty());
            // Check In-Stock
            Assert.AreEqual(10, book1.InStock);
            Assert.AreEqual(18, book2.InStock);
            Assert.AreEqual(2, book3.InStock);
            Assert.AreEqual(0, book4.InStock);

            // Add books to the cart
            bookCart.AddBook(bookCartItem4);

            // Create customer order (not In-stock)
            orderDetails = await customer.PlaceOrder();

            Assert.AreEqual(0, orderDetails.PurchasedBooks.Count);
            Assert.AreEqual(1, orderDetails.NotInStockBooks.Count);
            Assert.AreEqual(0, orderDetails.TotalQuantity);
            Assert.AreEqual(Convert.ToDecimal(0), orderDetails.TotalPrice);
        }