Simula’s Soups
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.
- 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.
- Upgrade to enumerations. Add your enumeration types and change the tuple variable to use these instead of strings.
- Try out tuple element names.
- Maybe make multiple soups with different types to get a feel for how you’d assign different soup values to the tuple variable.
- 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.
- Now do it repeatedly to populate the array.
The following is one possible solution to this challenge.
using System;
(SoupType type, MainIngredient ingredient, Seasoning seasoning)[] soups = new (SoupType, MainIngredient, Seasoning)[3];
for(int index = 0; index < soups.Length; index++)
soups[index] = GetSoup();
foreach (var soup in soups)
Console.WriteLine($"{soup.seasoning} {soup.ingredient} {soup.type}");
(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 }