/** * Given an item and a number of units, work out the cost. */ private double GetSubtotalForItem(Item item, int itemCount) { if (!item.HasSpecialOffer()) { return(item.GetPrice() * itemCount); } else { int numberOfSpecials = itemCount / item.GetSpecialOfferMultiple(); int numberOfRegulars = itemCount % item.GetSpecialOfferMultiple(); return(numberOfSpecials * item.GetSpecialOfferPrice() + numberOfRegulars * item.GetPrice()); } }
public void TestConstructorWithoutSpecialOffer() { Supermarket.Item item = new Supermarket.Item("Rob", 456); Assert.AreEqual(item.GetSku(), "Rob"); Assert.AreEqual(item.GetPrice(), 456); Assert.IsFalse(item.HasSpecialOffer()); Assert.AreEqual(item.GetSpecialOfferMultiple(), 0); Assert.AreEqual(item.GetSpecialOfferPrice(), 0); }
public void TestConstructorWithSpecialOffer() { Supermarket.Item item = new Supermarket.Item("Bob", 123, 2, 210); Assert.AreEqual(item.GetSku(), "Bob"); Assert.AreEqual(item.GetPrice(), 123); Assert.IsTrue(item.HasSpecialOffer()); Assert.AreEqual(item.GetSpecialOfferMultiple(), 2); Assert.AreEqual(item.GetSpecialOfferPrice(), 210); }