1 using System;
2 using System.Collections;
3
4 public class PhoneBook
5 {
6 static PhoneBook() : instance(new PhoneBook())
7 {
8 }
9 public static PhoneBook& Instance()
10 {
11 return *instance;
12 }
13 public int GetNumber(const ustring& name) const
14 {
15 Map<ustring, int>.ConstIterator i = map.CFind(name);
16 if (i != map.CEnd())
17 {
18 return i->second;
19 }
20 return 0;
21 }
22 private PhoneBook()
23 {
24 map[u"Stroustrup, B."] = 12345;
25 map[u"Gödel, K."] = 97531;
26 map[u"Knuth, D."] = 34567;
27 map[u"Stepanov, A."] = 98765;
28 map[u"Sjøberg, D."] = 13579;
29 }
30 public default ~PhoneBook();
31 private static UniquePtr<PhoneBook> instance;
32 private Map<ustring, int> map;
33 }
34
35 void main()
36 {
37 ustring name = u"Gödel, K.";
38 int number = PhoneBook.Instance().GetNumber(name);
39 if (number != 0)
40 {
41 Console.Out() << "Phone number of " << name << " is " << number << endl();
42 }
43 else
44 {
45 Console.Out() << "Phone number not found";
46 }
47 }