如何在c#控制台应用程序菜单中添加/删除项目

本文关键字:添加 删除项目 菜单 应用程序 控制台 | 更新日期: 2023-09-27 18:09:50

我想知道我是否可以使用一些简单的字典来存储这背后的数据,或者如果我需要更多的东西…需要一个系统,能够添加和删除项目,并有翻译到其他菜单(方法)。对不起,如果这是措词不好

    public int AddProducts(int customerIDinput)
    {
        //If the order already has 5 products then display an error that no more products can be added
        //Prompts the user for a product ID
        //If the user selects an invalid product then display an error
        //If the user selects a product that is discontinued then display an error
        //Prompt the user for a quantity
        //If the user enters a value < 1 or > 100 then display an error
        //Add the product and quantity to the order
        //Display the updated order information
        //Return to the main menu
        int input;
        input = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Please eneter a product ID:");
        switch (customerIDinput)
        {
            case 1
            break;
            case 2
            break;
            case 3
            break;
            case 4
            break;
            case 5
            break;
        }
        return customerIDinput;
    }

如何在c#控制台应用程序菜单中添加/删除项目

您可以使用通用列表,并将用户输入的值切换为该产品列表的INDEX

一个快速示例:

List<String> Products = new List<string>();
int Value = Int.Parse(Console.ReadLine());
switch(Value)
 {
  case 1:
      if (Products.Item(1) == null)
         Console.WriteLine("Doesnt Exist!"); // This check will be in all  cases in the Default one of caus
    break;
 }

或者你可以在第一个语句中使用这个复选框例如:

 List<String> Products = new List<string>();
 int Value = Int.Parse(Console.ReadLine());
    if (Products.Items(Value) == null)
     {
       //Display Error
     }
    else
   {
    switch(Value)
     {
      case 1:
         //what u want here
        break;
     }
  }