Allowing the user to add data to the list

Hey. I am trying to create a contact manager program using a list to store and display data. I need to view a report that shows a summary of the available contacts and then there is a menu that allows 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 that will contain their first and last name, phone number, email address and type. but I'm not sure how to do it.

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

      

+3


source to share


1 answer


prompt user for each field

 public static void createContact()
        {
            Contact c1 = new Contact();
            Console.WriteLine("GetFirstName");
            c1.GetFirstName = Console.ReadLine();
            Console.WriteLine("GetLastName");
            c1.GetLastName = Console.ReadLine();
            Console.WriteLine("GetEmailAddress");
            c1.GetEmailAddress = Console.ReadLine();
            Console.WriteLine("GetPhoneNumber");
            c1.GetPhoneNumber = Console.ReadLine();
            Console.WriteLine("ContactTypes");
            c1.ContactTypes = Console.ReadLine();

            //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
                // Save using this foreach loop to some collection where you are storing contacts or may be directly save the list.
            }

            Console.ReadLine();
        }

      



but I doubt how you can have all the contacts in this list, because every time he comes to this method it will create an emotional list.

0


source







All Articles