-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuGradeSchool.pas
111 lines (96 loc) · 2.91 KB
/
uGradeSchool.pas
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
unit uGradeSchool;
interface
uses
Generics.Collections;
type
TRoster = TList<string>;
ISchool = interface(IInterface)
['{623FFDE3-F742-4236-8733-132ADC2A0D89}']
procedure Add(AName : string; AGrade : integer = 1);
function Grade(AGrade : integer = 1) : TRoster;
function Roster : TRoster;
end;
TSchool = class(TInterfacedObject, ISchool)
constructor Create; overload;
destructor Destroy; override;
procedure Add(AName : string; AGrade : integer = 1);
function Grade(AGrade : integer = 1) : TRoster;
function Roster : TRoster;
private
FStudentList : TDictionary<integer, TRoster>;
end;
function NewSchool : ISchool;
implementation
uses
SysUtils;
constructor TSchool.Create;
begin
inherited;
FStudentList := TDictionary<integer, TRoster>.Create;
end;
destructor TSchool.Destroy;
var
StudentListKeys : TArray<Integer>;
begin
if Assigned(FStudentList) then
begin
SetLength(StudentListKeys, FStudentList.Keys.Count);
try
StudentListKeys := FStudentList.Keys.ToArray;
for var i := 0 to Length(StudentListKeys) - 1 do
begin
if Assigned(FStudentList.Items[StudentListKeys[i]]) then
FStudentList.Items[StudentListKeys[i]].Free;
FStudentList.Remove(StudentListKeys[i]);
end;
FStudentList.Clear;
finally
SetLength(StudentListKeys, 0);
end;
end;
FStudentList.Free;
inherited;
end;
procedure TSchool.Add(AName : string; AGrade : integer = 1);
begin
if FStudentList.ContainsKey(AGrade) then
begin
FStudentList.Items[AGrade].Add(AName);
FStudentList.Items[AGrade].Sort;
end
else
if FStudentList.TryAdd(AGrade, TRoster.Create) then
FStudentList.Items[AGrade].Add(AName);
end;
function TSchool.Grade(AGrade : integer = 1) : TRoster;
begin
Result := TRoster.Create;
if FStudentList.ContainsKey(AGrade) then
for var i:= 0 to (FStudentList.Items[AGrade].Count - 1) do
begin
Result.Add(FStudentList.Items[AGrade][i]);
end;
end;
function TSchool.Roster : TRoster;
var
StudentListKeys : TArray<integer>;
begin
Result := TRoster.Create;
if 0 < FStudentList.Count then
begin
SetLength(StudentListKeys, FStudentList.Count);
try
StudentListKeys := FStudentList.Keys.ToArray;
TArray.Sort<integer>(StudentListKeys);
for var i := 0 to (Length(StudentListKeys) - 1) do
Result.AddRange(FStudentList.Items[StudentListKeys[i]]);
finally
SetLength(StudentListKeys,0);
end;
end;
end;
function NewSchool : ISchool;
begin
Result := TSchool.Create;
end;
end.