是否可以将添加、删除和列表功能移动到单独的方法中

时间:2021-02-01 13:32:53

标签: c# console-application

我是一名兼职编码员,都没有 CS 学位并且自己学习。我写了这段代码,我的问题是,是否可以将添加、删除甚至列出功能移动到单独的方法中,并在 if 语句或 main 方法中调用它们?

我的第二个问题是 itemAdded == "null" 是什么意思?让我们假设如果我添加了 null 它说你什么都没添加?

 public void MyMethod()
    {
        var itemList = new List<string>();
        int choices;

        while (true)
        {
            //  Console.Clear();

            try
            {
                Console.WriteLine("Shopping list:\n1. Add to list\n2. Remove from list\n3. List the items on the shopping list\n4. Exit");
                choices = Convert.ToInt32(Console.ReadLine());


                if (choices == 1)
                {
                    Console.WriteLine("\nWhat would you like to add in the list?");
                    string itemAdded = Console.ReadLine();

                    if (itemAdded == "null" || itemAdded == "")
                    {
                        Console.WriteLine("you add nothing!!!");
                    }
                    else
                    {
                        itemList.Add(itemAdded);
                        Console.WriteLine(itemAdded + " is added to the list");
                    }

                }

                else if (choices == 2)
                {
                    Console.WriteLine("\nWhat would you like to remove from the list?");
                    string itemToRemove = Console.ReadLine();

                    if (itemToRemove == "null" || itemToRemove == "")
                    {
                        Console.WriteLine("you removed nothing");
                    }
                    else
                    {
                        bool isMatch = false;
                        foreach (string item in itemList)
                        {
                            if (item == itemToRemove)
                            {
                                isMatch = true;
                            }
                        }
                        if (isMatch)//if ismatch is true
                        {
                            itemList.Remove(itemToRemove);
                            Console.WriteLine(itemToRemove + " is successfully removed from your list");
                        }
                        else
                        {
                            Console.WriteLine("Item is not in the list");
                        }
                    }
                    //else
                    //{
                    //    itemList.Remove(itemToRemove);
                    //    Console.WriteLine(itemToRemove + " is successfully removed from your list");
                    //}
                }

                else if (choices == 3)
                {
                    foreach (string item in itemList)
                    {
                        Console.WriteLine(item);
                    }
                }

                else if (choices == 4)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("please enter valid choice!!!");
                }

            }
            catch (Exception)
            {

                Console.WriteLine("You have Entered non-numeric value");
            }
           
        }


    }

在main方法中调用这个列表

 private static void Main()
    {
        MyClass newClass = new MyClass();

        newClass.MyMethod();

        Console.ReadKey();

    }

1 个答案:

答案 0 :(得分:1)

itemAdded == "null" 正在检查用户是否实际输入了 null。如果值为 null,则不会。

你真的应该使用内置的字符串函数来检查一个字符串是否为空或空

if (String.IsNullOrEmpty(itemAdded))

https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=net-5.0

C# 是一种面向对象的语言。这意味着您构建应用程序的方式是围绕每个模块/类都有自己的功能的想法构建的。如果您尝试扩展此应用程序,您可以执行以下操作:

  1. 构建一个名为“ShoppingList”的新类,它有几个功能。 (添加、删除、阅读列表)。

  2. 您的主类将读取控制台输入并执行您当前正在执行的操作,但需要与 ShoppingList 类进行通信

  3. 与其将购物清单设为 List 对象,不如将其设为 Dictionary。键是列表中的项目(例如牛奶),值是数量(例 2)

示例类布局:

public class ShoppingList{
    Dictionary<string, int> _ShoppingList = new Dictionary<string, int>();

    public void AddItem(string item){
        if(_ShoppingList.ContainsKey(item)){
           _ShoppingList[item]++;
        }else{
           _ShoppingList.Add(item, 1);
        }
    }

    public void RemoveItem(string item){
        if(_ShoppingList.ContainsKey(item)){
          _ShoppingList[item]--;
          if(_ShoppingList[item] == 0) _ShoppingList.Remove(item);
        }else{
          //nothing to remove
        }
    }
}