It's dangerous to code alone! Take this.

Simula’s Soup

This challenge ended up being more difficult than I had intended, and it seems like almost everyone struggles with it a little. (I plan to make some adjustments to it in future editions.)

Before you look at the solution below, here’s another suggestion: break it into small parts.

  1. Start by just making a tuple variable and hardcoding it to a specific soup. Forget user input. Forget tuple element names. Forget enumerations. Just make a variable and display it.
  2. Upgrade to enumerations. Add your enumeration types and change the tuple variable to use these instead of strings.
  3. Try out tuple element names.
  4. Maybe make multiple soups with different types to get a feel for how you’d assign different soup values to the tuple variable.
  5. Now you can add user input. This will take up a lot of lines of code to turn user input into enumerations and the soup as a whole. But it is stuff you’ve already done before. But if you’re struggling with this particular part, there is a blog post that talks about converting between tuples and strings that can help as well.

The following is one possible solution to this challenge.

(SoupType type, MainIngredient ingredient, Seasoning seasoning) soup = GetSoup();
Console.WriteLine($"{soup.seasoning} {soup.ingredient} {soup.type}");

// Or this version, which skips the custom names:
//(SoupType, MainIngredient, Seasoning) soup = GetSoup();
//Console.WriteLine($"{soup.Item3} {soup.Item2} {soup.Item1}");

(SoupType, MainIngredient, Seasoning) GetSoup()
{
    SoupType type = GetSoupType();
    MainIngredient ingredient = GetMainIngredient();
    Seasoning seasoning = GetSeasoning();
    return (type, ingredient, seasoning);
}

SoupType GetSoupType()
{
    Console.Write("Soup type (soup, stew, gumbo): ");
    string input = Console.ReadLine();
    return input switch
    {
        "soup" => SoupType.Soup,
        "stew" => SoupType.Stew,
        "gumbo" => SoupType.Gumbo
    };
}

MainIngredient GetMainIngredient()
{
    Console.Write("Main ingredient (mushroom, chicken, carrot, potato): ");
    string input = Console.ReadLine();
    return input switch
    {
        "mushroom" => MainIngredient.Mushroom,
        "chicken" => MainIngredient.Chicken,
        "carrot" => MainIngredient.Carrot,
        "potato" => MainIngredient.Potato
    };
}

Seasoning GetSeasoning()
{
    Console.Write("Seasoning (spicy, salty, sweet): ");
    string input = Console.ReadLine();
    return input switch
    {
        "spicy" => Seasoning.Spicy,
        "salty" => Seasoning.Salty,
        "sweet" => Seasoning.Sweet,
    };
}

enum SoupType { Soup, Stew, Gumbo }
enum MainIngredient { Mushroom, Chicken, Carrot, Potato }
enum Seasoning { Spicy, Salty, Sweet }