Click here to Skip to main content
15,886,802 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I am trying to build a contact managers program using an object array to store 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 need to have the option to save and load files as CSV records to a file but I am unsure how to go about doing this.

Any guidance would be appreciated.

C#
static void Main(string[] args)
        {
            //The MAXPLAYERS constant is the physical table size
            const Int32 MAXCONTACTS = 23;

            //Declare the player tables

            Contact[] contacts = new Contact[MAXCONTACTS];

            //Keep track of the actual number of players (i.e. logical table size)
            Int32 contactCount = 0;
            Int32 number = 0;
            String lastName = " ";
            String phoneNumber = " "; 
            String emailAddress = " "; 

            String firstName = " "; ;

            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, firstName, lastName, phoneNumber, emailAddress, contacts, ref contactCount, MAXCONTACTS);
                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, String firstName, String lastName, String phoneNumber,
            String emailAddress, Contact[] contacts, ref Int32 contactCount, Int32 MAXCONTACTS)
        {
            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 saveToFile()
        {

        }
        public static void LoadToFile()
        {

        }
Posted
Comments
[no name] 26-Jul-15 23:41pm    
Unsure how to go about doing what? In your empty saveToFile method, write some code to save your contacts to a file. In your empty LoadToFile method, write some code to read your contacts from the file.

 
Share this answer
 
Since CSV is perfectly known and is simple text, try to build a csv file by hand to see what it should look like.

Then write the code to generate automatically the file.

Reading the csv file is more tricky.
you must read it line by line, then split the line to fields and store the fields in table.

You will learn a lot by trying yourself, and abuse of debugger to see what append during load ans save.
 
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