久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 C# 中實現具有子菜單的控制臺菜單

How to implement a console menu having submenus in C#(如何在 C# 中實現具有子菜單的控制臺菜單)
本文介紹了如何在 C# 中實現具有子菜單的控制臺菜單的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

(C#) 我正在開發一個類似于 RedBox 的程序,它將具有客戶和經理功能.我只是想在開始實現更多功能之前讓所有菜單正常工作,但我遇到了一個我似乎無法理解的錯誤.我的問題似乎出在 CustomerMenu() 方法和 MainMenu() 方法中.我已經添加了我擁有的所有內容,以便您可以獲得完整的圖片.感謝任何幫助,因為我仍然有點新,所以感謝任何提示,謝謝.

(C#) I am working on a program that is similar to RedBox which will have customer and manager functions. I am just trying to get all the menu's working correctly before I begin implementing more functions, but I am encountering an error that I can not seem to understand. My issue seems to be in the CustomerMenu() method and in the MainMenu() method. I have added all that I have so you can get the full picture. Any help is appreciated as I am still somewhat new so any tips are appreciated, thank you.

        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!
"); // The customer menu is brought up if user enters 1
                CustomerMenu();
            }
            if (userChoice == 2)
            {
                Console.WriteLine("Welcome to the manager menu!
"); // 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!  
At VideoMart you are able to rent a variety of movies from many genres such as action, family, horror, etc!");
        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.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.
"); // 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.
"); // 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.
"); // 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.
"); // 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!  
Below is a list of actions that can be performed by customers!");
        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 exit.");
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------");

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

        while (!(customerChoice2 == "1" || customerChoice2 == "2" || customerChoice2 == "3" || customerChoice2 == "4") || customerChoice2 == "5") // error checking
        {
        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 exit.");

            customerChoice2 = Console.ReadLine();
        }

        return int.Parse(customerChoice2);
    }
}

推薦答案

使用平面菜單系統

您可以嘗試更正并稍微重構一下.

You can try this corrected and a little refactored.

我們創建了一個獲取用戶選擇的方法,因此不再需要重復代碼.我們使用 uint 是因為選擇是肯定的,并且使用 TryParse 來轉換輸入的字符串.出錯時返回 0,所以這里沒問題.

We created a method to get the user choice, so repeat code is no more needed. We use uint because choice is positive and TryParse to convert the inputed string. It returns 0 in case of error, so that's fine here.

我們還使用 lamda 來打印選擇字符串以不重復它們.

Also we use a lamda to print the choices strings to not repeat them.

接下來我們使用 switch 來管理選擇,以便代碼更干凈和可維護.

Next we use a switch to manage choice so the code is more clean and maintainable.

清除菜單之間的控制臺,我們提供根菜單和子菜單之間的導航.

The console is cleared between menus and we offer navigation between root and sub menus.

未來的改進是使用運行這些表的自動菜單管理器創建菜單標題、選項和相關方法的表格.只是稍微復雜一點,但不要太多.這可以通過創建一些集合和一個 MenuManager 類來完成.有了這樣的東西,你將擁有一個健壯的系統,代碼很少,沒有重復.

A future improvement is to create tables of menus headers, choices and associated methods... with a auto-menu manager that runs these tables. Just a little more complex but not too much. It can be done by creating some collections and a MenuManager class. With such thing, you will have a robust system with very few code and nothing repeated.

static void Test()
{
  MainMenu();
}

static uint GetUserChoice(Action printMenu, int choiceMax)
{
  uint choice = 0;
  Action getInput = () =>
  {
    uint.TryParse(Console.ReadLine(), out choice);
  };
  getInput();
  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();
  }
}

使用自動菜單管理器

這是菜單選項類:

public class MenuChoice
{
  public string Title { get; private set; }
  public Action Action { get; private set; }
  public MenuChoice(string title, Action action)
  {
    Title = title;
    Action = action;
  }
}

這是菜單類:

public class Menu
{

  private readonly string Separator = new string('-', 100);
  private string Header;
  private List<MenuChoice> Choices;
  private Menu Root;

  public Menu(string header, List<MenuChoice> choices, Menu root)
  {
    Header = header;
    Choices = choices;
    Root = root;
  }

  private void Print()
  {
    for ( int index = 0; index < Choices.Count; index++ )
      Console.WriteLine($"Press {index + 1} {Choices[index].Title}");
      Console.WriteLine($"Press {Choices.Count + 1} to " + 
                        $"{( Root == null ? "exit" : "go to previous menu" )}");
  }

