Advertisement
Guest User

Untitled

a guest
Sep 21st, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Phonebook
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         SortedDictionary<string, string> phonebook = new SortedDictionary<string, string>();
  9.  
  10.         string input = "PersonName-PhoneNumber";
  11.         string[] nameAndNumber = new string[2];
  12.         bool search = false;
  13.  
  14.         Console.WriteLine("Enter \"search\" to toggle between input and search.");
  15.         Console.WriteLine("Enter \"END\" to exit.");
  16.         Console.WriteLine("Phone input format: \"Person Name-Phone Number\"");
  17.  
  18.         while (input != "END")
  19.         {
  20.             // Toggle between search and input.
  21.             if (input == "search")
  22.             {
  23.                 search = !search;
  24.                 input = "PersonName-PhoneNumber"; // Make sure we do not get infinite loop.
  25.             }
  26.             else
  27.             {
  28.                 // If we are searching
  29.                 if (search)
  30.                 {
  31.                     if (phonebook.ContainsKey(input)) // If name exists.
  32.                     {
  33.                         Console.WriteLine($"{input} -> {phonebook[input]}");
  34.                     }
  35.                     else
  36.                     {
  37.                         Console.WriteLine($"Contact {input} does not exist.");
  38.                     }
  39.                 }
  40.                 else // If we aren't searching, we are adding numbers.
  41.                 {
  42.                     if (!phonebook.ContainsKey(input))
  43.                     {
  44.                         nameAndNumber = input.Split('-');
  45.                         phonebook.Add(nameAndNumber[0], nameAndNumber[1]);
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             input = Console.ReadLine();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement