Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PhonebookCommands
- {
- class Program
- {
- private static Dictionary<string, string> phonebook;
- static void Main(string[] args)
- {
- phonebook = new Dictionary<string, string>();
- Input();
- }
- private static void Input()
- {
- string command = Console.ReadLine();
- while(command!="End")
- {
- ProcessCommand(command);
- command = Console.ReadLine();
- }
- }
- private static void ProcessCommand(string command)
- {
- string[] commandData = command
- .Split(' ')
- .ToArray();
- switch (commandData[0])
- {
- case "Add":
- Add(commandData);
- break;
- case "SearchByName":
- SearchByName(commandData);
- break;
- case "SearchByPhone":
- SearchByPhone(commandData);
- break;
- case "RemoveByName":
- RemoveByName(commandData);
- break;
- case "RemoveByPhone":
- RemoveByPhone(commandData);
- break;
- case "PrintAll":
- PrintAll(commandData);
- break;
- default:
- break;
- }
- }
- private static void PrintAll(string[] commandData)
- {
- foreach (var contact in phonebook)
- {
- Console.WriteLine(contact);
- }
- }
- private static void RemoveByPhone(string[] commandData)
- {
- string phone = commandData[1];
- string name = "";
- foreach (var contact in phonebook)
- {
- string contactPhone = contact.Value;
- if (contactPhone == phone)
- {
- name = contact.Key; //the .Key is the contact name!
- }
- }
- phonebook.Remove(name);
- }
- private static void RemoveByName(string[] commandData)
- {
- string name = commandData[1];
- phonebook.Remove(name);
- }
- private static void SearchByPhone(string[] commandData)
- {
- throw new NotImplementedException();
- }
- private static void SearchByName(string[] commandData)
- {
- string name = commandData[1];
- if (phonebook.ContainsKey(name))
- {
- //имаме такъв човек
- Console.WriteLine("{0} -> {1}",
- name,
- phonebook[name]);
- }
- else
- {
- //нямаме такъв човек
- Console.WriteLine($"Contact {name} does not exist.");
- }
- }
- private static void Add(string[] commandData)
- {
- //Add Gosho 08888888888888
- string name = commandData[1];
- string phone = commandData[2];
- //1. проверяваме дали имаме вече такъв човек
- if (phonebook.ContainsKey(name))
- {
- //ако имаме - сменяме номера
- phonebook[name] = phone;
- }
- else
- {
- //ако нямаме - добавяме човека с номера
- phonebook.Add(name, phone);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment