Example #1
0
        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);
        }
Example #2
0
        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();
        }
Example #3
0
        public void TestCalcRecipeCosts()
        {
            try
            {
                var items = new List <IRecipeItem>();
                items.Add(new ProduceItem("nonorganic1", "can", false, 2.12, 1));
                items.Add(new ProduceItem("nonorganic2", "jar", false, 3.13, 1));

                Assert.IsTrue(CalcRecipeCosts.CalculateWellnessDiscount(items, 100) == 0);
                items.Add(new ProduceItem("organic1", "leaf", true, 3.45, 1));
                Assert.IsTrue(CalcRecipeCosts.CalculateSalesTax(items, 100, 10) == 0);
                var curWellnessDiscount = CalcRecipeCosts.CalculateWellnessDiscount(items, 100);
                Assert.IsTrue(curWellnessDiscount > 0);
                items.Add(new ProduceItem("organic2", "clove", true, 5.73, 1));
                var curWellnessDiscount2 = CalcRecipeCosts.CalculateWellnessDiscount(items, 100);
                Assert.IsTrue(curWellnessDiscount2 > curWellnessDiscount);
                items.Add(new ProduceItem("nonorganic3", "bag", false, 2.53, 1));
                items.Add(new ProduceItem("nonorganic4", "box", false, 1.18, 1));
                Assert.IsTrue(CalcRecipeCosts.CalculateSalesTax(items, 100, 10) == 0);
                items.Add(new MeatPoultryItem("nonorganic5", "slab", false, 5.18, 1));
                var curTax = CalcRecipeCosts.CalculateSalesTax(items, 100, 10);
                Assert.IsTrue(curTax > 0);
                // adding 0 units of the recipe item
                items.Add(new PantryItem("organic4", "gallon", true, 11.21, 0));
                Assert.IsTrue(CalcRecipeCosts.CalculateSalesTax(items, 100, 10) == curTax);
                Assert.IsTrue(CalcRecipeCosts.CalculateWellnessDiscount(items, 100) == curWellnessDiscount2);
                // add 2 units now
                items.Add(new PantryItem("organic5", "gallon", true, 11.21, 2));
                Assert.IsTrue(CalcRecipeCosts.CalculateSalesTax(items, 100, 10) > curTax);
                Assert.IsTrue(CalcRecipeCosts.CalculateWellnessDiscount(items, 100) > curWellnessDiscount2);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
                Assert.IsTrue(false);
            }
        }