[英]How To Add/Remove Items in c# Console Application Menu
我想知道我是否可以使用一些簡單的字典來存儲背后的數據,還是我需要更多...需要一個能夠添加和刪除項目並將其轉換為其他菜單(方法)的系統。 對不起,如果措辭不好
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;
}
您可以使用通用列表並將用戶輸入的值切換為該產品列表的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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.