public IActionResult Simulation() { #region Instance Category food = new Category("Food"); Category electronic = new Category("Electronic"); Category smartPhone = new Category("SmartPhone", electronic); Product apple = new Product("Apple", 2, food); Product banana = new Product("Banana", 4, food); Product iPhoneX = new Product("IPhoneX", 5000, smartPhone); Campaign foodCampaign = new Campaign(food, 10, 10, DiscountType.Rate); Campaign foodCampaign2 = new Campaign(food, 10, 100, DiscountType.Amount); Campaign smartPhoneCampaign = new Campaign(smartPhone, 2, 10, DiscountType.Rate); Coupon foodCoupon = new Coupon(20, 20, DiscountType.Amount); Coupon smartPhoneCoupon = new Coupon(1, 100, DiscountType.Amount); #endregion #region ShoppingCart add object _cart.AddItem(apple, 20.5); _logger.LogInformation($"{apple.Title} product added to cart"); _cart.AddItem(apple, 9.5); _logger.LogInformation($"{apple.Title} product added to cart"); _cart.AddItem(banana, 50); _logger.LogInformation($"{banana.Title} product added to cart"); _cart.AddItem(iPhoneX, 3); _logger.LogInformation($"{iPhoneX.Title} product added to cart"); _cart.AddCampaings(foodCampaign, foodCampaign2, smartPhoneCampaign); _cart.AddCoupons(foodCoupon, smartPhoneCoupon); #endregion return(Ok(_cart.Print())); }
public void Can_Checkout_And_Get_Best_Sale_Price() { Dictionary <int, ISale> discounts = new Dictionary <int, ISale>(2); discounts.Add(2141, new Sale(2141, 1.99m)); discounts.Add(4011, new Sale(4011, 0.89m)); _storeRepository.Setup(foo => foo.GetSalePrices()).Returns(discounts); _cart = new Cart(_storeRepository.Object); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(3001, "Grapes")); _cart.AddItem(new CartItem(4011, "Banana")); Receipt receipt = _cart.Checkout(); List <ReceiptLineItem> receiptLineItems = receipt.GetLineItems().ToList(); Assert.Equal(2, receiptLineItems.Count); Assert.Equal(1, receipt.IgnoredItems.Count); Assert.Equal(2141, receiptLineItems[0].PLU); Assert.Equal(1.99m, receiptLineItems[0].FinalPrice); Assert.Equal(4011, receiptLineItems[1].PLU); Assert.Equal(0.89m, receiptLineItems[1].FinalPrice); Assert.Equal(2.88m, receipt.GrandTotal); }
public void UpdateProductQuantitesTest() { _cart.AddItem(_productServiceInMemoryDb.GetProductById(1), 1); _productServiceInMemoryDb.UpdateProductQuantities(); Assert.Equal(9, _productServiceInMemoryDb.GetProductById(1).Quantity); }
public void Can_Checkout_And_Get_Best_Sale_Price_Or_Promotion() { Dictionary <int, ISale> discounts = new Dictionary <int, ISale>(2); discounts.Add(2141, new Sale(2141, 1.56m)); discounts.Add(4011, new Sale(4011, 0.89m)); _storeRepository.Setup(foo => foo.GetSalePrices()).Returns(discounts); Dictionary <int, IPromotion> promotions = new Dictionary <int, IPromotion>(2); promotions.Add(2141, new Promotion("GroupPromotionalPrice", 2141, 4, 0, 1.56m)); promotions.Add(4011, new Promotion("AdditionalProductDiscount", 4011, 3, 1, 100)); _storeRepository.Setup(fn => fn.GetPromotions()).Returns(promotions); _cart = new Cart(_storeRepository.Object); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(4011, "Banana")); _cart.AddItem(new CartItem(4011, "Banana")); _cart.AddItem(new CartItem(4011, "Banana")); _cart.AddItem(new CartItem(4011, "Banana")); _cart.AddItem(new CartItem(3001, "Grapes")); _cart.AddItem(new CartItem(3291, "Pineapple")); _cart.AddItem(new CartItem(2101, "Orange")); Receipt receipt = _cart.Checkout(); List <ReceiptLineItem> receiptLineItems = receipt.GetLineItems().ToList(); Assert.Equal(4, receiptLineItems.Count); Assert.Equal(1, receipt.IgnoredItems.Count); Assert.Equal(2141, receiptLineItems[0].PLU); Assert.Equal(4.68m, receiptLineItems[0].FinalPrice); Assert.Equal(4011, receiptLineItems[1].PLU); Assert.Equal(2.97m, receiptLineItems[1].FinalPrice); Assert.Equal(3291, receiptLineItems[2].PLU); Assert.Equal(4.55m, receiptLineItems[2].FinalPrice); Assert.Equal(2101, receiptLineItems[3].PLU); Assert.Equal(2.49m, receiptLineItems[3].FinalPrice); Assert.Equal(14.69m, receipt.GrandTotal); }
static void Main(string[] args) { AddProducts(); AddOffers(); Cart = new Cart().AddOffers(Offers); while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Q)) { Console.Clear(); if (Cart.Items.Any()) { foreach (var item in Cart.Items) { Console.WriteLine("----------------------------"); Console.WriteLine(string.Format("{0} - {1} ({2}) TOTAL = {3}", item.Product.Id, item.Product.Name, item.Quantity, item.TotalPrice)); if (item.Offer != null) { Console.WriteLine(string.Format("Offer: quantity: {0} for: {1}", item.Offer.Quantity, item.Offer.Price)); } } Console.WriteLine("----------------------------"); Console.WriteLine((string.Format("TOTAL CART: {0}", Cart.Total))); Console.WriteLine(); } Console.WriteLine("Select a product (ESC to QUIT)"); foreach (var product in Products) { Console.WriteLine(product.Id + " - " + product.Name); } var selectedId = Console.ReadLine(); if (selectedId.Trim().ToUpper() == ConsoleKey.Q.ToString()) { return; } int id; int.TryParse(selectedId, out id); var selectedProduct = Products.FirstOrDefault(p => p.Id == id); if (selectedProduct == null) { Console.WriteLine("Invalid selected product"); continue; } var quantityString = ""; int selectedQuantity; while (!int.TryParse(quantityString, out selectedQuantity)) { Console.WriteLine("Insert the quantity"); quantityString = Console.ReadLine(); } Console.WriteLine(); Cart.AddItem(selectedProduct, selectedQuantity); } }
private static void ReadInputCartEntry(ICart cart) { string eneteredMoreItems = String.Empty; do { Console.Write("Please type the Sku of the product: "); string sku = Console.ReadLine(); Console.Write(desiredQuantityQuestion); string quantityString = Console.ReadLine(); int quantity; while (Int32.TryParse(quantityString, out quantity) == false) { Console.WriteLine(); Console.WriteLine("Invalid number enetered."); Console.Write(desiredQuantityQuestion); quantityString = Console.ReadLine(); } cart.AddItem(sku, quantity); Console.Write("Do you want to add another item (yes/no)?"); eneteredMoreItems = Console.ReadLine(); } while (eneteredMoreItems.Length > 0 && eneteredMoreItems.ToLower()[0].Equals('y')); }
public ActionResult AddItem(int ProductId, int Count) { cart.AddItem(new CartItem() { ProductId = ProductId, Count = Count }); return(Ok()); }
public void DeleteProductAddedToCartTest() { _cart.AddItem(_productServiceInMemoryDb.GetProductById(1), 1); _productControllerInMemoryDb.DeleteProduct(1); Assert.Empty(_cart.Lines); }
public JsonResult AddToCart(JsonVoucherItem itemUpdate) { ExperiencesViewModel storedModel = (ExperiencesViewModel)HttpContext.Session["experiences"]; ExperienceViewModel exp = storedModel.Experiences.Find(e => e.Code == itemUpdate.VoucherCode); _cart.AddItem(exp, itemUpdate.Qty); return(Json("OK")); }
public async Task <IActionResult> AddToCart(CartProductVm model) { var product = await Mediator.Handle(new GetProductByIdQuery() { ProductId = model.ProductId }); _cart.AddItem(product, 1); return(Redirect(model.ReturnUrl)); }
public ICart Copy() { ICart output = Factory.CreateCart(); foreach (var content in Contents) { output.AddItem(content.Key, content.Value); } return(output); }
public IActionResult AddToCart(int productId, string returnurl) { Course prod = _repository.GetCourse(productId); if (prod != null) { _cart.AddItem(prod, 1); } return(RedirectToLocal(returnurl)); }
public RedirectToActionResult AddToCart(int ProductID, string returnUrl) { Product product = repository.Products .FirstOrDefault(p => p.ProductID == ProductID); if (product != null) { cart.AddItem(product, 1); } return(RedirectToAction("Index", new { returnUrl })); }
public void Can_Add_Items_To_Cart() { _cart = new Cart(_storeRepository.Object); ICartItem cartItem1 = new CartItem(2001, "Indus"); ICartItem cartItem2 = new CartItem(3001, "Narmada"); ICartItem cartItem3 = new CartItem(4222, "Saraswati"); ICartItem cartItem4 = new CartItem(2001, "Indus"); ICartItem cartItem5 = new CartItem(3001, "Narmada"); _cart.AddItem(cartItem1); _cart.AddItem(cartItem2); _cart.AddItem(cartItem3); _cart.AddItem(cartItem4); _cart.AddItem(cartItem5); Assert.Equal(3, _cart.CartItems.Count); Assert.Equal(2, _cart.CartItems[2001]); Assert.Equal(2, _cart.CartItems[3001]); Assert.Equal(1, _cart.CartItems[4222]); }
public void AddItem() { var firstItem = catalog.All().First(); cart.AddItem(firstItem); Assert.AreEqual(firstItem, cart.Items().First().Key); Assert.AreEqual(1, cart.Items().First().Value); cart.Clear(); Assert.IsFalse(cart.Items().Any()); }
public CartTests() { _cart = new Cart(); _cart.AddItem(new Product { Id = 1, Description = "Description", Details = "Details", Name = "Name", Price = 10, Quantity = 10 }, 1); _cart.AddItem(new Product { Id = 2, Description = "Description", Details = "Details", Name = "Name", Price = 15, Quantity = 9 }, 2); }
public bool AddToCart(IStore store, ICart cart, int id, int quantity) { if (!store.CheckItemAvailability(id, quantity)) { return(false); } var item = store.Items.GetItem(id); store.RemoveItem(item, quantity); cart.AddItem(item, quantity); return(true); }
public RedirectToActionResult AddToCart(int id) { Product product = _productService.GetProductById(id); if (product != null) { _cart.AddItem(product, 1); return(RedirectToAction("Index")); } else { return(RedirectToAction("Index", "Product")); } }
public void AddItemTest() { var product = new Product { Id = 3, Description = "Description", Details = "Details", Name = "Name", Price = 8, Quantity = 11 }; _cart.AddItem(product, 1); var cartLine = _cart.Lines.Last(); Assert.Equal(product.Id, cartLine.Product.Id); Assert.Equal(product.Description, cartLine.Product.Description); Assert.Equal(product.Details, cartLine.Product.Details); Assert.Equal(product.Name, cartLine.Product.Name); Assert.Equal(product.Quantity, cartLine.Product.Quantity); Assert.Equal(product.Price, cartLine.Product.Price); Assert.Equal(1, cartLine.Quantity); }
public void IndexTest() { var product = _productService.GetProductById(1); _cart.AddItem(product, 2); var result = _cartController.Index(); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); var model = Assert.IsAssignableFrom <Cart>(viewResult.Model); Assert.Single(model.Lines); Assert.Equal(product.Id, model.Lines.First().Product.Id); Assert.Equal(product.Description, model.Lines.First().Product.Description); Assert.Equal(product.Details, model.Lines.First().Product.Details); Assert.Equal(product.Name, model.Lines.First().Product.Name); Assert.Equal(product.Price, model.Lines.First().Product.Price); Assert.Equal(product.Quantity, model.Lines.First().Product.Quantity); Assert.Equal(2, model.Lines.First().Quantity); }
public async void IndexSaveOrderTest() { _cart.AddItem(_productService.GetProductById(1), 2); var order = new OrderViewModel { Name = "Name", Address = "Address", City = "City", Country = "Country", Date = new DateTime(2019, 9, 25, 11, 52, 0), Zip = "Zip" }; var result = _orderController.Index(order); var savedOrder = (await _orderService.GetOrders()).Last(); var redirectResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Completed", redirectResult.ActionName); Assert.Single(savedOrder.OrderLine); Assert.Equal(1, savedOrder.OrderLine.First().ProductId); Assert.Equal(2, savedOrder.OrderLine.First().Quantity); }
public void OnPost() { var item = catalog.Get(CartViewModel.ItemId); Cart.AddItem(item, CartViewModel.Quantity); }
public void TestScenarioA() { cart.AddItem("A", 1); Assert.AreEqual(50, cart.CalculatePrice(store)); cart.ClearItems(); cart.AddItem("B", 1); Assert.AreEqual(30, cart.CalculatePrice(store)); cart.ClearItems(); cart.AddItem("C", 1); Assert.AreEqual(20, cart.CalculatePrice(store)); }
public void Can_Checkout_And_Get_Best_Promotion() { Dictionary <int, IPromotion> promotions = new Dictionary <int, IPromotion>(2); promotions.Add(2141, new Promotion("GroupPromotionalPrice", 2141, 4, 0, 2.12m)); promotions.Add(2101, new Promotion("AdditionalProductDiscount", 2101, 3, 1, 100)); _storeRepository.Setup(fn => fn.GetPromotions()).Returns(promotions); _cart = new Cart(_storeRepository.Object); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(2141, "Apple")); _cart.AddItem(new CartItem(2101, "Orange")); _cart.AddItem(new CartItem(2101, "Orange")); _cart.AddItem(new CartItem(2101, "Orange")); _cart.AddItem(new CartItem(2101, "Orange")); _cart.AddItem(new CartItem(2101, "Orange")); _cart.AddItem(new CartItem(2101, "Orange")); _cart.AddItem(new CartItem(3001, "Grapes")); _cart.AddItem(new CartItem(4011, "Banana")); _cart.AddItem(new CartItem(4011, "Banana")); Receipt receipt = _cart.Checkout(); List <ReceiptLineItem> receiptLineItems = receipt.GetLineItems().ToList(); Assert.Equal(3, receiptLineItems.Count); Assert.Equal(1, receipt.IgnoredItems.Count); Assert.Equal(2141, receiptLineItems[0].PLU); Assert.Equal(11.60m, receiptLineItems[0].FinalPrice); Assert.Equal(2101, receiptLineItems[1].PLU); Assert.Equal(12.45m, receiptLineItems[1].FinalPrice); Assert.Equal(4011, receiptLineItems[2].PLU); Assert.Equal(1.98m, receiptLineItems[2].FinalPrice); Assert.Equal(26.03m, receipt.GrandTotal); }
public void AddItem_ProductWithQuantity_ShouldAdd() { var product = new Product("Apple", 2, new Category("Food")); _cart.AddItem(product, 40); Assert.Equal(1, _cart.GetProductCount()); Assert.Equal(40, _cart.GetTotalQuantity()); }
public void AddItem(ProductDTO product, int quantity) { _cart.AddItem(product, quantity); SaveChanges(); }