Search code examples
c#.net-4.0generic-collections

Phone Directory Example


I am trying to make a simple phone directory program in windows console application. I have used SortedDictionary in which Key is name and Value is number, List for address and StringDictionary for email-ids. I put all of these in a class named Directory and then called it through an instance in Main. I am facing problem in enumerating the directory. I want to print all four entries of a person in same line. Can anyone tell me how should I proceed. This is how I was trying.I am well sure of that there are many mistakes in my logic..Sorry for inconvenience:-

public class Directry    
{      
    List <string> Email_id= new List <string>();

    SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();
    StringCollection Adress=new StringCollection();
    public Directry(SortedDictionary<string, int> dict, StringCollection adress, List<string> email)
    {
       this.Dict = dict;
       this.Email_id = email;
       this.Adress = adress;
    }      
}

class Another
{
    static void Main(string[] args)
    {
        SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
        List<string> email = new List<string>();
        StringCollection adres = new StringCollection();
        Directry dir = new Directry( dict, adres,email);
        string key, adress, Email;
        int numbr;
        start_again:
        for (int i = 0; i <2; i++)
        {
            Console.WriteLine("enter name to be added in the directory");
            key = Console.ReadLine();
            Console.WriteLine("enter number to be added in the directory");
            numbr = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter address to be added in the directory");
            adress = Console.ReadLine();
            Console.WriteLine("enter your email-id to be added in the directory");
            Email = Console.ReadLine();
            dict.Add(key, numbr);
            email.Add(Email);
            adres.Add(adress);
        }
        Console.WriteLine("do you wish to continue-y/n?");
        char c = Convert.ToChar(Console.ReadLine());
        if (c == 'y')
        {
            Console.WriteLine("you said yes");
            goto start_again;
        }
        else
        {
            Console.WriteLine("no more entries can be added");
            Console.WriteLine("Name         Number          adress            email");
            foreach (object ot in dir)
            {
                Console.WriteLine(ot);
            }               
        }
        Console.ReadLine();
    }
}

Thanks.


Solution

  • This code is far from perfect but it should get you on your way.

     public class Directory
    {
        public List<string> EmailAddresses = new List<string>();
        public List<string> Addresses = new List<string>();
    
        public void Add(string email, string address)
        {
            EmailAddresses.Add(email);
            Addresses.Add(address);
        }
    }
    
    class Another
    {
        static void Main(string[] args)
        {
    
            SortedDictionary<string, Directory> _directory = new SortedDictionary<string, Directory>();
            string key, adress, Email;
            int numbr;
        start_again:
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("enter name to be added in the directory");
                key = Console.ReadLine();
                Console.WriteLine("enter number to be added in the directory");
                numbr = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("enter address to be added in the directory");
                adress = Console.ReadLine();
                Console.WriteLine("enter your email-id to be added in the directory");
                Email = Console.ReadLine();
    
                Directory dir = new Directory();
                dir.Add(Email, adress);
    
                _directory.Add(key, dir);
    
            }
            Console.WriteLine("do you wish to continue-y/n?");
            char c = Convert.ToChar(Console.ReadLine());
            if (c == 'y')
            {
                Console.WriteLine("you said yes");
                goto start_again;
            }
            else
            {
                Console.WriteLine("no more entries can be added");
                Console.WriteLine("Name         Number          adress            email");
                foreach (KeyValuePair<string, Directory> d in _directory)
                {
                    Console.WriteLine(string.Format("{0}, {1}, {2}", d.Key, d.Value.Addresses.First(), d.Value.EmailAddresses.First()));
                }
            }
            Console.ReadLine();
        }