如何在C#中实现具有子菜单的控制台菜单

问题描述 投票:1回答:1

(C#)我正在开发一个类似于RedBox的程序,该程序将具有客户和经理功能。在开始实现更多功能之前,我只是试图使所有菜单正常工作,但是遇到一个我似乎无法理解的错误。我的问题似乎在CustomerMenu()方法和MainMenu()方法中。我已经添加了所有内容,因此您可以获得完整图片。感谢您提供任何帮助,因为我仍然有些陌生,因此感谢您提供任何提示,谢谢。

        MainMenu();
        Console.ReadKey();

    }
    static void MainMenu()
    {
        int userChoice = MainMenuChoice(); // Reading in the userChoice with the MenuChoice method

        if (userChoice == 3) // if user enters 3, the program ends
        {
            Console.WriteLine("Thank you, Goodbye!");
        }

        while (userChoice != 3)
        {
            if (userChoice == 1)
            {
                Console.WriteLine("Welcome to the customer menu!\n"); // The customer menu is brought up if user enters 1
                CustomerMenu();
            }
            if (userChoice == 2)
            {
                Console.WriteLine("Welcome to the manager menu!\n"); // The manager menu is brought up if user enters 2
                //ManagerMenu();
            }
            userChoice = MainMenuChoice(); // program ends
            if (userChoice == 3)
            {
                Console.WriteLine("Thank you for visiting VideoMart at University Boulevard, Goodbye!");

            }
        }

    }
    static int MainMenuChoice()
    {
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------"); // introducing the user with the menu 
        Console.WriteLine("Welcome to VideoMart at University Boulevard!  \nAt VideoMart you are able to rent a variety of movies from many genres such as action, family, horror, etc!");
        Console.WriteLine("\nPress 1 if you are a customer");
        Console.WriteLine("\nPress 2 if you are a manager");
        Console.WriteLine("\nPress 3 to Exit");
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------");

        string choice = Console.ReadLine();
        Console.WriteLine();

        while (!(choice == "1" || choice == "2" || choice == "3")) // error checking
        {
            Console.WriteLine("Please try again");
            Console.WriteLine("Press 1 if you are a customer");
            Console.WriteLine("Press 2 if you are a manager");
            Console.WriteLine("Press 3 to Exit");

            choice = Console.ReadLine();
        }

        return int.Parse(choice);
    }

    }


    static void CustomerMenu() {

        int customerChoice = CustomerMenuChoice(); // Reading in the customerChoice into the CustomerMenuChoice method

        if (customerChoice == 5) // if user enters 5, the program ends
        {
            Console.WriteLine("Thank you for using VideoMart!");
        }

        while (customerChoice != 5)
        {
            if (customerChoice == 1)
            {
                Console.WriteLine("Press 1 to view movies available to rent.\n"); // this option gives the user the opportunity to view all movies available to rent
                 //MoviesAvailable();
            }
            if (customerChoice == 2)
            {
                Console.WriteLine("Press 2 to rent a movie.\n"); // this option gives the user the opportunity to rent a movie, with email address
                //RentMovie();
            }
            if (customerChoice == 3)
            {
                Console.WriteLine("Press 3 to view a list of movies you currently have rented.\n"); // this option gives the user the opportunity to view movies a user currently has rented, with email address
                //RentMovie();
            }
            if (customerChoice == 4)
            {
                Console.WriteLine("Press 4 to return a movie rented.\n"); // this option gives the user the opportunity to return a movie rented
                //RentMovie();
            }
            customerChoice = CustomerMenuChoice();
            if (customerChoice == 5)
            {
                Console.WriteLine("Thank you for visiting VideoMart at University Boulevard, Goodbye!");

            }
        }            
}
    static int CustomerMenuChoice()
    {
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------"); // introducing the user with the menu 
        Console.WriteLine("Welcome to VideoMart at University Boulevard!  \nBelow is a list of actions that can be performed by customers!");
        Console.WriteLine("\nPress 1 to view movies available to rent.");
        Console.WriteLine("\nPress 2 to rent a movie.");
        Console.WriteLine("\nPress 3 to view a list of movies you currently have rented.");
        Console.WriteLine("\nPress 4 to return a movie rented.");
        Console.WriteLine("\nPress 5 to exit.");
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------");

        string customerChoice2 = Console.ReadLine();
        Console.WriteLine();

        while (!(customerChoice2 == "1" || customerChoice2 == "2" || customerChoice2 == "3" || customerChoice2 == "4") || customerChoice2 == "5") // error checking
        {
        Console.WriteLine("\nPress 1 to view movies available to rent.");
        Console.WriteLine("\nPress 2 to rent a movie.");
        Console.WriteLine("\nPress 3 to view a list of movies you currently have rented.");
        Console.WriteLine("\nPress 4 to return a movie rented.");
        Console.WriteLine("\nPress 5 to exit.");

            customerChoice2 = Console.ReadLine();
        }

        return int.Parse(customerChoice2);
    }
}
c# menu console submenu
1个回答
0
投票

