Esempio n. 1
0
        static void AddContact()
        {
            Console.WriteLine("Name? ");
            string name_str = Console.ReadLine().Trim();

            if (Contacts.ContainsKey(name_str))
            {
                Console.WriteLine("Contact name \"{0}\" already in list\n", name_str);
                return;
            }

            // Using a long integer (64bit) otherwise can be invalid
            Console.WriteLine("Phone? ");
            string phone_str = Console.ReadLine().Trim();

            if (!Int64.TryParse(phone_str, out long phone_num))
            {
                Console.WriteLine("Invalid phone number");
                return;
            }

            Console.WriteLine("Email? ");
            string email_str = Console.ReadLine().Trim();

            Console.WriteLine("Occupation? ");
            string occu_str = Console.ReadLine().Trim();

            Contact con = new Contact(name_str, phone_num, email_str, occu_str);

            Contacts.Add(name_str, con);

            Console.WriteLine(con.ToString());
        }
Esempio n. 2
0
 private void writeToFile(Contact contact)
 {
     using (StreamWriter sw = new StreamWriter("Contacts.txt", true))
     {
         sw.WriteLine(contact.ToString());
     }
 }
Esempio n. 3
0
 static Contact FindContact()
 {
     if (Contacts.Count != 0)
     {
         Console.WriteLine("Name of contact? ");
         string input_str = Console.ReadLine().Trim();
         if (Contacts.ContainsKey(input_str))
         {
             Contact contact_found = Contacts[input_str];
             Console.WriteLine(contact_found.ToString());
             return(contact_found);
         }
         else
         {
             Console.WriteLine("Contact not found\n");
             return(null);
         }
     }
     Console.WriteLine("Contact list is empty\n");
     return(null);
 }
Esempio n. 4
0
        static void EditContact()
        {
            if (Contacts.Count != 0)
            {
                Contact contact_edit = FindContact();
                if (contact_edit == null)
                {
                    return;
                }
                else
                {
                    while (true)
                    {
                        Console.WriteLine("Edit what? ");
                        string line = Console.ReadLine().Trim().ToLower();
                        switch (line)
                        {
                        case "name":
                        {
                            // Backup the old name
                            string old_name = contact_edit.Name;

                            // Read new name
                            Console.WriteLine("New name? ");
                            string new_name = Console.ReadLine().Trim();

                            // Update the dictionary entry
                            contact_edit.Name  = new_name;
                            Contacts[new_name] = contact_edit;
                            Contacts.Remove(old_name);

                            break;
                        }

                        case "phone":
                        {
                            Console.WriteLine("New phone? ");
                            string phone_str = Console.ReadLine().Trim();
                            if (!Int64.TryParse(phone_str, out long phone_num))
                            {
                                Console.WriteLine("Invalid phone number");
                                return;
                            }
                            contact_edit.Phone = phone_num;
                            break;
                        }

                        case "email":
                        {
                            Console.WriteLine("New email? ");
                            string email_str = Console.ReadLine().Trim();
                            contact_edit.Email = email_str;
                            break;
                        }

                        case "occupation":
                        {
                            Console.WriteLine("New occupation? ");
                            string occ_str = Console.ReadLine().Trim();
                            contact_edit.Occupation = occ_str;
                            break;
                        }

                        default:
                            Console.WriteLine("Invalid input\n");
                            break;
                        }
                        Console.WriteLine("\n" + contact_edit.ToString());
                        return;     // Otherwise it will keep looping
                    }
                }
            }
            else
            {
                Console.WriteLine("Contact list is empty\n");
                return;
            }
        }