-
Notifications
You must be signed in to change notification settings - Fork 15
/
ForAzizbek.pas
150 lines (133 loc) · 2.89 KB
/
ForAzizbek.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
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
program MainProject;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
TUsers = Record
ATime: TDateTime;
Title: String[100];
Subscription: Boolean;
End;
TMessage = Record
MsgTitle: String;
MsgBody: String;
End;
procedure AddTitle;
var
F: File of TUsers;
Users: TUsers;
Begin
AssignFile(F, 'users.dat');
Write('Input current user title: ');
ReadLn(Users.Title);
Users.ATime := Now; // assigns current date
Users.Subscription := False; // initial state for subscription is False - Not subscribed
if FileExists('users.dat') then
begin
FileMode := 2; // Read/Write Mode
Reset(F);
Seek(F, System.FileSize(F)); // Go to last record to append
end
else
ReWrite(F);
Write(F, Users);
WriteLn;
CloseFile(F);
End;
procedure NotifyAllUsers;
var
F: File of TUsers;
Users: TUsers;
Messages: TMessage;
Begin
AssignFile(F, 'users.dat');
Write('Please give a message Title: ');
ReadLn(Messages.MsgTitle);
Write('Please give a message Body: ');
ReadLn(Messages.MsgBody);
if FileExists('users.dat') then
begin
Reset(F);
while Not EoF(F) do
begin
Read(F, Users);
WriteLn('-------------------');
WriteLn('Title: ', Users.Title);
WriteLn('Time: ', DateToStr(Users.ATime));
Users.Subscription := True;
WriteLn('Notified State: ', Users.Subscription);
end;
WriteLn;
WriteLn(Messages.MsgTitle);
WriteLn(Messages.MsgBody);
WriteLn('All Users Notified!');
WriteLn;
CloseFile(F);
end
else
WriteLn('Empty database.');
End;
procedure Search;
var
F: File of TUsers;
News: TUsers;
Keyword: String;
Found: Boolean;
Begin
AssignFile(F, 'users.dat');
Write('Input keyword to search for: ');
ReadLn(Keyword);
Found := False; // initialization
if FileExists('users.dat') then
begin
Reset(F);
while Not Eof(F) do
begin
Read(F, News);
if Pos(LowerCase(Keyword), LowerCase(News.Title)) > 0 then
begin
Found := True;
WriteLn('-------------------');
WriteLn('Title: ', News.Title);
WriteLn('Time: ', DateToStr(News.ATime));
end;
end;
CloseFile(F);
if Not Found then
WriteLn(Keyword, ' not found directed user')
else
WriteLn('Empty database.');
end;
End;
Function Menu: Char;
Begin
WriteLn('1. Add Users');
WriteLn('2. Notify Users');
WriteLn('3. Search');
WriteLn('x. Exit');
Write('Please input a selection : ');
ReadLn(Result);
End;
var Selection: Char;
Begin
WriteLn;
WriteLn('@@@@@@@ Welcome to Database @@@@@@@');
WriteLn;
try
Repeat
Selection := Menu;
case Selection of
'1':
AddTitle;
'2':
NotifyAllUsers;
'3':
Search;
end;
Until LowerCase(Selection) = 'x'; // exit the console
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
End.