从用户选择的列表中更新项目 [英] Update Item from list selected by user

查看:80
本文介绍了从用户选择的列表中更新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用列表来存储和显示数据,从而在控制台应用程序中构建联系人管理器程序。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个联系人和一个联系人对象。我还有一种更新联系人的方法,但我想让用户能够选择联系人的名字,姓氏,电子邮件地址,类型,电话号码,并能够使用他们输入的信息进行更新。但是,当从菜单中选择更新时,它只是跳过方法而不执行任何操作。我不知道如何解决这个问题。



任何改进计划的指导或方法都将受到赞赏。



I am trying to build a contact managers program in a console application 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 contact and a contact object. I also have a method to update a contact but I want to have the user to be able to pick a contact first name, last name, email address, type, phone number and be able to update it with the information they enter. However when select update from the menu it just skips over the method and does nothing. I am not sure how to fix this.

Any guidance or ways to improve the program would be appreciated.

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

            List<Contact> contactList = new List<Contact>();
                       
            //Main Driver
            char menuItem;
             Console.WriteLine("Contact List\n");
            menuItem = GetMenuItem();
            while (menuItem != 'Q')
            {

                ProcessMenuItem(menuItem, contactList);
                menuItem = GetMenuItem();

            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
            while (menuItem != 'C'
                && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(IOConsole.GetChar("\nEnter option or M for menu:"));
            }
            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, List<Contact> contactList)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    Console.WriteLine("enter the contact name To delete");
                    removeContact(contactList, Console.Read().ToString() );
                        break;
                case 'U':
                    updateContact(contactList);
                    break;
                case 'D':
                    LoadFromFile();
                    break;
                case 'S':
                    saveToFile();
                    break;
                    
                case 'L':
                    sortByLastName(contactList);
                    break;
                case 'F':
                    sortByFirstName(contactList);
                       break;
                case 'P':
                       DisplayList(contactList);
                       break;
                case 'T':
                       sortByContactType();
                       break;
                case 'Q':
                       
                       break;

            }                   
        }
// allows the user to update a contact
         public static void updateContact(List<Contact> contactList)
        {
            char answer, answer2, answer3, answer4,asnwer5;
            String newFirstName = " ";
            String newLastName = " ";
            String newPhoneNumber = " ";
            String newEmailAddress = " ";
            newContactType = Contact.ContactTypes;

            
            Console.WriteLine("\nUpdate Contact:{0}" , contactList); 
                          
            
              foreach (Contact obj in contactList)
             {

                    answer = IOConsole.GetChar("\nEdit FirstName?: Y/N: ");


                    if (answer.Equals('Y') || answer.Equals('y'))
                    {
                        newFirstName = IOConsole.Getstring("\nUpdate Contact: please enter the contact's new first name: ");
                        obj.GetFirstName = newFirstName;
                        break;
                    }

                    else if (answer.Equals('N') || answer.Equals('n'))
                    {

                    }
                
                   
                        answer2 = IOConsole.GetChar("\nEdit LastName?: Y/N: ");


                        if (answer2.Equals('Y') || answer.Equals('y'))
                        {
                            newLastName = IOConsole.Getstring("\nUpdate Contact: please enter the contact's new last name: ");
                            obj.GetFirstName = newLastName;
                            break;
                        }

                        else if (answer2.Equals('N') || answer.Equals('n'))
                        {



                        }
                    
                    
                        answer2 = IOConsole.GetChar("\nEdit EmailAddress?: Y/N: ");


                        if (answer2.Equals('Y') || answer.Equals('y'))
                        {
                            newEmailAddress = IOConsole.Getstring("\nUpdate Player: please enter the contact's new emailaddress) name: ");
                            obj.GetFirstName = newEmailAddress;
                            break;
                        }

                        else if (answer2.Equals('N') || answer.Equals('n'))
                        {



                        }
                    
                    
                        answer3 = IOConsole.GetChar("\nEdit phonenumber?: Y/N: ");


                        if (answer3.Equals('Y') || answer.Equals('y'))
                        {
                            newPhoneNumber = IOConsole.Getstring("\nUpdate Contact: please enter the contact's new phonenumber: ");
                            obj.GetFirstName = newPhoneNumber;
                            break;
                        }

                        else if (answer3.Equals('N') || answer.Equals('n'))
                        {



                        }
                    
                    
                        answer4 = IOConsole.GetChar("\nEdit ContactType?: Y/N: ");


                          if (answer4.Equals('Y') || answer.Equals('y'))
                        {
                            newContactType = IOConsole.Getstring("\nUpdate Player: please enter the contact's new type: ");
                            obj.GetFirstName = newContactType;
                            break;
                        }

                        else if (answer4.Equals('N') || answer.Equals('n'))
                        {



                        }
                                                   
               
            }
        
        }

推荐答案

从调试器开始:运行你的应用程序,然后输入你的数据。然后在行上放置一个断点

Start with the debugger: run your app, and enter your data. Then put a breakpoint on the line
ProcessMenuItem(menuItem, contactList);

在你的Main方法中。

输入你的U进行更新,它应该点到断点。

步入方法你会发现自己在这里:

in your Main method.
Enter your "U" to update and it should hit the breakpoint.
Step into the method and you should find yourself here:

switch (menuItem)

检查调试器中menuItem的值 - 它应该仍然是'U'但确保 - 并确保contactList中有项目。

单步执行:交换机应引导您更新联系。



然后,逐步完成该方法,确定在执行一行之前应该发生什么。那发生了吗?如果没有,为什么不呢?



如果发生任何事情,你没想;看看为什么 - 这可能与你的问题有关。



这是一项技能,称为调试,就像所有必须实践的技能一样,才能正确开发它。在这样的小任务中学习它是一个很好的开始方式!

Check the value of menuItem in the debugger - it should still be 'U' but make sure - and make sure there are items in your contactList.
Step through: the switch should lead you to updateContact.

Then, step through the method, working out exactly what should happen before tyou execute a line. Did that happen? If not, why not?

If anything happens that you didn;t expect, look at why - that may be related to your problem.

This is a skill, called debugging and like all skills it has to be practiced to develop it properly. And learning it while working in a small task like this is a good way to start!


这篇关于从用户选择的列表中更新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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