-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.cpp
491 lines (390 loc) · 9.94 KB
/
user.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
File Name : user.cpp
Description :
Author : Garly Nugraha & Nazwa Fitriyani Zahra
Date : 08/12/2001
*/
/* ========== Header File ========== */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include "administrator.h"
#include "user.h"
/* ======= End of Header File ====== */
int MenuUser()
{
int Menu;
printf("|==================================|\n");
printf("| User Menu |\n");
printf("|==================================|\n");
printf("|1. Create |\n");
printf("|2. Read |\n");
printf("|3. Update |\n");
printf("|4. Delete |\n");
printf("|5. Back |\n");
printf("|==================================|\n");
printf("\n\n");
printf("Select Menu : ");
scanf("%d", &Menu);
system("cls");
switch (Menu) {
case 1:
/* Call Modul Create Data User */
CreateUser();
break;
case 2:
/* Call Modul Read Data User */
ReadUser();
break;
case 3:
/* Call Modul Update Data User */
UpdateUser();
break;
case 4:
/* Call Modul Delete Data User */
DeleteUser();
break;
case 5:
/* Call Modul Menu Administrator */
AdministratorMenu();
break;
default:
printf("ERROR : Sorry I don't know the answer to this one!!!");
getch();
system("cls");
/* Call Modul Menu User */
MenuUser();
break;
}
}
void CreateUser()
{
char More;
int Check;
User UserData;
FILE *f_user;
/* Open File */
f_user = fopen("User.DAT", "ab+");
/* Erro Handle About Open File */
if (!f_user)
{
printf ("ERROR : Sorry the file is unavailable!!!\n");
printf("Press Any Key to continue . . .");
getch();
system("cls");
MenuUser();
}
do {
system("cls");
printf("|=====================================|\n");
printf("| Create User |\n");
printf("|=====================================|\n");
printf("User Code : ");
fflush(stdin);
scanf("%d", &UserData.Code);
Check = CheckUserCode(UserData, UserData.Code);
if(Check != 0)
{
system("cls");
printf("ERROR : User code has been used, please enter another code!!!\n");
printf("Press Any Key to continue . . .");
getch();
system("cls");
CreateUser();
}
printf("Full Name : ");
fflush(stdin);
scanf("%[^\n]",&UserData.FullName);
printf("Username : ");
fflush(stdin);
gets(UserData.Username);
printf("Email : ");
fflush(stdin);
gets(UserData.Email);
printf("Password : ");
fflush(stdin);
gets(UserData.Password);
printf("Role (1/2) : ");
fflush(stdin);
scanf("%d", &UserData.Role);
fflush(stdin);
UserData.Status = 0;
fwrite(&UserData, sizeof(UserData), 1, f_user);
printf("\n");
printf("Do you want to create more data? (Y/N) ");
fflush(stdin);
More = getche();
} while (More == 'Y' || More == 'y');
/* Close File */
fclose(f_user);
system("cls");
printf("The data is already stored in the file!!!\n");
printf("Press Any Key to continue . . .");
getche();
system("cls");
/* Call Modul Menu User */
MenuUser();
}
void ShowUser()
{
FILE *f_user;
User UserData;
char Role[15], Status[7];
/* Open File */
f_user = fopen("User.DAT", "rt");
/* Error Handle About Open File */
if (!f_user)
{
printf ("ERROR : Sorry file cannot be open!!!\n");
getch();
/* Call Modul Menu User */
MenuUser();
}
printf("|==========================================================================================================================================|\n");
printf("| User Data |\n");
printf("|==========================================================================================================================================|\n");
printf("| User ID Full Name Username mail Password Role Status |\n");
printf("|==========================================================================================================================================|\n");
while ((fread(&UserData, sizeof(UserData), JUM_BLOK, f_user)) == JUM_BLOK)
{
if(UserData.Role == 1){
strcpy(Role, "Administrator");
} else if(UserData.Role == 2){
strcpy(Role, "Concierge");
} else{
strcpy(Role, "-");
}
if(UserData.Status == 1){
strcpy(Status, "Login");
} else{
strcpy(Status, "Logout");
}
printf(" %-5d %-26s %-15s %-38s %-15s %-17s %-10s\n", UserData.Code, UserData.FullName, UserData.Username, UserData.Email, UserData.Password, Role, Status);
}
printf("\n\n\n");
/* Close User */
fclose(f_user);
}
void ReadUser()
{
/* Call Modul Show Data User */
ShowUser();
printf("Press Any Key to continue . . .");
getche();
system("cls");
/* Call Modul Menu User */
MenuUser();
}
void UpdateUser()
{
int Code;
User UserData, TempUserData;
FILE *f_user, *f_tempuser;
fflush(stdin);
printf("Enter the User Code to be updated : ");
scanf("%d", &Code);
printf("Full Name : ");
fflush(stdin);
scanf("%[^\n]", &TempUserData.FullName);
printf("Username : ");
fflush(stdin);
gets(TempUserData.Username);
printf("Email : ");
fflush(stdin);
gets(TempUserData.Email);
printf("Password : ");
fflush(stdin);
gets(TempUserData.Password);
printf("Role (1/2) : ");
fflush(stdin);
scanf("%d", &TempUserData.Role);
fflush(stdin);
TempUserData.Status = 0;
/* Open File */
f_user = fopen("User.DAT", "rb");
f_tempuser = fopen("TempUser.DAT", "wb");
/* Error Handle About Open File */
if (!f_user)
{
printf ("ERROR : Sorry file cannot be open!!!\n");
getch();
system("cls");
MenuUser();
}
/* Error Handle About Open File */
if (!f_tempuser)
{
printf ("ERROR : Sorry file cannot be open!!!\n");
getch();
system("cls");
MenuUser();
}
while (fread(&UserData, sizeof(UserData),1, f_user))
{
if(Code == UserData.Code){
TempUserData.Code = UserData.Code;
fwrite(&TempUserData, sizeof(TempUserData), 1, f_tempuser);
} else {
fwrite(&UserData, sizeof(UserData), 1, f_tempuser);
}
}
/* Close File */
fclose(f_user);
fclose(f_tempuser);
/* Remove File */
remove("User.DAT");
/* Rename File */
rename("TempUser.DAT", "User.DAT");
system("cls");
printf("Data successfully updated!!!\n");
printf("Press Any Key to continue . . .");
getch();
system("cls");
/* Call Modul Menu User */
MenuUser();
}
int CheckUserCode(User UserData, int ID)
{
FILE *f_user;
/* Open File */
f_user = fopen("User.DAT","rb");
while(fread(&UserData, sizeof(UserData), 1, f_user))
{
if(ID == UserData.Code)
{
/* Close File */
fclose(f_user);
return 1;
}
}
}
void DeleteUser()
{
int MenuDelete;
/* Call Modul Show Data User */
ShowUser();
printf("|==================================|\n");
printf("| User Delete |\n");
printf("|==================================|\n");
printf("|1. One Record |\n");
printf("|2. All |\n");
printf("|3. Back |\n");
printf("|==================================|\n");
printf("\n");
printf("Select Menu : ");
scanf("%d", &MenuDelete);
system("cls");
switch (MenuDelete) {
case 1:
/* Call Modul Alert Delete One Record Data User */
AlertDeleteOneRecordUser();
break;
case 2:
/* Call Modul Alert Delete All Data User */
AlertDeleteAllUser();
break;
case 3:
/* Call Modul Menu User */
MenuUser();
break;
default:
printf("ERROR : Sorry I don't know the answer to this one!!!\n");
printf("Press Any Key to continue . . .");
getch();
system("cls");
/* Call Modul Delete Data User */
DeleteUser();
break;
}
}
void AlertDeleteOneRecordUser()
{
User UserData;
char Answer;
int Code;
printf("\n");
printf("Enter the User Code to be deleted : ");
scanf("%d", &Code);
fflush(stdin);
printf("\n");
printf("Are you sure? (Y/N)");
Answer = getche();
if (Answer == 'Y' || Answer == 'y')
{
system("cls");
DeleteOneRecordUser(UserData, Code);
printf("Data successfully deleted!!!\n");
printf("Press Any Key to continue . . .");
getch();
system("cls");
DeleteUser();
}
system("cls");
/* Call Modul Delete Data User */
DeleteUser();
}
void DeleteOneRecordUser(User UserData, int ID)
{
FILE *f_user, *f_tempuser;
int Code, Check;
Check = CheckUserCode(UserData, ID);
if(Check != 0)
{
/* Open File */
f_user = fopen("User.DAT", "rb");
f_tempuser = fopen("TempUser.DAT", "wb");
while (fread(&UserData, sizeof(UserData), 1, f_user))
{
Code = UserData.Code;
if (Code != ID){
fwrite(&UserData, sizeof(UserData), 1, f_tempuser);
}
}
/* Close File */
fclose(f_user);
fclose(f_tempuser);
/* Remove File */
remove("User.DAT");
/* Rename File */
rename("TempUser.DAT", "User.DAT");
} else{
system("cls");
printf("ERROR : User data with code %d not found!!!\n", ID);
printf("Press Any Key to continue . . .");
getch();
system("cls");
/* Call Modul Alert Delete One Record Data User */
AlertDeleteOneRecordUser();
}
}
void AlertDeleteAllUser()
{
char Answer;
printf("Are you sure? (Y/N)");
Answer = getche();
if (Answer == 'Y' || Answer == 'y')
{
system("cls");
/* Call Modul Delete All Data User */
DeleteAllUser();
}
system("cls");
/* Call Modul Delete Data User */
DeleteUser();
}
void DeleteAllUser()
{
FILE *f_user;
/* Open File */
f_user = fopen("User.DAT","wb");
/* Close File */
fclose(f_user);
printf("All data was deleted!!!\n");
printf("Press Any Key to continue . . .");
getch();
system("cls");
/* Call Modul Delete Data User */
DeleteUser();
}