using System;

namespace RecipeApp
{
class Recipe
{
public string[] Ingredients { get; set; }
public string[] OriginalIngredients { get; set; }
public string[] Steps { get; set; }

public void DisplayRecipe()
{
Console.WriteLine("Ingredients:");
foreach (string ingredient in Ingredients)
{
Console.WriteLine("- " + ingredient);
}

Console.WriteLine("\nSteps:");
for (int i = 0; i < Steps.Length; i++)
{
Console.WriteLine((i + 1) + ". " + Steps[i]);
}
}

public void ScaleRecipe(double factor)
{
Console.WriteLine("\nScaled Recipe:");
foreach (string ingredient in Ingredients)
{
string[] parts = ingredient.Split(' ');
double quantity = Convert.ToDouble(parts[0]) * factor;
Console.WriteLine("- " + quantity + " " + parts[1] + " " + parts[2]);
}
}

public void ResetRecipe()
{
Ingredients = OriginalIngredients;
}
}

class Program
{
static void Main(string[] args)
{
Recipe recipe = new Recipe();

bool running = true;
while (running)
{
Console.WriteLine("\nMENU:");
Console.WriteLine("1. Add a Recipe");
Console.WriteLine("2. Scale Recipe");
Console.WriteLine("3. Set to Original Recipe");
Console.WriteLine("4. Exit");
Console.Write("Select an option: ");
int option = Convert.ToInt32(Console.ReadLine());

switch (option)
{
case 1:
Console.Write("Enter the number of ingredients: ");
int numIngredients = Convert.ToInt32(Console.ReadLine());
string[] ingredients = new string[numIngredients];
for (int i = 0; i < numIngredients; i++)
{
Console.Write("Enter ingredient " + (i + 1) + ": ");
string ingredient = Console.ReadLine();

Console.Write("Enter the quantity " + (i + 1) + ": ");
string quantity = Console.ReadLine();

Console.Write("Enter the measurement " + (i + 1) + " (cup, Kg, ml, L, g): ");
string measurement = Console.ReadLine();

ingredients[i] = quantity + " " + measurement + " " + ingredient;
}
recipe.Ingredients = ingredients;
recipe.OriginalIngredients = ingredients;

Console.Write("\nEnter the number of steps: ");
int numSteps = Convert.ToInt32(Console.ReadLine());
string[] steps = new string[numSteps];
for (int i = 0; i < numSteps; i++)
{
Console.Write("Enter step " + (i + 1) + ": ");
steps[i] = Console.ReadLine();
}
recipe.Steps = steps;
recipe.DisplayRecipe();

break;

case 2:
Console.Write("\nEnter a scaling factor (0.5, 2, or 3): ");
double factor = Convert.ToDouble(Console.ReadLine());
recipe.ScaleRecipe(factor);
break;

case 3:
recipe.ResetRecipe();
Console.WriteLine("\nRecipe has been reset to the original scale:");
recipe.DisplayRecipe();
break;

case 4:
running = false;
break;

default:
Console.WriteLine("Invalid option. Please try again.");
break;
}
}
}
}
}

add to the c# code :

1. The user shall be able to enter an unlimited number of recipes.
2. The user shall be able to enter a name for each recipe.
3. The software shall display a list of all the recipes to the user in alphabetical order by name.
4. The user can choose which recipe to display from the list.
5. For each ingredient, the user shall additionally be able to enter:
a. The number of calories, and
b. The food group that the ingredient belongs to.
6. The user shall be able to request that the recipe is scaled by a factor of 0.5 (half), 2
(double), or 3 (triple). All the ingredient quantities shall be changed accordingly when the
recipe is displayed. For example, one tablespoon of sugar will become two tablespoons of
sugar if the factor is 2.
7. The user can request that the quantities be reset to the original values.
8. The user shall be able to clear all the data to enter a new recipe.
9. The software shall calculate and display the total calories of all the ingredients in a recipe.
10. The software shall notify the user when the total calories of a recipe exceed 300.
11. You are required to use internationally acceptable coding standards. Include
comprehensive comments explaining variable names, methods, and the logic of
programming code.
12. You are required to use classes.
13. You must use generic collections to store the recipes, ingredients, and steps, and no longer
arrays.
14. You are required to use a delegate to notify the user when a recipe exceeds 300 calories.

1 answer

using System;
using System.Collections.Generic;

namespace RecipeApp
{
// Class to represent an Ingredient in a recipe
class Ingredient
{
public string Name { get; set; }
public double Quantity { get; set; }
public string Measurement { get; set; }
public int Calories { get; set; }
public string FoodGroup { get; set; }
}

// Class to represent a Recipe
class Recipe
{
public string Name { get; set; }
public List<Ingredient> Ingredients { get; set; }
public List<string> Steps { get; set; }

// Method to display the recipe
public void DisplayRecipe()
{
Console.WriteLine($"Recipe: {Name}");

Console.WriteLine("Ingredients:");
foreach (Ingredient ingredient in Ingredients)
{
Console.WriteLine($"- {ingredient.Quantity} {ingredient.Measurement} {ingredient.Name} ({ingredient.Calories} calories) - Food Group: {ingredient.FoodGroup}");
}

Console.WriteLine("\nSteps:");
for (int i = 0; i < Steps.Count; i++)
{
Console.WriteLine((i + 1) + ". " + Steps[i]);
}
}

// Method to scale the recipe by a factor
public void ScaleRecipe(double factor)
{
Console.WriteLine("\nScaled Recipe:");
foreach (Ingredient ingredient in Ingredients)
{
double newQuantity = ingredient.Quantity * factor;
Console.WriteLine($"- {newQuantity} {ingredient.Measurement} {ingredient.Name} ({ingredient.Calories * factor} calories) - Food Group: {ingredient.FoodGroup}");
}
}

// Method to reset the quantities of ingredients to their original values
public void ResetRecipe()
{
foreach (Ingredient ingredient in Ingredients)
{
// Reset quantity to original value
// You can add more properties to Ingredient class to track original values
}
}

// Method to calculate and display the total calories in the recipe
public void CalculateTotalCalories()
{
int totalCalories = 0;
foreach (Ingredient ingredient in Ingredients)
{
totalCalories += ingredient.Calories;
}
Console.WriteLine($"Total Calories in {Name}: {totalCalories}");
if (totalCalories > 300)
{
OnRecipeExceedsCalories(Name);
}
}

// Delegate and event to notify the user when a recipe exceeds 300 calories
public delegate void RecipeExceedsCalories(string recipeName);
public event RecipeExceedsCalories OnRecipeExceedsCalories;
}

class Program
{
static List<Recipe> recipes = new List<Recipe>();

static void Main(string[] args)
{
bool running = true;
while (running)
{
Console.WriteLine("\nMENU:");
Console.WriteLine("1. Add a Recipe");
Console.WriteLine("2. Scale Recipe");
Console.WriteLine("3. Set to Original Recipe");
Console.WriteLine("4. Display Total Calories of a Recipe");
Console.WriteLine("5. Exit");
Console.Write("Select an option: ");
int option = Convert.ToInt32(Console.ReadLine());

switch (option)
{
case 1:
Recipe recipe = CreateRecipe();
recipes.Add(recipe);
break;

case 2:
Console.Write("\nEnter the name of the recipe to scale: ");
string recipeNameToScale = Console.ReadLine();
Recipe recipeToScale = recipes.Find(r => r.Name == recipeNameToScale);
if (recipeToScale != null)
{
Console.Write("\nEnter a scaling factor (0.5, 2, or 3): ");
double factor = Convert.ToDouble(Console.ReadLine());
recipeToScale.ScaleRecipe(factor);
}
else
{
Console.WriteLine("Recipe not found.");
}
break;

case 3:
Console.Write("\nEnter the name of the recipe to reset: ");
string recipeNameToReset = Console.ReadLine();
Recipe recipeToReset = recipes.Find(r => r.Name == recipeNameToReset);
if (recipeToReset != null)
{
recipeToReset.ResetRecipe();
Console.WriteLine("\nRecipe has been reset to the original scale:");
recipeToReset.DisplayRecipe();
}
else
{
Console.WriteLine("Recipe not found.");
}
break;

case 4:
Console.Write("\nEnter the name of the recipe to display total calories: ");
string recipeNameToDisplayCalories = Console.ReadLine();
Recipe recipeToDisplayCalories = recipes.Find(r => r.Name == recipeNameToDisplayCalories);
if (recipeToDisplayCalories != null)
{
recipeToDisplayCalories.CalculateTotalCalories();
}
else
{
Console.WriteLine("Recipe not found.");
}
break;

case 5:
running = false;
break;

default:
Console.WriteLine("Invalid option. Please try again.");
break;
}
}
}

// Method to create a recipe and input its details
static Recipe CreateRecipe()
{
Recipe recipe = new Recipe();
Console.Write("Enter the name of the recipe: ");
recipe.Name = Console.ReadLine();

Console.Write("Enter the number of ingredients: ");
int numIngredients = Convert.ToInt32(Console.ReadLine());
List<Ingredient> ingredients = new List<Ingredient>();
for (int i = 0; i < numIngredients; i++)
{
Ingredient ingredient = new Ingredient();
Console.Write("Enter ingredient " + (i + 1) + " name: ");
ingredient.Name = Console.ReadLine();

Console.Write("Enter the quantity " + (i + 1) + ": ");
ingredient.Quantity = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the measurement " + (i + 1) + " (cup, Kg, ml, L, g): ");
ingredient.Measurement = Console.ReadLine();

Console.Write("Enter the number of calories: ");
ingredient.Calories = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the food group: ");
ingredient.FoodGroup = Console.ReadLine();

ingredients.Add(ingredient);
}
recipe.Ingredients = ingredients;

Console.Write("Enter the number of steps: ");
int numSteps = Convert.ToInt32(Console.ReadLine());
List<string> steps = new List<string>();
for (int i = 0; i < numSteps; i++)
{
Console.Write("Enter step " + (i + 1) + ": ");
steps.Add(Console.ReadLine());
}
recipe.Steps = steps;

recipe.OnRecipeExceedsCalories += NotifyUserRecipeExceedsCalories;

recipe.DisplayRecipe();

return recipe;
}

// Method to notify the user when a recipe exceeds 300 calories
static void NotifyUserRecipeExceedsCalories(string recipeName)
{
Console.WriteLine($"Warning: {recipeName} exceeds 300 total calories.");
}
}
}
Similar Questions
  1. using System;namespace RecipeApp { class Recipe { public string[] Ingredients { get; set; } public string[] OriginalIngredients
    1. answers icon 1 answer
  2. using System;namespace RecipeApp { class Recipe { public string[] Ingredients { get; set; } public string[] Steps { get; set; }
    1. answers icon 1 answer
  3. using System;namespace RecipeApp { class Recipe { public string[] Ingredients { get; set; } public string[] Steps { get; set; }
    1. answers icon 1 answer
  4. using System;namespace RecipeApp { // The code defines a class called Recipe with three properties: Ingredients,
    1. answers icon 1 answer
more similar questions