public void TestGivenRecipeScenarios() { // recipe 1: var recipeItems = new List <IRecipeItem>(); recipeItems.Add(new ProduceItem("garlic", "clove", true, 0.67, 1)); recipeItems.Add(new ProduceItem("lemon", "whole", false, 2.03, 1)); recipeItems.Add(new PantryItem("olive oil", "cup", true, 1.92, 0.75)); recipeItems.Add(new PantryItem("salt", "teaspoon", false, 0.16, 0.75)); recipeItems.Add(new PantryItem("pepper", "teaspoon", false, 0.17, 0.5)); var baseCost = CalcRecipeCosts.CalculateCost(recipeItems); var wellnessDiscount = CalcRecipeCosts.CalculateWellnessDiscount(recipeItems, 0.05); var salestax = CalcRecipeCosts.CalculateSalesTax(recipeItems, 0.086, 7); Assert.IsTrue(wellnessDiscount == 0.11); Assert.IsTrue(salestax == 0.21); Assert.IsTrue(Utilities.RoundUp(baseCost + salestax - wellnessDiscount, 2) == 4.45); // recipe 2 recipeItems = new List <IRecipeItem>(); recipeItems.Add(new ProduceItem("garlic", "clove", true, 0.67, 1)); recipeItems.Add(new MeatPoultryItem("chicken breast", "whole", false, 2.19, 4)); recipeItems.Add(new PantryItem("olive oil", "cup", true, 1.92, 0.5)); recipeItems.Add(new PantryItem("vinegar", "cup", false, 1.26, 0.5)); baseCost = CalcRecipeCosts.CalculateCost(recipeItems); wellnessDiscount = CalcRecipeCosts.CalculateWellnessDiscount(recipeItems, 0.05); salestax = CalcRecipeCosts.CalculateSalesTax(recipeItems, 0.086, 7); Assert.IsTrue(wellnessDiscount == 0.09); Assert.IsTrue(salestax == 0.91); var total = Utilities.RoundUp(baseCost + salestax - wellnessDiscount, 2); // note: I had an off by 1 error in the processor where the total was calculated to be 11.020000000000001 which rounds up to 11.03, hence the need for both to pass. Assert.IsTrue(total == 11.84 || total == 11.85); // recipe 3 recipeItems = new List <IRecipeItem>(); recipeItems.Add(new ProduceItem("garlic", "clove", true, 0.67, 1)); recipeItems.Add(new ProduceItem("corn", "cup", false, 0.87, 4)); recipeItems.Add(new MeatPoultryItem("bacon", "slice", false, 0.24, 4)); recipeItems.Add(new PantryItem("pasta", "ounce", false, 0.31, 8)); recipeItems.Add(new PantryItem("olive oil", "cup", true, 1.92, Utilities.Convert("1/3"))); recipeItems.Add(new PantryItem("salt", "teaspoon", false, 0.16, Utilities.Convert("1 1/4"))); recipeItems.Add(new PantryItem("pepper", "teaspoon", false, 0.17, Utilities.Convert("3/4"))); baseCost = CalcRecipeCosts.CalculateCost(recipeItems); wellnessDiscount = CalcRecipeCosts.CalculateWellnessDiscount(recipeItems, 0.05); salestax = CalcRecipeCosts.CalculateSalesTax(recipeItems, 0.086, 7); Assert.IsTrue(wellnessDiscount == 0.07); Assert.IsTrue(salestax == 0.42); total = Utilities.RoundUp(baseCost + salestax - wellnessDiscount, 2); Assert.IsTrue(Utilities.RoundUp(baseCost + salestax - wellnessDiscount, 2) == 8.91); }
static void Main(string[] args) { IDictionary <string, IRecipeItem> ingredientsList; IDictionary <string, IDictionary <string, double> > recipeList; var settings = LoadSettings(); LoadRecipes(out ingredientsList, out recipeList); if (ingredientsList == null || recipeList == null) { throw new Exception("ingredients or recipe list did not load correctly"); } var sbResults = new StringBuilder(); foreach (var recipeName in recipeList.Keys) { var recipeItemsWithUnits = new List <IRecipeItem>(); foreach (var ingredientName in recipeList[recipeName].Keys) { if (!ingredientsList.ContainsKey(ingredientName)) { throw new Exception("Ingredient " + ingredientName + " does not exist in Ingredient list!"); } // get a deep copy of the ingredient so we dont alter unit amounts var ingredient = facIngredientItem.GetItemOfType(ingredientsList[ingredientName], recipeList[recipeName][ingredientName]); recipeItemsWithUnits.Add(ingredient); } // Calculate Recipe Cost var recipeBaseCost = CalcRecipeCosts.CalculateCost(recipeItemsWithUnits); var recipeSalestax = CalcRecipeCosts.CalculateSalesTax(recipeItemsWithUnits, settings.SalesTaxPercent, settings.SalesTaxRoundTo); var recipeWellnessDiscount = CalcRecipeCosts.CalculateWellnessDiscount(recipeItemsWithUnits, settings.WellnessDiscountPercent); sbResults.AppendLine(recipeName); // TAX var sbTmp = new StringBuilder("\tTax = $"); sbTmp.Append(recipeSalestax); sbResults.AppendLine(sbTmp.ToString()); // DISCOUNT sbTmp = new StringBuilder("\tDiscount = ($"); sbTmp.Append(recipeWellnessDiscount); sbTmp.Append(")"); sbResults.AppendLine(sbTmp.ToString()); // TOTAL sbTmp = new StringBuilder("\tTotal = $"); var total = Utilities.RoundUp(recipeBaseCost + recipeSalestax - recipeWellnessDiscount, 2); sbTmp.Append(total); sbResults.AppendLine(sbTmp.ToString()); sbResults.AppendLine(String.Empty); } Console.Write(sbResults.ToString()); Console.WriteLine("I noticed that the 11.84 becomes 11.85, this is because the total was calculated to be 11.020000000000001 which rounds up to 11.03, I think the discrepency is minor and the code is correct :)"); Console.Read(); }