public int AddItemToBasket(int customerId, ShoppingBasketItem item) { var basket = _repository.GetBasket(customerId); if (basket != null) { if (basket.OrderItems.Exists(i => i.ProductId == item.ProductId)) { var existingItem = basket.OrderItems.First(i => i.ProductId == item.ProductId); var quantity = existingItem.Quantity + item.Quantity; _repository.UpdateShoppingBasketItem(existingItem, quantity); return(existingItem.ItemId); } else { return(_repository.InsertShoppingBasketItem(basket, item)); } } else { var newBasket = new ShoppingBasket { CustomerId = customerId }; _repository.InsertBasket(newBasket); var existingBasket = _repository.GetBasket(customerId); return(_repository.InsertShoppingBasketItem(existingBasket, item)); } }
public void UpdateItem(ShoppingBasketItem sbi) { sbi.UserName = this.GetUser().UserName; ShoppingBasketItem existingItem = ctx.ShoppingBasket. SingleOrDefault(i => i.UserName == sbi.UserName && i.Description == sbi.Description); if (existingItem != null) { // Item is already in the shopping basket // Of course, we should provide a status indicator to the caller, // perhaps the item that you are deleting couldn't be found in the basket in // the first place. I'll leave that one for another day. if (sbi.Quantity == 0) { /* Oh well, user has decided not to take their imaginary item. * terrible shame, it was going to be free after all */ this.DeleteItem(sbi); } else { /* Add the new quantity to the quantity already in the basket */ existingItem.Quantity += sbi.Quantity; } } else { // A new order has been placed into the basket ctx.ShoppingBasket.Add(sbi); } ctx.SaveChanges(); }
public async Task <ShoppingBasketItem> GetShoppingBasketItem(int coffeeId, Guid shoppingBasketNumber) { ShoppingBasketItem shoppingItem = null; using (DbCommand command = this.coffeeDatabase.GetStoredProcCommand("dbo.spGetShoppingBasketItem")) { this.coffeeDatabase.AddInParameter(command, "@coffeeId", DbType.Int32, coffeeId); this.coffeeDatabase.AddInParameter(command, "@shoppingBasketNumber", DbType.Guid, shoppingBasketNumber); Task <IDataReader> readerTask = Task <IDataReader> .Factory.FromAsync(this.coffeeDatabase.BeginExecuteReader, this.coffeeDatabase.EndExecuteReader, command, null); using (IDataReader reader = await readerTask) { while (reader.Read()) { shoppingItem = new ShoppingBasketItem(); shoppingItem.ShoppingBasketId = Guid.Parse(reader["ShoppingBasketNumber"].ToString()); shoppingItem.ShoppingBasketItemId = int.Parse(reader["Id"].ToString()); shoppingItem.Quantity = int.Parse(reader["Quantity"].ToString()); shoppingItem.Coffee = new Coffee() { Id = int.Parse(reader["CoffeeId"].ToString()) }; } } } if (shoppingItem != null) { shoppingItem.Coffee = await this.coffeeDataAccess.GetCoffeeById(coffeeId); } return(shoppingItem); }
public void RemoveFromBasket() { ShoppingBasketItem existingItem = Basket.FirstOrDefault(x => x.Product == SelectedProducts); if (existingItem != null) { if (existingItem.Quantity == 1) { SelectedProducts.ItemsLeft++; repo.UpdateQuantity(SelectedProducts); Basket.Remove(existingItem); } else if (existingItem.Quantity > 1) { existingItem.Quantity--; SelectedProducts.ItemsLeft++; repo.UpdateQuantity(SelectedProducts); } } NotifyOfPropertyChange(() => Total); NotifyOfPropertyChange(() => Basket); NotifyOfPropertyChange(() => CalculateChange); }
private ShoppingBasket ParseShoppingBasket(string rawData) { Guard.IsNotNullOrEmptyString(rawData, nameof(rawData)); string[] lines = rawData.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); var items = new List <ShoppingBasketItem>(); var parsingExceptions = new List <BaseReceiptGeneratorException>(); foreach (string line in lines) { try { ShoppingBasketItem newItem = ParseShoppingBasketItem(line.Trim()); items.Add(newItem); } catch (BaseReceiptGeneratorException ex) { parsingExceptions.Add(ex); } } if (parsingExceptions.Any()) { throw new ParseShoppingBasketAggregatedException(parsingExceptions); } ShoppingBasket shoppingBasket = new ShoppingBasket(); shoppingBasket.Items = items; return(shoppingBasket); }
public int InsertShoppingBasketItem(ShoppingBasket basket, ShoppingBasketItem item) { item.ItemId = _baskets.Count() > 0 ? (_baskets.SelectMany(b => b.OrderItems).Max(i => i.ItemId)) + 1 : 1; basket.OrderItems.Add(item); return(item.ItemId); }
public void UpdateItemOnBasket_WhenUpdatingItem_AndBasketExists_AndItemExists_AndNewQuantityIsZero_ThenItemIsRemoved() { var item = new ShoppingBasketItem { ProductId = 677, Price = 15, ProductName = "The Art of Unit Test Book", Quantity = 1 }; var itemId = _shoppingBasketService.AddItemToBasket(777, item); var secondItem = new ShoppingBasketItem { ProductName = "Golden Son Book", ProductId = 888, Price = 15.00m, Quantity = 2 }; _shoppingBasketService.AddItemToBasket(777, secondItem); _shoppingBasketService.UpdateItemOnBasket(777, itemId, 0); var basket = _shoppingBasketService.GetBasketForCustomer(777); Assert.AreEqual(basket.OrderItems.Count(), 1); Assert.IsFalse(basket.OrderItems.Exists(i => i.ItemId == itemId)); }
public void AddItemToBasket_WhenAddingAnItemToABasket_AndBasketExists_AndTheItemExists_ThenTheItemQuantityIsIncreasedByQuantityValueInNewItem() { var item = new ShoppingBasketItem { ProductName = "Golden Son Book", ProductId = 888, Price = 15.00m, Quantity = 1 }; _shoppingBasketService.AddItemToBasket(333, item); var items = _shoppingBasketService.GetBasketForCustomer(333).OrderItems; var basketItemsCount = items.Count(); var secondItem = new ShoppingBasketItem { ProductName = "Golden Son Book", ProductId = 888, Price = 15.00m, Quantity = 2 }; _shoppingBasketService.AddItemToBasket(333, secondItem); var basket = _shoppingBasketService.GetBasketForCustomer(333); Assert.AreEqual(basketItemsCount, basket.OrderItems.Count()); Assert.IsTrue(basket.OrderItems.Exists(i => i.ProductId == 888)); Assert.AreEqual(basket.OrderItems.First(i => i.ProductId == 888).Quantity, 3); }
/// <summary> /// Creates a list of tax rates for a given item /// </summary> /// <param name="item">The item to create tax rates for</param> /// <returns>A list of tax rates to be applied to the item</returns> public List <ITaxRate> CreateTaxRates(ShoppingBasketItem item) { List <ITaxRate> taxRates = new List <ITaxRate>(); switch (item.Product.Type) { case ProductType.Book: case ProductType.Food: case ProductType.Medical: taxRates.Add(new ExemptSalesTaxRate()); break; case ProductType.Other: default: taxRates.Add(new BasicSalesTaxRate()); break; } if (item.IsImported) { taxRates.Add(new ImportSalesTaxRate()); } return(taxRates); }
private ReceiptLine CreateReceiptLine(ShoppingBasketItem item) { Guard.IsNotNull(item, nameof(item)); string categoryName = GetItemCategoryName(item.Name); decimal tax = 0; decimal taxRatePercentage = 10; decimal importTaxRatePercentage = 5; if (!CategoryManager.IsCategoryExempt(categoryName)) { tax = item.Price * taxRatePercentage / 100; } if (item.Name.ToLower().StartsWith(Constants.ImportedWithSpace)) { tax += item.Price * importTaxRatePercentage / 100; } tax = RoundItemTax(tax); return(new ReceiptLine { Name = item.Name, Price = item.Price, Quantity = item.Quantity, Tax = tax }); }
public void Removing_An_Item_That_Doesnt_Exist_In_The_Basket_Will_Result_In_An_KeyNotFoundException_Being_Thrown(int itemId, string itemName, decimal itemPrice) { //Arrange IShoppingItem item = new ShoppingItem(itemId, itemName, itemPrice); //Act And Assert IShoppingBasketItem basketItem = _basket.AddItem(item); IShoppingBasketItem itemThatDoesntExistInBasket = new ShoppingBasketItem(itemId + 1, itemName + "1", itemPrice + 1, null, null); //Assert Assert.Throws <KeyNotFoundException>(() => _basket.RemoveItem(itemThatDoesntExistInBasket)); }
public void GetSalesTaxAmount_ItemIsImported_ReturnTaxAmt() { // Arrange var salesTaxRate = new SalesTaxRate(); var importDutySalesTaxCalculator = new ImportDutySalesTaxCalculator(salesTaxRate); var item = new ShoppingBasketItem(new Product("Candy", ProductType.Candy), 1, true, importDutySalesTaxCalculator); // Act var result = importDutySalesTaxCalculator.GetSalesTaxAmount(item); // Assert Assert.AreNotEqual(result, 0); }
public void CalculateSingleTax_WithBasicTaxAndRoundingRule_CalculatesTaxValue() { //arrange double price = 1.5; ShoppingBasketItem item = new ShoppingBasketItem(new Product("test", price)); double expectedTax = new RoundingTaxRule().ApplyTaxRule(new BasicSalesTaxRate().CalculateTax(price)); //act double actualTax = item.CalculateSingleItemTax(); //assert Assert.AreEqual(expectedTax, actualTax, "Single item tax incorrectly calculated for item with basic tax rate"); }
public void GetPriceWithTaxIncluded_ProductIsImported_ReturnPriceWithTax() { // Arrange var salesTaxRate = new SalesTaxRate(); var importDutySalesTaxCalculator = new ImportDutySalesTaxCalculator(salesTaxRate); var item = new ShoppingBasketItem(new Product("Coffee", ProductType.Coffee), 11.00m, true, importDutySalesTaxCalculator); // Act var result = importDutySalesTaxCalculator.GetPriceWithTaxIncluded(item); // Assert Assert.AreEqual(result, 11.55m); }
public void GetSalesTaxAmount_ProductTypeIsOther_ReturnTaxAmt() { // Arrange var salesTaxRate = new SalesTaxRate(); var basicSalesTaxCalculator = new BasicSalesTaxCalculator(salesTaxRate); var item = new ShoppingBasketItem(new Product("Other", ProductType.Other), 1, false, basicSalesTaxCalculator); // Act var result = basicSalesTaxCalculator.GetSalesTaxAmount(item); // Assert Assert.AreNotEqual(result, 0); }
public void GetPriceWithTaxIncluded_ProductIsTaxed_ReturnPriceWithTax() { // Arrange var salesTaxRate = new SalesTaxRate(); var basicSalesTaxCalculator = new BasicSalesTaxCalculator(salesTaxRate); var item = new ShoppingBasketItem(new Product("Other", ProductType.Other), 99.99m, false, basicSalesTaxCalculator); // Act var result = basicSalesTaxCalculator.GetPriceWithTaxIncluded(item); // Assert Assert.AreEqual(result, 109.99m); }
public void GetPriceWithTaxIncluded_ProductIsExempt_ReturnPrice() { // Arrange var salesTaxRate = new SalesTaxRate(); var basicSalesTaxCalculator = new BasicSalesTaxCalculator(salesTaxRate); var item = new ShoppingBasketItem(new Product("Coffee", ProductType.Coffee), 11.50m, false, basicSalesTaxCalculator); // Act var result = basicSalesTaxCalculator.GetPriceWithTaxIncluded(item); // Assert Assert.AreEqual(result, 11.50m); }
public void GetSalesTaxAmount_ProductTypeIsCoffee_ReturnZero() { // Arrange var salesTaxRate = new SalesTaxRate(); var basicSalesTaxCalculator = new BasicSalesTaxCalculator(salesTaxRate); var item = new ShoppingBasketItem(new Product("Coffee", ProductType.Coffee), 1, false, basicSalesTaxCalculator); // Act var result = basicSalesTaxCalculator.GetSalesTaxAmount(item); // Assert Assert.AreEqual(result, 0); }
public void CalculateSingleTax_WithImportTaxAndExemptTaxAndRoundingRule_CalculatesTaxValue() { //arrange double price = 100.99; ShoppingBasketItem item = new ShoppingBasketItem(new Product("test", price, ProductType.Food), true); double expectedTax = (new RoundingTaxRule().ApplyTaxRule(new ExemptSalesTaxRate().CalculateTax(price))) + (new RoundingTaxRule().ApplyTaxRule(new ImportSalesTaxRate().CalculateTax(price))); //act double actualTax = item.CalculateSingleItemTax(); //assert Assert.AreEqual(expectedTax, actualTax, "Single item tax incorrectly calculated for item with import and exempt tax rate"); }
public void GetItemDisplayName_WithNonImportItem_DisplaysProperName() { //arrange ShoppingBasketItem item = new ShoppingBasketItem(new Product("Test product", 100)); ShoppingBasketItemReport report = new ShoppingBasketItemReport(item); string expectedName = item.Product.Name; //act string actualName = report.GetItemDisplayName(); //assert Assert.AreEqual(expectedName, actualName, "Display name is incorrect for imported item"); }
public async Task <int> SaveShoppingBasketItem(ShoppingBasketItem shoppingItem) { using (DbCommand command = this.coffeeDatabase.GetStoredProcCommand("dbo.spSaveShoppingBasketItem")) { this.coffeeDatabase.AddInParameter(command, "@coffeeId", DbType.Int32, shoppingItem.Coffee.Id); this.coffeeDatabase.AddInParameter(command, "@quantity", DbType.Int32, shoppingItem.Quantity); this.coffeeDatabase.AddInParameter(command, "@shoppingBasketNumber", DbType.Guid, shoppingItem.ShoppingBasketId); Task <int> execNonQueryTask = Task <int> .Factory.FromAsync(this.coffeeDatabase.BeginExecuteNonQuery, this.coffeeDatabase.EndExecuteNonQuery, command, null); int result = await execNonQueryTask; return(result); } }
public void CalculateTotalTax_WithMultipleItems_CalculatesTaxValue() { //arrange double price = 45.35; int quantity = 4; ShoppingBasketItem item = new ShoppingBasketItem(new Product("test", price), quantity); double expectedTax = item.CalculateSingleItemTax() * quantity; //act double actualTax = item.CalculateTotalTax(); //assert Assert.AreEqual(expectedTax, actualTax, "total tax incorrectly calculated for item with quantity of 4"); }
public void CalculateTotalCost_WithMultipleItems_CalculatesTotalCost() { //arrange double price = 12.5; int quantity = 3; ShoppingBasketItem item = new ShoppingBasketItem(new Product("test", price), quantity); double expectedTax = (item.CalculateSingleItemTax() + price) * quantity; //act double actualTax = item.CalculateTotalCost(); //assert Assert.AreEqual(expectedTax, actualTax, "total cost incorrectly calculated for item with quantity of 3"); }
public void AddItem_WithNewBasket_UpdatesTotals() { //arrange ShoppingBasket basket = new ShoppingBasket(); ShoppingBasketItem item = new ShoppingBasketItem(new Product("test", 5.5, ProductType.Other)); double expetedTotalCost = item.CalculateTotalCost(); double expectedTotalTax = item.CalculateTotalTax(); //act basket.AddItem(item); //assert Assert.AreEqual(expectedTotalTax, basket.TotalTax, "Total tax not updated correctly"); Assert.AreEqual(expetedTotalCost, basket.TotalCost, "Total cost not update correctly"); }
public void GetSalesTaxAmount_ItemIsTaxedAndImported_ReturnBasicAndImportTax() { // Arrange var salesTaxRate = new SalesTaxRate(); var totalSalesTaxCalculator = new TotalSalesTaxCalculator(new List <SalesTaxCalculator> { new BasicSalesTaxCalculator(salesTaxRate), new ImportDutySalesTaxCalculator(salesTaxRate) }); var item = new ShoppingBasketItem(new Product("Other", ProductType.Other), 15001.25m, true, totalSalesTaxCalculator); // Act var result = totalSalesTaxCalculator.GetSalesTaxAmount(item); // Assert Assert.AreEqual(result, 2250.25m); }
public void AddItem_WithMultipleItems_UpdatesTotals() { //arrange ShoppingBasket basket = new ShoppingBasket(); ShoppingBasketItem item1 = new ShoppingBasketItem(new Product("test", 5.5, ProductType.Other)); ShoppingBasketItem item2 = new ShoppingBasketItem(new Product("test", 8.75, ProductType.Food)); double expetedTotalCost = item1.CalculateTotalCost() + item2.CalculateTotalCost(); double expectedTotalTax = item1.CalculateTotalTax() + item2.CalculateTotalTax(); //act basket.AddItem(item1); basket.AddItem(item2); //assert Assert.AreEqual(expectedTotalTax, basket.TotalTax, "Total tax not updated correctly"); Assert.AreEqual(expetedTotalCost, basket.TotalCost, "Total cost not update correctly"); }
public void GetSalesTaxAmount_ItemIsExemptAndImported_ReturnImportTax() { // Arrange var salesTaxRate = new SalesTaxRate(); var totalSalesTaxCalculator = new TotalSalesTaxCalculator(new List <SalesTaxCalculator> { new BasicSalesTaxCalculator(salesTaxRate), new ImportDutySalesTaxCalculator(salesTaxRate) }); var item = new ShoppingBasketItem(new Product("Candy", ProductType.Candy), 75.99m, true, totalSalesTaxCalculator); // Act var result = totalSalesTaxCalculator.GetSalesTaxAmount(item); // Assert Assert.AreEqual(result, 3.80m); }
public void GetPriceWithTaxIncluded_ItemIsExemptAndNotImported_ReturnPrice() { // Arrange var salesTaxRate = new SalesTaxRate(); var totalSalesTaxCalculator = new TotalSalesTaxCalculator(new List <SalesTaxCalculator> { new BasicSalesTaxCalculator(salesTaxRate), new ImportDutySalesTaxCalculator(salesTaxRate) }); var item = new ShoppingBasketItem(new Product("Candy", ProductType.Candy), 16.00m, false, totalSalesTaxCalculator); // Act var result = totalSalesTaxCalculator.GetPriceWithTaxIncluded(item); // Assert Assert.AreEqual(result, 16.00m); }
public void AddItemToBasket_WhenAddingAnItemToABasket_AndBasketExists_AndTheItemDoesNotExists_ThenTheItemIsAddedToTheBasket() { var basketItemsCount = _shoppingBasketService.GetBasketForCustomer(1).OrderItems.Count(); var item = new ShoppingBasketItem { ProductId = 677, Price = 15, ProductName = "The Art of Unit Test Book", Quantity = 1 }; _shoppingBasketService.AddItemToBasket(1, item); var basket = _shoppingBasketService.GetBasketForCustomer(1); Assert.AreEqual(basketItemsCount + 1, basket.OrderItems.Count()); Assert.IsTrue(basket.OrderItems.Exists(i => i.ProductId == 677)); }
public void UpdateItemOnBasket_WhenUpdatingItem_AndBasketExists_AndItemExists_AndNewQuantityGreaterThanZero_ThenItemQuantityIsUpdated() { var item = new ShoppingBasketItem { ProductId = 677, Price = 15, ProductName = "The Art of Unit Test Book", Quantity = 1 }; var itemId = _shoppingBasketService.AddItemToBasket(666, item); _shoppingBasketService.UpdateItemOnBasket(666, itemId, 5); var basket = _shoppingBasketService.GetBasketForCustomer(666); Assert.AreEqual(basket.OrderItems.First(i => i.ItemId == itemId).Quantity, 5); }