Search code examples
c#methodsdynamicmultiple-choice

How do i make a dynamic multiple choice method in C#?


I have a multiple choice method, but sometimes i need it to be only 2 options, but sometimes 3, 4 or even 5. I want to make a dynamic method that takes in an int of how many options there should be, and that is how many options the user can choose from. So if i call the function with the int being 2, there will only be 2 choices, but i call it with 4, then there will be 4.

Here is the code of what i'm trying to achieve so you can see.

string OptionChoice(int howManyOptions)
{
    bool optionChosenIsValid = false;
    char optionChosen;

    // If howManyOptions is 2, it should be like this ↓

    do
    {
        Console.Write("\n\nMelyik opciót választod? (Nyomd meg az 'A' vagy 'B' billentyűt!) ");
        optionChosen = Console.ReadKey().KeyChar;
        switch (optionChosen)
        {
            case 'a':
            case 'A':
                optionChosenIsValid = true;
                break;

            case 'b':
            case 'B':
                optionChosenIsValid = true;
                break;

            default:
                Console.Write("\nIlyen opció nincsen, kérlek próbáld újra!");
                break;
        }
    } while (!optionChosenIsValid);

    Console.WriteLine($"\n({optionChosen.ToString().ToUpper()}) lehetőség kiválasztva");


    //But if howManyOptions is for example 4, it should be like this ↓

    do
    {
        Console.Write("\n\nMelyik opciót választod? (Nyomd meg az 'A', 'B', 'C' vagy 'D' billentyűt!) ");
        optionChosen = Console.ReadKey().KeyChar;
        switch (optionChosen)
        {
            case 'a':
            case 'A':
                optionChosenIsValid = true;
                break;

            case 'b':
            case 'B':
                optionChosenIsValid = true;
                break;

            case 'c':
            case 'C':
                optionChosenIsValid = true;
                break;

            case 'd':
            case 'D':
                optionChosenIsValid = true;
                break;

            default:
                Console.Write("\nIlyen opció nincsen, kérlek próbáld újra!");
                break;
        }
    } while (!optionChosenIsValid);

    Console.WriteLine($"\n({optionChosen.ToString().ToUpper()}) lehetőség kiválasztva");

    return optionChosen.ToString().ToUpper();
}

I know i could just put the howManyOptions int in a switch case and whatever number it is, i'd have that many choices in it, but i feel like that is very ugly, and there must be a better solution. Thank you in advance!


Solution

  • Well At first you could try this

    1. Generate the console logs dynamically based on the number of "howManyOptions" you pass to the function.
    2. Use the dynamic loop again to check if the chosen option is within the acceptable range or not.
    string OptionChoice(int howManyOptions)
    {
        bool optionChosenIsValid = false;
        char optionChosen;
    
    
        do
    {
        Console.Write($"\n\nMelyik opciót választod? (Nyomd meg az ");
        
        // Construct your options list with this loop
        for (int i = 0; i < howManyOptions; i++)
        {
            char optionKey = (char)('A' + i);
            Console.Write($"'{optionKey}'{(i < howManyOptions - 1 ? ", " : "")}");
        }
    
        Console.Write(" billentyűt!) ");
        
        optionChosen = Console.ReadKey().KeyChar;
        
        // similarly check if the chosen operation is within the option range you expect it to be
        for (int i = 0; i < howManyOptions; i++)
        {
            char optionKey = (char)('A' + i);
            if (char.ToUpper(optionChosen) == optionKey)
            {
                optionChosenIsValid = true;
                break;
            }
        }
    
        if (!optionChosenIsValid)
        {
            Console.Write("\nIlyen opció nincsen, kérlek próbáld újra!");
        }
    
    } while (!optionChosenIsValid);
    
    Console.WriteLine($"\n({optionChosen.ToString().ToUpper()}) lehetőség kiválasztva");
        return optionChosen.ToString().ToUpper();
    }