如何使用 DRY 重写菜单

How to re-write menu using DRY

我正在做一个学校项目,我真的觉得我可以在我的代码中完成我想要的,但是我觉得我 - 总是 - 重复我的代码。

我曾尝试将 switch 案例放入方法中,但没有取得任何成功,尽管超出了范围。

我是初学者,请原谅我的代码!

但是,如果有人对使用方法或其他东西有任何提示,那么我可以更好地清理我的代码。

这是我的一些代码,这是我菜单的开始分支。

我已经将代码大致翻译成英文了。

所以这是主方法中的子菜单。

            string[] flowers = { "Sunflower", "Coleus Tree", "Tomato" };
            int[] plant = { 0, 0, 0 };


                      switch (menuInput03)
                            {
                                /////WATER PLANT////
                                case 1:
                                    Console.Clear();
                                    Console.WriteLine("Press SPACEBAR several times to water plant!");

                                    if (plant[0] < 21)
                                    {
                                        for (int i = plant[0]; i < 21; i++)
                                        {
                                            var keyPressed = Console.ReadKey();
                                            if (keyPressed.Key == ConsoleKey.Spacebar)
                                            {
                                                plant[0] = i;
                                                Console.Clear();
                                                Console.WriteLine("Press SPACEBAR several times to water plant!");
                                                Console.WriteLine("\nWatering plant..");
                                                Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]");
                                            }
                                     
                                        }
                                        while (true)
                                        {
                                            Console.Clear();
                                            Console.WriteLine("Your plant has been watered!");
                                            Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]\n");
                                            Console.WriteLine("To continue press ENTER.");
                                            var keyPressed01 = Console.ReadKey();
                                            if (keyPressed01.Key == ConsoleKey.Enter)
                                            {
                                                Console.Clear();
                                                break;
                                            }
                                            else
                                            {
                                                        Console.Clear();
                                                    }
                                        }
                                    }
                                            else
                                            {
                                                break;
                                            }
                                            Console.Clear();
                                            break;
                                case 2:
                                            Console.Clear();
                                            Console.WriteLine("Press SPACEBAR several times to remove weed!");

                                            if (plant[0] > 19)
                                            {
                                                for (int i = plant[0]; i < 51; i++)
                                                {
                                                    var keyPressed = Console.ReadKey();
                                                    if (keyPressed.Key == ConsoleKey.Spacebar)
                                                    {
                                                        plant[0] = i;
                                                        i++;
                                                        Console.Clear();
                                                        Console.WriteLine("Press SPACEBAR several times to remove weed!");
                                                        Console.WriteLine("\nRipping out weeds..");
                                                        Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]");
                                                    }

                                                }
                                                while (true)
                                                {
                                                    Console.Clear();
                                                    Console.WriteLine("Your plant has gotten it's weed removed!");
                                                    Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]\n");
                                                    Console.WriteLine("To continue press ENTER.");
                                                    var keyPressed01 = Console.ReadKey();
                                                    if (keyPressed01.Key == ConsoleKey.Enter)
                                                    {
                                                        Console.Clear();
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        Console.Clear();
                                                    }
                                                }
                                            }
                                            Console.Clear();
                                            break;`

我想到了以下;不要只是 copy-paste 在不了解发生了什么的情况下进行学校项目。如果有些事情不清楚,请随时提出 follow-up 问题。

public record Action
{
    public string Title { get; }
    public int RequiredHealth { get; }
    public string ActionMessage { get; }
    public string CompletedMessage { get; }

    private Action(string title, int requiredHealth, string actionMessage, string completedMessage)
    {
        Title = title;
        RequiredHealth = requiredHealth;
        ActionMessage = actionMessage;
        CompletedMessage = completedMessage;
    }

    public static Action WaterPlant { get; } = new Action(
        title: "Press SPACEBAR several times to water plant!",
        requiredHealth: 20,
        actionMessage: "Watering plant..",
        completedMessage: "Your plant has been watered!");

    public static Action RemoveWeed { get; } = new Action(
        title: "Press SPACEBAR several times to remove weed!",
        requiredHealth: 50,
        actionMessage: "Ripping out weeds..",
        completedMessage: "Your plant has gotten it's weed removed!");
}

static void Main()
{
    string[] flowers = { "Sunflower", "Coleus Tree", "Tomato" };
    int[] plant = { 0, 0, 0 };

    for (var menuInput03 = 1; menuInput03 <= 2; menuInput03++)
    {
        var action = menuInput03 switch
        {
            1 => Action.WaterPlant,
            2 => Action.RemoveWeed,
        };

        Console.Clear();
        Console.WriteLine(action.Title);

        while (plant[0] < action.RequiredHealth)
        {
            if (Console.ReadKey().Key != ConsoleKey.Spacebar)
            {
                Console.Write("\b \b");
                continue;
            }

            plant[0]++;
            Console.Clear();
            Console.WriteLine(action.Title);
            Console.WriteLine(action.ActionMessage);
            Console.WriteLine($"[{flowers[0]} {plant[0]}/100]");
        }

        Console.Clear();
        Console.WriteLine(action.CompletedMessage);
        Console.WriteLine($"[{flowers[0]} {plant[0]}/100]");
        Console.WriteLine("To continue press ENTER.");

        while (Console.ReadKey().Key != ConsoleKey.Enter)
        {
            Console.Write("\b \b");
        }
        
        Console.Clear();
    }
}