Guest User

Phonebook Upgrade

a guest
Jun 30th, 2016
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _13.PhonebookUpdate
  8. {
  9.     class PhonebookUpdate
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             string input = Console.ReadLine();
  15.             SortedDictionary<string, string> phonebook = new SortedDictionary<string, string>();
  16.             List<string> result = new List<string>();
  17.             bool listed = false;
  18.  
  19.             while (input.ToUpper() != "END")
  20.             {
  21.                 if (input == "ListAll")
  22.                 {
  23.                     listed = true;
  24.                     input = Console.ReadLine();
  25.                     continue;
  26.                 }
  27.  
  28.                 string[] text = input.Split(' ');
  29.                 string action = text[0];
  30.                 string name = text[1];
  31.  
  32.                 if (action == "A")
  33.                 {
  34.                     string number = text[2];
  35.                     if (phonebook.ContainsKey(name))
  36.                         phonebook[name] = number;  
  37.                     else
  38.                         phonebook.Add(name, number);
  39.                 }
  40.                 else if (action == "S")
  41.                 {
  42.                     string answer = "";
  43.                     if (phonebook.ContainsKey(name))
  44.                         answer = string.Format("{0} -> {1}", name, phonebook[name]);
  45.                     else
  46.                         answer = string.Format("Contact {0} does not exist.", name);
  47.                     result.Add(answer);
  48.                 }
  49.                 input = Console.ReadLine();
  50.             }
  51.  
  52.             if (listed)
  53.             {
  54.                 foreach (var contact in phonebook)
  55.                 {
  56.                     Console.WriteLine("{0} -> {1}", contact.Key, contact.Value);
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 Console.WriteLine(string.Join("\n", result));
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment