public void PurchaseMultipleAppleOrange() { ShoppingCart cart = new ShoppingCart(); Product apple = Catalogue.GetProductByName("apple"); Product orange = Catalogue.GetProductByName("orange"); cart.AddLineItem(new LineItem(apple, 2)); cart.AddLineItem(new LineItem(orange, 10)); Console.WriteLine(cart); }
public void PurchaseSingleAppleOrange() { ShoppingCart cart = new ShoppingCart(); //retrieve the products from the catalogue Product apple = Catalogue.GetProductByName("apple"); Product orange = Catalogue.GetProductByName("orange"); cart.AddLineItem(new LineItem(apple, 1)); cart.AddLineItem(new LineItem(orange, 1)); Console.WriteLine(cart); }
public void RemoveFromCart() { ShoppingCart cart = new ShoppingCart(); Product apple = Catalogue.GetProductByName("apple"); Product orange = Catalogue.GetProductByName("orange"); cart.AddLineItem(new LineItem(apple, 2)); cart.AddLineItem(new LineItem(orange, 10)); Console.WriteLine(cart); cart.RemoveLineItem(new LineItem(apple, 2)); cart.RemoveLineItem(new LineItem(orange, 1)); Console.WriteLine(cart); }
public void PurchaseUnavailableProduct() { ShoppingCart cart = new ShoppingCart(); Product apple = Catalogue.GetProductByName("apple"); Product orange = Catalogue.GetProductByName("orange"); Product banana = Catalogue.GetProductByName("banana"); Product watermelon = Catalogue.GetProductByName("watermelon"); cart.AddLineItem(new LineItem(banana, 2)); cart.AddLineItem(new LineItem(orange, 10)); cart.AddLineItem(new LineItem(watermelon, 1)); Console.WriteLine(cart); }
public void GetShoppingCart_AlreadySavedShoppingCart_IsReturned() { var cart = new ShoppingCart(); cart.AddLineItem(new OrderLine() { BookId = 1, Quantity = 1, OrderId = 1, Book = new Book() { Id = 1, Price = 1m } }); var serializedCart = JsonConvert.SerializeObject(cart); var shoppingCartLogic = new ShoppingCartLogic(new Mock <IDatabaseContext>().Object); var sessionMock = new Mock <ISession>(); sessionMock.Setup(m => m.TryGetValue(It.IsAny <string>(), out It.Ref <byte[]> .IsAny)) .Callback(new GobbleCallback((string key, out byte[] data) => { data = Encoding.UTF8.GetBytes(serializedCart); })).Returns(true); var shoppingCart = shoppingCartLogic.GetShoppingCart(sessionMock.Object); shoppingCart.Should().BeEquivalentTo(cart); }
public ShoppingCart AddBookToCart(ISession session, int bookId, ShoppingCart shoppingCart) { var existingLine = shoppingCart.Lines.SingleOrDefault(l => l.Book.Id == bookId); if (existingLine != null) { existingLine.Quantity++; } else { var book = _databaseContext.Books.First(b => b.Id == bookId); var newOrderLine = new OrderLine { Book = book, BookId = bookId, Quantity = 1 }; shoppingCart.AddLineItem(newOrderLine); } session.SetString(CartSessionKey, JsonConvert.SerializeObject(shoppingCart)); return(shoppingCart); }
public void TestGetLineItem() { ShoppingCart shoppingCart = new ShoppingCart(); Product product = ProductData.GetProduct(productID); LineItem lineItem1 = new LineItem(4, product); shoppingCart.AddLineItem(lineItem1); LineItem lineItem2 = new LineItem(5, product); shoppingCart.AddLineItem(lineItem2); LineItem cartLineItem = shoppingCart.GetLineItem(productID); Assert.IsNotNull(cartLineItem, "The LineItem was not found in the ShoppingCart, gasp!"); Assert.AreEqual(productID, cartLineItem.Item.ProductID, "Invalid Product ID, gasp!"); }
public void TestMultipleAddProduct() { ShoppingCart shoppingCart = new ShoppingCart(); Product product = ProductData.GetProduct(productID); LineItem lineItem1 = new LineItem(4, product); int count = shoppingCart.Quantity; shoppingCart.AddLineItem(lineItem1); count += lineItem1.Quantity; LineItem lineItem2 = new LineItem(5, product); shoppingCart.AddLineItem(lineItem2); count += lineItem2.Quantity; Assert.AreEqual(count, shoppingCart.Quantity, "Invalid number of items in cart, gasp!"); }
public void NegativeTestUpdateLineItemQuantity() { ShoppingCart shoppingCart = new ShoppingCart(); Product product = ProductData.GetProduct(productID); int newQuantity = -10; LineItem lineItem1 = new LineItem(4, product); shoppingCart.AddLineItem(lineItem1); LineItem lineItem2 = new LineItem(5, product); shoppingCart.AddLineItem(lineItem2); // Update the LineItem quantity of the Product with productID shoppingCart.UpdateLineItemQuantity(productID, newQuantity); LineItem cartLineItem = shoppingCart.GetLineItem(productID); Assert.IsNull(cartLineItem, "The LineItem was not found in the ShoppingCart, gasp!"); }
public void TestGetCartContents() { ShoppingCart shoppingCart = new ShoppingCart(); Product product = ProductData.GetProduct(productID); LineItem lineItem1 = new LineItem(4, product); shoppingCart.AddLineItem(lineItem1); LineItem lineItem2 = new LineItem(5, product); shoppingCart.AddLineItem(lineItem2); IDictionaryEnumerator cartEnumerator = shoppingCart.GetCartContents(); LineItem cartLineItem = null; // there should be only one line item while (cartEnumerator.MoveNext()) { cartLineItem = (LineItem)cartEnumerator.Value; } Assert.IsNotNull(cartLineItem, "There where no LineItems in the ShoppingCart, gasp!"); Assert.AreEqual(productID, cartLineItem.Item.ProductID, "Invalid Product ID, gsap!"); }
public void TestUpdateLineItemMessage() { ShoppingCart shoppingCart = new ShoppingCart(); Product product = ProductData.GetProduct(productID); LineItem lineItem1 = new LineItem(4, product); shoppingCart.AddLineItem(lineItem1); // Update the LineItem quantity of the Product with productID message = "test message"; shoppingCart.UpdateLineItemMessage(productID, message); LineItem cartLineItem = shoppingCart.GetLineItem(productID); Assert.IsNotNull(cartLineItem, "The LineItem was not found in the ShoppingCart, gasp!"); Assert.AreEqual(message, cartLineItem.Message, "Did not get the expected message, gasp!"); }
public void WhenIAddWithQuantityToTheCart(string productName, string quantity) { try { if (productName == null) { throw new ArgumentNullException("productName or quantity"); } Product product = Catalogue.GetProductByName(productName.ToUpper().Trim()); int intQuantity = Int32.Parse(quantity); cart.AddLineItem(new LineItem(product, intQuantity)); } catch (FormatException e) { Console.WriteLine(e.ToString()); } }
public ILineItem AddItemToCart(string code, int quantity) { var lineItem = LineItemByCode(code); if (lineItem == null) { lineItem = _orderGroupFactory.CreateLineItem(code, ShoppingCart); lineItem.Quantity = quantity; ShoppingCart.AddLineItem(lineItem); } else { var shipment = ShoppingCart.GetFirstShipment(); ShoppingCart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity); } return(lineItem); }