1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
unit contactmanager;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
model;
type
{ TContactManager }
TContactManager = class(TMarkObject)
private
FCityList: TCityList;
FContactList: TContactList;
FCountryList: TCountryList;
procedure PopulateCountries;
procedure PopulateCities;
function GenPhone: string;
public
constructor Create; override;
destructor Destroy; override;
procedure PopulateContacts;
published
property CountryList: TCountryList read FCountryList;
property CityList: TCityList read FCityList;
property ContactList: TContactList read FContactList;
end;
// Global singleton
function gContactManager: TContactManager;
implementation
uses
SysUtils;
var
uContactManager: TContactManager;
const
LastNames: array[1..10] of string = ('Geldenhuys', 'Botha', 'Johnson', 'Fourie',
'Louw', 'Bougas', 'van der Mescht', 'Waskanski', 'Welgens', 'Viljoen');
FirstNames: array[1..10] of string = ('Graeme', 'Johan', 'Debbie', 'Freda',
'Jack', 'Ryno', 'Dirkus', 'Angela', 'Denise', 'Daniel');
StreetNames: array[1..11] of string = ('Stellenberg Rd', 'Stellendal Rd',
'Abelia', 'Main Rd', 'Links Drive', 'Short Street',
'Long Street', 'Loop Street', 'Hillside Rd', 'Mountain Rd', 'Beach Drive');
function gContactManager: TContactManager;
begin
if not Assigned(uContactManager) then
uContactManager:= TContactManager.Create;
result:= uContactManager;
end;
{ TContactManager }
procedure TContactManager.PopulateCountries;
begin
FCountryList.Add(TCountry.CreateNew('za', 'South Africa'));
FCountryList.Add(TCountry.CreateNew('gb', 'Great Britain'));
FCountryList.Add(TCountry.CreateNew('uk', 'Ukrain'));
FCountryList.Add(TCountry.CreateNew('fr', 'France'));
FCountryList.Add(TCountry.CreateNew('us', 'United States'));
FCountryList.Add(TCountry.CreateNew('gr', 'Germany'));
end;
procedure TContactManager.PopulateCities;
var
c: TCity;
begin
c:= TCity.CreateNew;
c.Name:= 'Somerset West';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'Cape Town';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'Pretoria';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'Durban';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'London';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['gb'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'Watford';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['gb'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'Frankfurt';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['gr'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'New York';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['us'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'San Fransisco';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['us'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'Paris';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['fr'], True));
FCityList.Add(c);
c:= TCity.CreateNew;
c.Name:= 'Big City';
c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['uk'], True));
FCityList.Add(c);
end;
function TContactManager.GenPhone: string;
begin
result:= '+27 ' + IntToStr(Random(9)) + IntToStr(Random(9)) + ' '
+ IntToStr(Random(9)) + IntToStr(Random(9)) + IntToStr(Random(9)) + '-'
+ IntToStr(Random(9)) + IntToStr(Random(9)) + IntToStr(Random(9)) + IntToStr(Random(9));
end;
constructor TContactManager.Create;
begin
inherited Create;
FCountryList:= TCountryList.Create;
FCountryList.Owner:= self;
FCityList:= TCityList.Create;
FCityList.Owner:= self;
FContactList:= TContactList.Create;
FContactList.Owner:= self;
end;
destructor TContactManager.Destroy;
begin
FContactList.Free;
FCityList.Free;
FCountryList.Free;
inherited Destroy;
end;
procedure TContactManager.PopulateContacts;
var
C: TContact;
I,J: Integer;
A: TAddress;
begin
PopulateCountries;
PopulateCities;
for I:= 1 to 10 do
begin
C:= TContact.CreateNew;
C.FirstName:= FirstNames[I];
C.LastName := LastNames[I];
C.Mobile := GenPhone;
C.Email := LowerCase(FirstNames[i])+ '@freepascal.org';
for J:= 1 to 1+Random(2) do
begin
A:= TAddress.CreateNew;
A.Street := StreetNames[1+Random(10)];
A.Nr := Random(100)+1;
A.City := FCityList[Random(10)];
A.Fax := GenPhone;
A.Telephone1:= GenPhone;
If Random(2)>0 then
A.Telephone2:= GenPhone;
C.AddressList.Add(A);
end;
FContactList.Add(C);
end;
end;
initialization
uContactManager:= nil;
finalization
uContactManager.Free;
end.
|