简体   繁体   中英

How To Add/Remove Items in c# Console Application Menu

I'm wondering if I could use a few simple dictionaries to store the data behind this or if I need something more... Need a system to be able to add and remove items and have that translated to other menus (methods). Sorry if this is worded poorly

    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;
    }

You can use a generic List and switch the value entered by the user to be the INDEX Value of that list of products

a quick Example :

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;
 }

Or you can use that check in the first for example :

 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;
     }

  } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM