In this exercise you'll be writing code to keep track of international dialling codes via an international dialing code dictionary.
The dictionary uses an integer for its keys (the dialing code) and a string (country name) for its values.
You have 9 tasks which involve the DialingCodes
static class.
Implement the (static) method DialingCodes.GetEmptyDictionary()
that returns an empty dictionary.
DialingCodes.GetEmptyDictionary();
// => empty dictionary
There exists a pre-populated dictionary which contains the following 3 dialing codes: "United States of America" which has a code of 1, "Brazil" which has a code of 55 and "India" which has a code of 91. Implement the (static) DialingCodes.GetExistingDictionary()
method to return the pre-populated dictionary:
DialingCodes.GetExistingDictionary();
// => 1 => "United States of America", 55 => "Brazil", 91 => "India"
Implement the (static) method DialingCodes.AddCountryToEmptyDictionary()
that creates a dictionary and adds a dialing code and associated country name to it.
DialingCodes.AddCountryToEmptyDictionary(44, "United Kingdom");
// => 44 => "United Kingdom"
Implement the (static) method DialingCodes.AddCountryToExistingDictionary()
that adds a dialing code and associated country name to a non-empty dictionary.
DialingCodes.AddCountryToExistingDictionary(DialingCodes.GetExistingDictionary(),
44, "United Kingdom");
// => 1 => "United States of America", 44 => "United Kingdom", 55 => "Brazil", 91 => "India"
Implement the (static) method DialingCodes.GetCountryNameFromDictionary()
that takes a dialing code and returns the corresponding country name. If the dialing code is not contained in the dictionary then an empty string is returned.
DialingCodes.GetCountryNameFromDictionary(
DialingCodes.GetExistingDictionary(), 55);
// => "Brazil"
DialingCodes.GetCountryNameFromDictionary(
DialingCodes.GetExistingDictionary(), 999);
// => string.Empty
Implement the (static) method DialingCodes.CheckCodeExists()
to check whether a dialing code exists in the dictionary.
DialingCodes.CheckCodeExists(DialingCodes.GetExistingDictionary(), 55);
// => true
Implement the (static) method DialingCodes.UpdateDictionary()
which takes a dialing code and replaces the corresponding country name in the dictionary with the country name passed as a parameter. If the dialing code does not exist in the dictionary then the dictionary remains unchanged.
DialingCodes.UpdateDictionary(
DialingCodes.GetExistingDictionary(), 1, "Les États-Unis");
// => 1 => "Les États-Unis", 55 => "Brazil", 91 => "India"
DialingCodes.UpdateDictionary(
DialingCodes.GetExistingDictionary(), 999, "Newlands");
// 1 => "United States of America", 55 => "Brazil", 91 => "India"
Implement the (static) method DialingCodes.RemoveCountryFromDictionary()
that takes a dialing code and will remove the corresponding record, dialing code + country name, from the dictionary.
DialingCodes.RemoveCountryFromDictionary(
DialingCodes.GetExistingDictionary(), 91);
// => 1 => "United States of America", 55 => "Brazil"
Implement the (static) method DialingCodes.FindLongestCountryName()
which will return the name of the country with the longest name stored in the dictionary.
DialingCodes.FindLongestCountryName(
DialingCodes.GetExistingDictionary());
// => "United States of America"