public void DiscountTest() { Receipt target = new Receipt(); // TODO: Initialize to an appropriate value StringBuilder expected = new StringBuilder(); //not in order on purpose. expected.Append(TEST1); expected.AppendFormat("\r\n{0}", TEST3); expected.AppendFormat("\r\n{0}", TEST2); expected.AppendFormat("\r\n{0}", TEST4); //configure remarks target.AddDiscount(TEST1); target.AddDiscount(TEST3); target.AddDiscount(TEST2); target.AddDiscount(TEST4); string actual; actual = target.Discount; string temp = expected.ToString(); Assert.AreEqual(temp.Length, actual.Length); Assert.AreEqual(temp.CompareTo(actual), 0); Assert.AreEqual <string>(temp, actual); }
/// <summary> /// /// </summary> public Receipt CalculateReceipt(string[] items) { Receipt result = new Receipt(); //zero the result StockKeepingUnits itemSKUs = new StockKeepingUnits(); foreach (var s in items) { StockKeepingUnit tempRef = fSKUs.FindSingletonSKU(s); if (tempRef != null) { itemSKUs.Add(new StockKeepingUnit(tempRef)); } else { result.AddRemark(String.Format("\"{0}\" was not recognised and skipped", s)); } } result.AddRemark(itemSKUs.ToString()); //now that we know what we are buting, we should apply the discounts result.Subtotal = itemSKUs.Price(); DiscountResults discountResults = fDiscounts.ApplyDiscounts(itemSKUs); result.Total = result.Subtotal - discountResults.Sum(); result.AddDiscount(discountResults.ToString()); return(result); }
public void AddDiscountTest() { Receipt target = new Receipt(); // TODO: Initialize to an appropriate value string discount = TEST1; // TODO: Initialize to an appropriate value target.AddDiscount(TEST1); Assert.AreEqual <string>(target.Discount, discount); }
public void HandleOffers(Receipt receipt, Dictionary <Product, Offer> offers, ISupermarketCatalog catalog) { foreach (var p in productQuantities.Keys) { double quantity = productQuantities[p]; if (offers.ContainsKey(p)) { var offer = offers[p]; var unitPrice = catalog.GetUnitPrice(p); var quantityAsInt = (int)quantity; Discount discount = null; int x = 1; if (offer.OfferType == SpecialOfferType.ThreeForTwo) { x = 3; } else if (offer.OfferType == SpecialOfferType.TwoForAmount) { x = 2; if (quantityAsInt >= 2) { var total = offer.Argument * (quantityAsInt / x) + quantityAsInt % 2 * unitPrice; var discountN = unitPrice * quantity - total; discount = new Discount(p, "2 for " + offer.Argument, discountN); } } if (offer.OfferType == SpecialOfferType.FiveForAmount) { x = 5; } int numberOfXs = quantityAsInt / x; if (offer.OfferType == SpecialOfferType.ThreeForTwo && quantityAsInt > 2) { var discountAmount = quantity * unitPrice - ((numberOfXs * 2 * unitPrice) + quantityAsInt % 3 * unitPrice); discount = new Discount(p, "3 for 2", discountAmount); } if (offer.OfferType == SpecialOfferType.TenPercentDiscount) { discount = new Discount(p, offer.Argument + "% off", quantity * unitPrice * offer.Argument / 100.00); } if (offer.OfferType == SpecialOfferType.FiveForAmount && quantityAsInt >= 5) { var discountTotal = unitPrice * quantity - (offer.Argument * numberOfXs + quantityAsInt % 5 * unitPrice); discount = new Discount(p, x + " for " + offer.Argument, discountTotal); } if (discount != null) { receipt.AddDiscount(discount); } } } }
public void DiscountTest2() { Receipt target = new Receipt(); // TODO: Initialize to an appropriate value StringBuilder expected = new StringBuilder(); //not in order on purpose. expected.AppendLine(TEST1); expected.AppendLine(TEST2); expected.AppendLine(TEST3); expected.AppendLine(TEST4); //configure remarks target.AddDiscount(TEST1); target.AddDiscount(TEST3); target.AddDiscount(TEST2); target.AddDiscount(TEST4); string actual; actual = target.Discount; Assert.AreNotEqual <string>(expected.ToString(), actual); }