允许用户将数据添加到列表中 [英] Allowing the user to add data into a list

查看:76
本文介绍了允许用户将数据添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试使用列表构建联系人管理器程序来存储和显示数据。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个包含数据的列表,但我需要更改方法以允许用户创建一个新的联系人,其中包含他们的姓名,电话号码,电子邮件地址和类型。但我不知道该如何做到这一点。



任何指导都将不胜感激。



Hi I am trying to build a contact managers program using a list to store and display the data. I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. I have a method to create a list with data but I need to change the method to allow the user to create a new contact which will contain their first and last name, phone number, email address and type. but I am not sure as to how to do this.

Any guidance would be appreciated.

static void Main(string[] args)
        {           
            //Declare the list

            ArrayList list = new ArrayList();           

            Console.WriteLine("Contact List");
            // display the menu to the user
            Console.WriteLine("Enter option or M for menu:");
            //Main Driver
            char menuItem;
            menuItem = GetMenuItem();
            while (menuItem != 'X')
            {

                ProcessMenuItem(menuItem);
                menuItem = GetMenuItem();
               
            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = IOConsole.GetChar((Console.ReadLine()));

            while (menuItem != 'C'
                && menuItem != 'L' && menuItem != 'X' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                //menuItem = IOConsole.GetChar((Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
           Console.WriteLine("C-> Create Contacts");
           Console.WriteLine("R-> Remove Contacts");
           Console.WriteLine("U-> Update Contacts");
           Console.WriteLine("D -> Load data from file");
           Console.WriteLine("S-> Save data to file");
           Console.WriteLine("L-> View sorted by last name");
           Console.WriteLine("F-> View sorted by first name");
           Console.WriteLine("P-> View by partial name search");
           Console.WriteLine("T-> View by contact type");
           Console.WriteLine("Q-> Quit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    removeContact();
                    break;
                case 'U':
                    updateContact();
                    break;
                case 'D':
                    LoadToFile();
                    break;
                case 'S':
                    saveToFile();
                    break;
                    
                case 'L':
                    sortByLastName();
                    break;
                case 'F':
                    sortByFirstName();
                       break;
                case 'P':
                       
                       break;
                case 'T':
                       
                       break;
                case 'Q':
                       
                       break;

            }                   
        }

         public static void createContact()
        {
            Contact c1      = new Contact();
            //c1.Number       = 1; //Id?
            c1.GetLastName     = "Doe";
            c1.GetFirstName    = "John";
            c1.GetEmailAddress = "johndoe@email.com";
            c1.GetPhoneNumber  = "12345678";
            c1.ContactTypes         = ContactTypesEnum.Friend;

            //Create more contacts...

            //Add all contacts here
            ContactCollection contactList = new ContactCollection();
            contactList.Add(c1);

            //Loop through list
            foreach( Contact c in contactList)
            {
                Console.WriteLine(c.GetFirstName); //Do something with fields
            }

            Console.ReadLine();
        }



这是我的联系人类中使用的枚举


Here is the enumeration that is used in my contacts class

enum ContactTypesEnum { Family, Friend, Professional }

推荐答案

你好

通常,在需要更新时不应使用列表。这是因为你必须枚举以确定在添加元素之前是否存在哪个元素会花费更长时间和更长时间。

更有效的方法是使用Dictionary或HashSet,但是直到达到临界质量才会产生这种效果。

http://stackoverflow.com/questions/150750/hashset-vs-list-performance



我个人喜欢HashSet,但要高效工作包含类必须实现 https://msdn.microsoft.com/en-us/library/ms132151.aspx [ ^ ]



现在如果你的ID足以控制处理(比如批量等),那么字典会表现得更好,因为处理密钥与数据分开的相对速度,bu通常,就必须检查存在和检索而言,您会发现散列集的树结构优于 http://theburningmonk.com/2011/03/hashset-vs-list-vs-dictionary/ [ ^ ]列表,除非您只处理相对较小的数据列表。



添加和更新这些备选方案更容易,但是如果你必须对列表进行更新,那么使用扩展ToArray然后通过分配到正确的索引位置来更新。之后,您可以再次使用ToList扩展名。或者,列表支持删除,因此如果您的类支持复制构造函数,您可以使用该模式查找元素,将其从列表中删除,从该对象创建一个新元素,将其添加到列表中,甚至查找,删除,编辑,添加但是再次使用实际用户列表我不建议使用简单列表。

例如,有一个带有内部字典或smth的静态库类,它总是可以为你的应用程序提供服务,并且可以根据变化或每个请求处理异常的持久性,例如:当操作强度增加时,进出文件格式的替代方案就像地狱中的雪鞋一样;)
Hi
Generally you shouldn't use lists when you need to update. This is because you'll have to enumerate to see if an element exists before adding which will take longer and longer and longer.
A much more efficient approach is for instance to use a Dictionary or a HashSet, but of cause not until a critical mass is reached will this be a gain.
http://stackoverflow.com/questions/150750/hashset-vs-list-performance

I personally like the HashSet, but to work efficiently your containing class must implement https://msdn.microsoft.com/en-us/library/ms132151.aspx[^]

Now if your ID is enough to control handling (like say batches etc) the Dictionary will perform better because of the relative speed of handling keys seperate from data, but generally in terms of having to check for existance and retrieve, you'll find that the tree structure of a hashset outperforms http://theburningmonk.com/2011/03/hashset-vs-list-vs-dictionary/[^]lists unless you're only dealing with relatively small lists of data.

Adding and updating those alternatives is easier, but if you have to do updates on lists a good way towork with them is to use the extension ToArray and then update by assigning to the right index position. After that you can always use the ToList extension again. Alternatively a list supports remove, so if your class support a copy constructor you can use the pattern to find the element, remove it from list, make a new one from that object, add it to the list or even find, remove, edit, add but again with an actual users list i wouldn't recommend using a simple list.
Rather have for instance a static library class with an internal dictionary or smth which will always be ready to serve your application and will handle persisting asyncrously on change or per request e.g. as alternative io of going to and from file format is going to perform like snowshoes in hell when intensity of operations increase ;)


这篇关于允许用户将数据添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