Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 delete and update a contact in the list but I want have the user to be able to pick a contact and be able to delete the contact. However I am unsure how to do this.

Any guidance would be appreciated.

C#
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':
                    removeContact(contactList);
                    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 remove a contact
         public static void removeContact(List<Contact> contactList) 
         {

             for (int i = 0; i < contactList.Count; i++)
               if (i % 5 == 0)
             contactList.RemoveAt(i);
                          
        }
Posted
Comments
[no name] 28-Jul-15 20:07pm    
How to do what, exactly? "How to do this" means nothing to us. You know how to remove a contact from your list. You know how to get user input. So what is the problem?

1 solution

You need to offer the user a way to pick a contact. The biggest problem is - with a long list - the contract may have scrolled off the screen.

You may want to limit the display to N rows and implement PgUp / PgDn key functionality. You will have to keep track of an index offset into your contact list.

To select a specific row, the easy thing is to use the Up/Down arrow keys to highlight a specific row. Highlighting a row can be done by changing the text background or placing the text in brackets [like this]. The style is at your discretion. You will also have to keep track of the index of the highlighted row.

Modify your DisplayList function to accept two additional parameters - offset and number of rows. Implement the PgUp/PgDn and Up/Dn arrow keys - plus a delete menu, and you're done.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900