开发者

Phone Directory Example

开发者 https://www.devze.com 2023-02-04 15:21 出处:网络
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 em

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

    开发者_如何学JAVASortedDictionary<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.


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


You aren't coding this in a particularly good OO way.

I would suggest changing your code structure to something along the lines of...

public class DirectoryEntry
{
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public string Addresss { get; set; }
    public string Email { get; set; }
}

public class Directory
{
    List<DirectoryEntry> _entries;

    public Directory()
    {
        _entries = new List<DirectoryEntry>();
    }

    public List<DirectoryEntry> Entries { get { return _entries; } }
}

You can flesh all that out to keep the directory ordered, not allow duplicate names, or whatever you want.

Now you can have something like

Directory directory = new Directory();
directory.Entries.Add(new DirectoryEntry { Name = "Tom", Number = "01293485943" })

foreach (var entry in directory.Entries.OrderBy(de => de.Name))
{
     Console.WriteLine("Name: {0}, Number: {1}", entry.Name, entry.Number);
}


By the looks of it you are declaring the dictionary

Directry dir = new Directry( dict, adres,email);

But then you aren't updating it with the values you read from the console. You could make a method within the Dictionary class which you pass the dict, adres and email objects to it.

Edit to grab values from dictionary:

Your Directry object has a SortedDictonary, make it public.

public SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();

Then enumerate like this:

 foreach (KeyValuePair<string, int> kvp in dir.Dict)
            {
                Console.WriteLine(kvp.Key, kvp.Value);
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号