/// <summary> /// Method which processes the checkout order of a user. /// Loads both the price and promotion catalogues when called. /// </summary> /// <returns>A string which represents the receipt of the customers order.</returns> public string Checkout() { //// Load price catalogue and promotional resources // Parse from JSON to respective Model (PriceCatalogue, PromotionCatalogue) var priceCatalogue = _checkoutService.GetPriceCatalogue(PriceCatalogueRelativePath); var promotionCatalogue = _checkoutService.GetPromotionCatalogue(PromotionsRelativePath); // Read items from cart.json var shoppingCart = _checkoutService.GetShoppingCart(ShoppingCartRelativePath); var receipt = new Receipt(); foreach (var item in shoppingCart) { // Item is already in receipt. if (receipt.CheckedOutItems.Any(x => x.Name == item)) { // Update quantity. receipt.CheckedOutItems.FirstOrDefault(x => x.Name == item).Quantity++; } // New Item to add to receipt. else { // Search for item in price catalogue. var itemFromPriceCatalogue = priceCatalogue.Items.FirstOrDefault(x => x.Name == item); // Matching item was found in price catalogue. if (itemFromPriceCatalogue != null) { // Create new checked out item to be placed in receipt. var checkedOutItem = new CheckedOutItem { Name = item.ToLower(), Price = itemFromPriceCatalogue.Price, Quantity = 1 }; // Search for item in promotion catalogue. var itemFromPromotionCatalogue = promotionCatalogue.Items.FirstOrDefault(x => x.Name == item); // Matching item was found in promotion catalogue. if (itemFromPromotionCatalogue != null) { // Add Sale Price to CheckedOutItem. checkedOutItem.SalePrice = itemFromPromotionCatalogue.SalePrice; } // Add item to Receipt receipt.CheckedOutItems.Add(checkedOutItem); } } } return(receipt.ToString()); }
public void CheckoutControllerCheckoutMethodTest() { // arrange var expected = new Receipt { CheckedOutItems = new List <CheckedOutItem> { new CheckedOutItem { Name = "item1", Quantity = 1, Price = .75, SalePrice = .50, }, new CheckedOutItem { Name = "item2", Quantity = 2, Price = 2.00 } } }; var promotionCatalogue = new PromotionCatalogue { Items = new List <Item> { new Item { Name = "item1", SalePrice = .50 } } }; var priceCatalogue = new PriceCatalogue { Items = new List <Item> { new Item { Name = "item1", Price = .75 }, new Item { Name = "item2", Price = 2.00 } } }; var shoppingCart = new Queue <string>(); shoppingCart.Enqueue("item2"); shoppingCart.Enqueue("item1"); shoppingCart.Enqueue("item2"); var actual = new Receipt(); // act foreach (var item in shoppingCart) { // Item is already in receipt. if (actual.CheckedOutItems.Any(x => x.Name == item)) { actual.CheckedOutItems.FirstOrDefault(x => x.Name == item).Quantity++; } // New Item to add to receipt. else { // Search for item in price catalogue. var itemFromPriceCatalogue = priceCatalogue.Items.FirstOrDefault(x => x.Name == item); // Matching item was found in price catalogue. if (itemFromPriceCatalogue != null) { // Create new checked out item to be placed in receipt. var checkedOutItem = new CheckedOutItem { Name = item.ToLower(), Price = itemFromPriceCatalogue.Price, Quantity = 1 }; // Search for item in promotion catalgoue. var itemFromPromotionCatalogue = promotionCatalogue.Items.FirstOrDefault(x => x.Name == item); // Matching item was found in promotion catalogue. if (itemFromPromotionCatalogue != null) { // Add Sale Price to CheckedOutItem. checkedOutItem.SalePrice = itemFromPromotionCatalogue.SalePrice; } // Add item to the receipt. actual.CheckedOutItems.Add(checkedOutItem); } } } // assert Assert.AreEqual(expected.TotalPrice, actual.TotalPrice); }