public PostageResult CalculatePostageFor(Basket basket) { if (basket == null) { throw new ArgumentNullException("basket"); } var postages = postageRepository.GetAll(); var postZone = basket.Country.PostZone; var totalWeight = (int)basket.BasketItems .Sum(bi => bi.TotalWeight); var postageToApply = postages .Where(p => totalWeight <= p.MaxWeight && p.IsActive) .OrderBy(p => p.MaxWeight) .FirstOrDefault(); var postageDescription = string.Format("for {0}", basket.Country.Name); if (postageToApply == null) return PostageResult.WithDefault(postZone, postageDescription); var multiplier = postZone.Multiplier; var total = new Money(Math.Round(postageToApply.Price.Amount * multiplier, 2, MidpointRounding.AwayFromZero)); return PostageResult.WithPrice(total, postageDescription); }
public ActionResult MoneyBindingTest(Money money) { if (ModelState.IsValid) { return Content(string.Format("Bound to Money value: {0}", money)); } return Content(ModelState.SelectMany(ms => ms.Value.Errors).Aggregate("", (agg, error) => agg + error.ErrorMessage + " ")); }
public void Equals_should_work() { var result1 = new Money(123.45M) == new Money(123.45M); result1.ShouldBeTrue(); var result2 = new Money(123.45M) == null; result2.ShouldBeFalse(); var result3 = null == new Money(123.45M); result3.ShouldBeFalse(); var result4 = new Money(123.45M) == new Money(123.46M); result4.ShouldBeFalse(); }
public void TotalWithPostage_should_return_postageResult_postage() { var postageCost = new Money(5.66M); var itemPrice = new Money(101.43M); var order = new Order { Postage = PostageResult.WithPrice(postageCost, "postage desc") }; order.AddLine("line1", 1, itemPrice, "", 1, ""); var expectedTotalWithPostage = postageCost + itemPrice; order.TotalWithPostage.ShouldEqual(expectedTotalWithPostage.ToStringWithSymbol()); }
public void Total_should_return_sum_of_order_line_totals() { var price1 = new Money(3.40M); var price2 = new Money(6.23M); var price3 = new Money(10.44M); var order = new Order(); order.AddLine("line1", 2, price1, "", 1, ""); order.AddLine("line2", 1, price2, "", 1, ""); order.AddLine("line3", 3, price3, "", 1, ""); order.AddLine("line4", 2, price1, "", 1, ""); var expectedTotal = (2*price1) + (1*price2) + (3*price3) + (2*price1); order.Total.ShouldEqual(expectedTotal); }
public void Should_be_able_to_add_an_OrderLine_to_an_Order() { const string itemName = "Large Black Mac"; const int quantity = 4; var price = new Money(23.45M); const string urlName = "Large_Black_Mac"; const int productId = 999; const string sizeName = "Large"; order.AddLine(itemName, quantity, price, urlName, productId, sizeName); order.OrderLines[0].ProductName.ShouldEqual("Large Black Mac"); order.OrderLines[0].Quantity.ShouldEqual(4); order.OrderLines[0].Price.Amount.ShouldEqual(23.45M); order.OrderLines[0].Order.ShouldBeTheSameAs(order); order.OrderLines[0].ProductUrlName.ShouldEqual(urlName); order.OrderLines[0].ProductId.ShouldEqual(productId); order.OrderLines[0].SizeName.ShouldEqual(sizeName); }
public virtual void AddLine(string productName, int quantity, Money price, string productUrlName, int productId, string sizeName) { if (productName == null) { throw new ArgumentNullException("productName"); } if (quantity == 0) { throw new ArgumentException("quantity can not be zero"); } if (productUrlName == null) { throw new ArgumentNullException("productUrlName"); } if (sizeName == null) { throw new ArgumentNullException("sizeName"); } var orderLine = new OrderLine { ProductName = productName, Quantity = quantity, Price = price, Order = this, ProductUrlName = productUrlName, ProductId = productId, SizeName = sizeName }; OrderLines.Add(orderLine); }
public void Should_be_able_to_do_other_arithmetic_with_money() { var x = new Money(43.5M); var result = x * 3 + x/2 - 4; result.Amount.ShouldEqual(148.25M); }
public void Should_be_able_to_add_money() { var result = new Money(2.32M) + new Money(3.44M); result.Amount.ShouldEqual(5.76M); }
public static PostageResult WithPrice(Money price, string description) { return new PostageResult { Phone = false, Price = price, Description = description }; }
public bool Equals(Money otherMoney) { if (otherMoney == null) return false; return Amount.Equals(otherMoney.Amount); }