  public void Run()
  {
    Console.Clear();
    Console.WriteLine(Separator);
    Console.WriteLine(Header);
    Console.WriteLine();
    Print();
    Console.WriteLine(Separator);
    uint choice = GetUserChoice();
    if ( choice == Choices.Count + 1 )
      if ( Root == null )
      {
        Console.WriteLine("Thank you for visiting VideoMart at University Boulevard, Goodbye!");
        return;
      }
      else
        Root.Run();
    else
    {
      var action = Choices[(int)choice - 1].Action;
      if ( action != null )
        action();
      else
      {
        Console.WriteLine("Not implemented yet, press a key to continue.");
        Console.ReadKey();
        Run();
      }
    }
  }

  uint GetUserChoice()
  {
    uint choice = 0;
    Action getInput = () =>
    {
      uint.TryParse(Console.ReadLine(), out choice);
    };
    getInput();
    while ( choice < 1 || choice > Choices.Count + 1 )
    {
      Console.WriteLine();
      Console.WriteLine("Please try again");
      Print();
      getInput();
    }
    return choice;
  }

}

這是菜單管理器類:

public class MenuManager
{
  private Menu Root;

  public MenuManager(Menu root)
  {
    Root = root;
  }

  public void Run()
  {
    Root.Run();
  }
}

這里是菜單管理器的初始化,在選擇中使用方法或另一個菜單而不是空值:

Here the menu manager initialization, using methods or another menu instead of null in choices:

var choicesMain = new List<MenuChoice>();
var choicesCustomer = new List<MenuChoice>();
var choicesManager = new List<MenuChoice>();

string headerMain = "Welcome to VideoMart at University Boulevard!" + Environment.NewLine +
                    "At VideoMart you are able to rent a variety of movies from many genres such as action, family, horror, etc!";
string headerCustomer = "Below is a list of actions that can be performed by customers!";
string headerManager = "Below is a list of actions that can be performed by managers!";

var root = new Menu(headerMain, choicesMain, null);
var menuCustomer = new Menu(headerCustomer, choicesCustomer, root);
var menuManager = new Menu(headerManager, choicesManager, root);

choicesMain.Add(new MenuChoice("if you are a customer", menuCustomer.Run));
choicesMain.Add(new MenuChoice("if you are a manager", menuManager.Run));

choicesCustomer.Add(new MenuChoice("to view movies available to rent.", null));
choicesCustomer.Add(new MenuChoice("to rent a movie.", null));
choicesCustomer.Add(new MenuChoice("to view a list of movies you currently have rented.", null));
choicesCustomer.Add(new MenuChoice("to return a movie rented.", null));

現在要做的是:

new MenuManager(root).Run();

這篇關于如何在 C# 中實現具有子菜單的控制臺菜單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加圖標?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
Building a database driven menu with ASP.NET, JQuery and Suckerfish(使用 ASP.NET、JQuery 和 Suckerfish 構建數據庫驅動的菜單)
主站蜘蛛池模板: 一区二区成人 | 欧美一区二区三区四区视频 | 亚洲久久一区 | 国产99久久精品一区二区永久免费 | 国产综合精品一区二区三区 | 久久99国产精品 | 丁香久久 | 日本久久网 | 国产成都精品91一区二区三 | a级在线免费观看 | 国产黄色av网站 | 欧美一级欧美三级在线观看 | 欧美不卡在线 | 国产一级在线 | 久久久久久国产一区二区三区 | 亚洲综合色网 | 国产在线精品一区二区三区 | 国产高清视频一区 | 国产精品网址 | 亚洲三区视频 | 国产精品亚洲精品久久 | 精品一二三区在线观看 | 午夜精品久久久久久 | 久久久亚洲成人 | 日韩伦理电影免费在线观看 | 国产综合一区二区 | 粉色午夜视频 | 91pron在线 | 人妖无码| 欧美日韩在线免费观看 | 欧美色性 | 国产亚洲精品久久情网 | 免费久久精品视频 | 欧美在线一二三 | 日韩精品国产精品 | 国产欧美一区二区三区日本久久久 | 久草在线在线精品观看 | 亚洲不卡在线观看 | 欧美一级在线免费观看 | 一区网站 | 欧美自拍视频 |