您可以尝试对此进行更正和重构。

我们创建了一种获取用户选择的方法,因此不再需要重复代码。我们使用uint是因为choice是肯定的,而TryParse可以转换输入的字符串。如果发生错误,它将返回0,所以在这里很好。

此外,我们也使用lamda来打印选择字符串以免重复。

接下来,我们使用开关来管理选择,以便代码更清晰和可维护。

在菜单之间清除了控制台,我们提供了根菜单和子菜单之间的导航。

未来的改进是通过运行这些表的自动菜单管理器来创建菜单标题,选项和相关方法的表。只是稍微复杂一点而不会太多。可以通过创建一些集合和MenuManager类来完成。有了这样的东西,您将拥有一个健壮的系统,该系统具有很少的代码,并且不再重复。让我知道您是否有兴趣。

static void Test()
{
  MainMenu();
}
static uint GetUserChoice(Action printMenu, int choiceMax)
{
  uint choice = 0;
  Action getInput = () =>
  {
    uint.TryParse(Console.ReadLine(), out choice);
  };
  getInput();
  Console.WriteLine();
  while ( choice < 1 || choice > choiceMax )
  {
    Console.WriteLine();
    Console.WriteLine("Please try again");
    printMenu();
    getInput();
  }
  return choice;
}
static void MainMenu()
{
  Action printMenu = () =>
  {
    Console.WriteLine("Press 1 if you are a customer");
    Console.WriteLine("Press 2 if you are a manager");
    Console.WriteLine("Press 3 to Exit");
  };
  Console.Clear();
  Console.WriteLine("-----------------------------------------------------------------------------------------------------------"); // introducing the user with the menu 
  Console.WriteLine("Welcome to VideoMart at University Boulevard!");
  Console.WriteLine("At VideoMart you are able to rent a variety of movies from many genres such as action, family, horror, etc!");
  Console.WriteLine();
  printMenu();
  Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
  uint choice = GetUserChoice(printMenu, 3);
  switch ( choice )
  {
    case 1:
      CustomerMenu();
      break;
    case 2:
      //ManagerMenu();
      break;
    case 3:
      Console.WriteLine("Thank you for visiting VideoMart at University Boulevard, Goodbye!");
      break;
    default:
      throw new NotImplementedException();
  }
}
static void CustomerMenu()
{
  Action printMenu = () =>
  {
    Console.WriteLine("Press 1 to view movies available to rent.");
    Console.WriteLine("Press 2 to rent a movie.");
    Console.WriteLine("Press 3 to view a list of movies you currently have rented.");
    Console.WriteLine("Press 4 to return a movie rented.");
    Console.WriteLine("Press 5 to return to main menu.");
  };
  Console.Clear();
  Console.WriteLine("-----------------------------------------------------------------------------------------------------------"); // introducing the user with the menu 
  Console.WriteLine("Below is a list of actions that can be performed by customers!");
  Console.WriteLine();
  printMenu();
  Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
  Console.WriteLine();
  uint choice = GetUserChoice(printMenu, 5);
  switch ( choice )
  {
    case 1:
      //MoviesAvailable();
      break;
    case 2:
      //RentMovie();
      break;
    case 3:
      //RentedMovies();
      break;
    case 4:
      //ReturnMovie();
      break;
    case 5:
      MainMenu();
      break;
    default:
      throw new NotImplementedException();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.