25.07.2017 Views

Intro-CSharp-Book-v2015

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

808 Въведение в програмирането със C#<br />

}<br />

static void ProcessQuery(string query)<br />

{<br />

if (query.StartsWith("list("))<br />

{<br />

int listLen = "list(".Length;<br />

string name = query.Substring(<br />

listLen, query.Length-listLen-1);<br />

name = name.Trim().ToLower();<br />

PrintAllMatches(name);<br />

}<br />

else if (query.StartsWith("find("))<br />

{<br />

string[] queryParams = query.Split(<br />

new char[] { '(', ' ', ',', ')' },<br />

StringSplitOptions.RemoveEmptyEntries);<br />

string name = queryParams[1];<br />

name = name.Trim().ToLower();<br />

string town = queryParams[2];<br />

town = town.Trim().ToLower();<br />

string nameAndTown = CombineNameAndTown(town, name);<br />

PrintAllMatches(nameAndTown);<br />

}<br />

else<br />

{<br />

Console.WriteLine(query + " is invalid command!");<br />

}<br />

}<br />

}<br />

static void PrintAllMatches(string key)<br />

{<br />

List allMatches;<br />

if (phone<strong>Book</strong>.TryGetValue(key, out allMatches))<br />

{<br />

foreach (string entry in allMatches)<br />

{<br />

Console.WriteLine(entry);<br />

}<br />

}<br />

else<br />

{<br />

Console.WriteLine("Not found!");<br />

}<br />

Console.WriteLine();<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!