from dataclasses import dataclass
import random
from typing import List
@dataclass
class Ingredient:
"""Represents an ingredient with a name, quantity, and unit."""
name: str
quantity: float
unit: str
def __str__(self):
return f"{self.quantity} {self.unit} of {self.name}"
@dataclass
class RecipeStep:
"""Represents a single step in the recipe."""
step_number: int
instruction: str
def __str__(self):
return f"Step {self.step_number}: {self.instruction}"
class TomatoPastaRecipe:
"""Sophisticated tomato pasta recipe generator."""
def __init__(self):
self.ingredients: List[Ingredient] = []
self.steps: List[RecipeStep] = []
self.recipe_name = "Tomato Pasta"
def add_ingredient(self, name: str, quantity: float, unit: str):
"""Adds an ingredient to the recipe."""
self.ingredients.append(Ingredient(name, quantity, unit))
def add_step(self, instruction: str):
"""Adds a step to the recipe."""
self.steps.append(RecipeStep(len(self.steps) + 1, instruction))
def display_recipe(self):
"""Displays the recipe in a structured format."""
print(f"\n{self.recipe_name}\n{'=' * len(self.recipe_name)}")
print("\nIngredients:")
for ingredient in self.ingredients:
print(f"- {ingredient}")
print("\nSteps:")
for step in self.steps:
print(step)
def customize_recipe(self, servings: int):
"""Customizes the recipe based on the number of servings."""
print(f"\nCustomizing recipe for {servings} servings...")
for ingredient in self.ingredients:
ingredient.quantity = round(ingredient.quantity * servings / 2, 2) # Base recipe is for 2 servings
@staticmethod
def random_tip() -> str:
"""Provides a random cooking tip."""
tips = [
"Use fresh basil for better aroma.",
"Add a pinch of sugar to balance the acidity of the tomatoes.",
"Reserve some pasta water to adjust sauce consistency.",
"Toast the garlic lightly for a deeper flavor.",
]
return random.choice(tips)
def main():
recipe = TomatoPastaRecipe()
# Adding ingredients
recipe.add_ingredient("Spaghetti", 200, "g")
recipe.add_ingredient("Tomatoes", 4, "medium-sized")
recipe.add_ingredient("Olive oil", 2, "tbsp")
recipe.add_ingredient("Garlic", 3, "cloves")
recipe.add_ingredient("Basil", 10, "leaves")
recipe.add_ingredient("Salt", 1, "tsp")
recipe.add_ingredient("Black pepper", 0.5, "tsp")
# Adding steps
recipe.add_step("Boil water in a large pot and add a pinch of salt.")
recipe.add_step("Cook the spaghetti until al dente, then drain and set aside.")
recipe.add_step("Heat olive oil in a pan over medium heat.")
recipe.add_step("Add minced garlic and sauté until fragrant.")
recipe.add_step("Chop tomatoes and add them to the pan. Cook until soft.")
recipe.add_step("Season with salt and black pepper.")
recipe.add_step("Toss the cooked spaghetti in the pan with the tomato sauce.")
recipe.add_step("Garnish with fresh basil leaves before serving.")
# Displaying the original recipe
recipe.display_recipe()
# Customizing recipe for 4 servings
recipe.customize_recipe(servings=4)
recipe.display_recipe()
# Displaying a random cooking tip
print(f"\nCooking Tip: {recipe.random_tip()}")
if __name__ == "__main__":
main()