Question: create a unit test to test the total calory calculation , to use classes and to do a proper error handling and data validation

 create a unit test to test the total calory calculation ,  to use classes and   to do a proper error handling and data validation . 

The user shall be able to enter an unlimited number of recipes.

The user shall be able to enter a name for each recipe.

The software shall display a list of all the recipes to the user in alphabetical order by name.

The user can choose which recipe to display from the list.

For each ingredient, the user shall additionally be able to enter:

The number of calories, and

The food group that the ingredient belongs to.

The software shall calculate and display the total calories of all the ingredients in a recipe.

The software shall notify the user when the total calories of a recipe exceed 300.                                                                        You must use generic collections to store the recipes, ingredients, steps.

use a delegate to notify the user when a recipe exceeds 300 calories.



 

using System;
using System.Numerics;

namespace RecipeProgram
{
   // Class Ingredient

   class Ingredient
   {
       // Variables of Ingredient class
       public string Ingredientname;
       public float Ingredientquantity;
       public string Ingredientunit;
       public float Ingredientrscale;

   }

   // Class Step

   class Step
   {
       // Variable step description

       public string description;
   }

   // Class StRecipe
   class Recipe
   {

       private Ingredient[] ingredients;


       private Step[] steps;

       // add Ingredients methods
       public void AddIngredients()
       {
           Console.Write("Enter number of ingredients: ");
           int numIngredients = int.Parse(Console.ReadLine());
           ingredients = new Ingredient[numIngredients];

           for (int i = 0; i < numIngredients; i++)
           {
               Ingredient ingredient = new Ingredient();

               Console.Write($"Enter ingredient {i + 1} name: ");
               ingredient.Ingredientname = Console.ReadLine();

               Console.Write($"Enter ingredient {i + 1} quantity: ");
               ingredient.Ingredientquantity = float.Parse(Console.ReadLine());

               Console.Write($"Enter ingredient {i + 1} unit: ");
               ingredient.Ingredientunit = Console.ReadLine();

               ingredients[i] = ingredient;
           }
       }
       //add Steps methods
       public void AddStep()
       {
           Console.Write("Enter number of steps: ");
           int numSteps = int.Parse(Console.ReadLine());
           steps = new Step[numSteps];

           for (int i = 0; i < numSteps; i++)
           {
               Step step = new Step();

               Console.Write($"Enter your step {i + 1}: ");
               step.description = Console.ReadLine();

               steps[i] = step;
           }
       }

       // methods ScaleRecipe
       public void RecipeScale(float factor)
       {
           Console.WriteLine("*********************************");
           Console.WriteLine("\nPlease select a scaling factor:");
           Console.WriteLine("1. Half");
           Console.WriteLine("2. Double");
           Console.WriteLine("3. Triple");

           factor = float.Parse(Console.ReadLine());

           foreach (Ingredient ingredient in ingredients)
           {

               if (factor == 1)
               {
                   ingredient.Ingredientquantity *= 0.5f;
               }
               else if (factor == 2)
               {
                   ingredient.Ingredientquantity *= 2;
               }
               else if (factor == 3)
               {
                   ingredient.Ingredientquantity *= 3;
               }
           }

           Console.WriteLine($"Recipe scaled by factor of {factor}.\n");
           RecipeDisplay();
       }


       // methods Reset recipe

       public void ResetQuantities()
       {
           Console.WriteLine("**************************************");
           Console.WriteLine("Quantities reset to original values.\n");
           ingredients.Clone();
           steps.Clone();
           RecipeDisplay();
       }


       //  methods Clear recipe
       public void RecipeClear()
       {

           ingredients = null;
           steps = null;

           Console.WriteLine("Recipe cleared.\n");


       }
       //  methods  display recipe
       public void RecipeDisplay()
       {
           Console.WriteLine("**************************************");
           Console.WriteLine("\nFull Recipe:");
           Console.WriteLine("=====================");
           Console.WriteLine("Ingredients:");

           foreach (Ingredient ingredient in ingredients)
           {
               Console.WriteLine($"{ingredient.Ingredientname}: {ingredient.Ingredientquantity} {ingredient.Ingredientunit}");
           }
           Console.WriteLine("----------------------");
           Console.WriteLine("\nSteps:");

           for (int i = 0; i < steps.Length; i++)
           {
               Console.WriteLine($"{i + 1}. {steps[i].description}");
           }
       }
   }

   
   class Run
   {
       // Main Method
       static void Main(string[] args)
       {

           St10073671Recipe recipe = new St10073671Recipe();
           //
           while (true)
           {
               Console.WriteLine("==============================");
               Console.WriteLine("\nRecipe Program\n");
               Console.WriteLine("1. Add Ingredients");
               Console.WriteLine("2. Add Steps");
               Console.WriteLine("3. Display Recipe");
               Console.WriteLine("4. Scale Recipe");
               Console.WriteLine("5. Reset Quantities");
               Console.WriteLine("6. Clear Recipe");
               Console.WriteLine("7. Exit");
               Console.WriteLine("--------------------------------");
               Console.Write("\nEnter selection: ");
               int selection = int.Parse(Console.ReadLine());

               switch (selection)
               {
                   // add Ingredient
                   case 1:
                       recipe.AddIngredients();
                       break;
                   //  add Step
                   case 2:
                       recipe.AddStep();
                       break;
                   //  Display recipe
                   case 3:
                       recipe.RecipeDisplay();
                       break;
                   //  scaling the order
                   case 4:
                       Console.WriteLine("**************************************************************************");
                       Console.Write("Press (1) to  confirm your scale oder and Press (2) to Cancel your scale order : ");
                       float factor = float.Parse(Console.ReadLine());

                       if (factor == 1)
                       {
                           Console.WriteLine("**************************************************************");
                           Console.WriteLine("You have scale your oder  all the Recipe program succesfully");
                           recipe.RecipeScale(factor);
                       }
                       if (factor > 1)
                       {
                           Console.WriteLine("**************************************************************************");
                           Console.WriteLine("You have Cancel to scale  all your oder for  the Recipe program succesfully ");
                           recipe.RecipeDisplay();
                       }
                       break;
                   //  Reset Quantities
                   case 5:

                       Console.WriteLine(" quantitie reset to the original value .\n");

                       recipe.ResetQuantities();

                       break;
                   //  Clear Recipe
                   case 6:
                       Console.WriteLine("**********************************************************************************");
                       Console.Write("Press the number  1 to  confirm  you want to clear all recipes and Press 2 to Cancel: ");

                       float confirmclear = float.Parse(Console.ReadLine());

                       if (confirmclear == 1)
                       {
                           recipe.RecipeClear();
                           Console.WriteLine("*************************************************");
                           Console.WriteLine("You have Clear all the Recipe program succesfully");
                       }
                       if (confirmclear > 1)
                       {
                           Console.WriteLine("************************************************************");
                           Console.WriteLine("You have Cancel to Clear all the Recipe program succesfully ");
                           recipe.RecipeDisplay();
                       }

                       break;
                   // Exit
                   case 7:
                       Environment.Exit(0);
                       Console.WriteLine("********************************************");
                       Console.WriteLine("You have Exit the Recipe program succesfully");
                       break;
                   // default
                   default:
                       Console.WriteLine("Invalid selection.");
                       break;
               }
           }
       }
   }
}                                        .                                                                                                                                   


Step by Step Solution

3.48 Rating (161 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To create unit tests for the total calorie calculation error handling and data validation we need to refactor the code to make it more testable and structured Heres an updated version of your code wit... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!