-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
651 lines (539 loc) · 17.2 KB
/
main.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <conio.h>
#include <stdlib.h>
using namespace std;
struct id
{
string Animename ;
string Favcharacters ;
int Rating ;
};
struct node
{
id data ;
node * next ;
node * back ;
};
class SDB
{
private :
node * first , *last , *new_node ;
long int type ;
int counter ; int flag = 0 ;
public:
SDB(){first = last = NULL ; int counter = 1; }
int Construct ( long int i ,string n ,string x , long double y )
{
if ( first == NULL)
{
type = i ;
new_node = new node ;
new_node -> data.Animename = n;
new_node -> data.Favcharacters = x ;
new_node -> data.Rating = y ;
new_node -> next = NULL;
new_node -> back = NULL;
first = new_node ;
last = new_node ;
counter++ ;
return 1 ;
}
else
{
cout << " You used create option previously \n " ;
return 0 ;
}
}
int insertelement ( string n , string x , long double y )
{
new_node = new node ;
new_node -> data.Animename = n;
new_node -> data.Favcharacters = x ;
new_node -> data.Rating = y ;
if( first != NULL)
{
switch ( type )
{
case 1 :
if ( first -> data.Animename[0] >= n[0])
{
first -> back = new_node ;
new_node -> next = first ;
new_node -> back = NULL ;
first = new_node ;
counter++ ;
return 1 ;
}
else if ( last -> data.Animename[0] <= n[0] )
{
last -> next = new_node ;
new_node -> next = NULL ;
new_node -> back = last ;
last = new_node ;
counter++ ;
return 1 ;
}
else
{
node * current = first ;
node * cur_next = current-> next ;
for ( ; cur_next -> next != NULL ; )
{
current = current -> next ;
cur_next = cur_next ->next ;
if( cur_next -> data.Animename[0] >= n[0])
break ;
}
cur_next -> back = new_node ;
new_node -> next = cur_next ;
new_node -> back = current ;
current -> next = new_node ;
counter++ ;
return 1 ;
}
break ;
case 2 :
if ( first -> data.Favcharacters >= x)
{
first -> back = new_node ;
new_node -> next = first ;
new_node -> back = NULL ;
first = new_node ;
counter++ ;
return 1 ;
}
else if ( last -> data.Favcharacters <= x )
{
last -> next = new_node ;
new_node -> next = NULL ;
new_node -> back = last ;
last = new_node ;
counter++ ;
return 1 ;
}
else
{
node * current = first ;
node * cur_next = current-> next ;
for ( ; cur_next -> next != NULL ; )
{
current = current -> next ;
cur_next = cur_next ->next ;
if( cur_next -> data.Favcharacters >= x)
break ;
}
cur_next -> back = new_node ;
new_node -> next = cur_next ;
new_node -> back = current ;
current -> next = new_node ;
counter++ ;
return 1 ;
}
break ;
}
}
else
{
cout << " Please use create option first \n";
return 0 ;
}
}
int deleteelement (string x )
{
if ( first != NULL)
{
node * del_first = first ;
node * del_last = last;
node * del_next = del_first -> next ;
node * del_back = last -> back ;
if ( first -> data.Favcharacters == x )
{
first = del_next ;
delete (del_first);
counter-- ;
return 1;
}
else if ( last -> data.Favcharacters == x)
{
last = del_back;
last -> next = NULL ;
delete (del_last);
counter-- ;
return 1;
}
else
{
for ( ; del_first ->next != NULL ; )
{
del_first = del_first -> next ;
del_back = del_first -> back ;
del_next = del_first -> next ;
if ( x == del_first -> data.Favcharacters)
{
del_next -> back = del_first ->back ;
del_back -> next = del_first ->next;
delete (del_first);
flag = 1 ;
counter-- ;
return 1;
}
}
}
if ( flag == 0 ) {
cout << "Wrong Favcharacters \n" ;
return 3;
}
}
else
{
cout << "Empty database !!\n";
return 0;
}
}
void print ( void )
{
cout << " Animename\t\tFavcharacters\tRating\n\n";
node* pri_ele = first ;
for ( ; pri_ele != NULL ; )
{
cout << pri_ele-> data.Animename << "\t" << pri_ele ->data.Favcharacters << "\t" << pri_ele->data.Rating << endl ;
pri_ele = pri_ele-> next ;
}
}
int savefile ( void )
{
node *save = first ;
int fs = 0 ;
ofstream fout("save.txt") ;
if (!fout.is_open()) // failed to open file
{
cout << "Could not open the file " << "save.txt" << endl;
return 0 ;
}
fout << " Animename\t\tFavcharacters\tRating\n\n";
for ( ; save != NULL ; )
{
fout << save-> data.Animename << "\t" << save ->data.Favcharacters << "\t" << save ->data.Rating << endl ;
save = save-> next ;
fs = 1 ;
}
fout.close();
if (fs == 0)
{
cout << "Empty database !! \n";
return 0 ;
}
else
{
cout << "Done \n";
return 1 ;
}
}
int searchelement ( string x)
{
node* sch = first ;
int f = 0;
if ( first == NULL )
{
cout << "Empty Database \n";
return 0 ;
}
else
{
while ( sch != NULL)
{
if( sch -> data.Favcharacters == x )
{
cout <<sch -> data.Animename << "\t" << sch -> data.Favcharacters << "\t" << sch -> data.Rating << endl ;
f = 1;
return 1;
break ;
}
sch = sch ->next;
}
if (f == 0)
{
cout << "Wrong Favcharacters \n" ;
return 3;
}
}
}
void help ( void )
{
system("CLS");
cout << "This program considered as simple database of Anime or Movie , you can make it ordered by Animename or by Favcharacters and Rating , there are some functions help you to create a database : \n"
<< "Crate Function : \n"
<< "You must use this is function at first to create database ,in this function you must enter type of order ( Animename or Favcharacters ) , if you want to insert another data you can use insert function \n"
<< "Insert/Delete/Search Functions :\n"
<< "In this function you can insert/delete/search on any number of data , you must use this functions after using create function \n"
<< "Save Function :\n"
<< "You can save data in file after you finish your work ,be attention the folder of program must contain (save.text)\n "
<< "Thanks for reading \n" ;
}
};
void show (void) ;
int check_num(int x = 0);
int menu ();
int main()
{
show();
SDB obj ;
long int type , test , num_cases ;
long int num_insert , num_del ,num_search ,check_insert , check_del ,check_search ;
long double gpa;
string Animename , id ;
test = 1;
cout << " Enter your choice: \n";
num_cases = check_num(3);
while( test == 1 )
{
switch ( num_cases )
{
case 1 :
system("CLS");
cout << "You choose 1 to create a database, Please enter the Animename, Favcharacters and Rating \n";
getline(cin ,Animename);
while ( Animename[0] < 65 or Animename[0] > 122)
{
cout << " Wrong input !\n";
getline(cin ,Animename);
}
getline(cin ,id);
while ( !(cin >> gpa) or gpa > 4)
{
cin.clear();
while ( cin.get() != '\n') continue;
cout << " Wrong input !\n";
}
while ( cin.get() != '\n') continue;
cout << " What type of database arrangement you want ? \n "
<< " 1-Animename 2-Favcharacters \n";
type = check_num(2);
obj.Construct(type , Animename , id , gpa);
num_cases = menu();
break ;
case 2 :
system("CLS");
cout << " You choose 2 to insert data , Please enter name of Anime you want to save . \n" ;
check_insert = 1 ;
num_insert = check_num(0) ;
for ( int j = 1 ; j <= num_insert ; j++)
{
if ( check_insert == 1)
{
cout << " Please enter the Animename, Favcharacters and Rating of the Anime\n";
getline(cin ,Animename);
while ( Animename[0] < 65 or Animename[0] > 122)
{
cout << " Wrong Animename ! \n";
getline(cin ,Animename);
}
getline(cin ,id);
while ( !(cin >> gpa) or gpa > 4)
{
cin.clear();
while ( cin.get() != '\n') continue;
cout << " Wrong input !\n";
}
while ( cin.get() != '\n') continue;
check_insert = obj.insertelement(Animename , id , gpa);
}
}
num_cases = menu();
break;
case 3 :
system("CLS");
cout << "You choose 3 to delete data, Please enter name of Anime you want to delete \n";
check_del = 1 ;
num_del = check_num(0);
for ( int k = 1 ; k <= num_del ; k++)
{
if ( check_del != 0)
{
cout << "Please enter Favcharacters of Anime \n";
getline(cin , id);
check_del = obj.deleteelement(id);
if ( check_del == 3) k-- ;
}
}
num_cases = menu();
break;
case 4 :
system("CLS");
cout << "You choose 4 to search on data,Please enter name of Anime you want to search on \n";
check_search = 1 ;
num_search = check_num();
for ( int l = 1 ; l <= num_search ; l++)
{
if ( check_search != 0 )
{
cout << " Please enter Favcharacters of Anime \n";
getline(cin , id);
check_search = obj.searchelement(id);
if ( check_search == 3) l--;
}
}
num_cases = menu();
break;
case 5 :
system("CLS");
obj.print() ;
num_cases = menu();
break ;
case 6 :
system("CLS");
int save_cheak ;
save_cheak =obj.savefile();
num_cases = menu();
break ;
case 7 :
obj.help();
num_cases = menu();
cout << "Contact @shuence on Instagram: \n";
break ;
case 8 :
exit(EXIT_SUCCESS);
break ;
default :
cout << "Wrong input ! \n";
cout << "Enter your choice: \n";
num_cases = check_num(3);
break ;
}
}
return 0;
}
int check_num(int x )
{
int num ;
switch ( x )
{
case 1 :
case 2 :
while ( !(cin >> num) or num > 2)
{
cin.clear();
while ( cin.get() != '\n') continue;
cout << " Wrong input !\n";
}
while ( cin.get() != '\n') continue;
break ;
case 3 :
while ( !(cin >> num) or num > 8)
{
cin.clear();
while ( cin.get() != '\n') continue;
cout << " Wrong input !\n";
}
while ( cin.get() != '\n') continue;
break ;
default :
while ( !(cin >> num) )
{
cin.clear();
while ( cin.get() != '\n') continue;
cout << " Wrong input !\n";
}
while ( cin.get() != '\n') continue;
break ;
}
return num ;
}
int login();
void show (void)
{
login();
cout <<"\n\t ------WELCOME TO DATABASE MANAGEMENT SYSTEM------- \n\n";
cout <<"\n\t ---------------Created By @Shuence --------------- \n\n";
cout << "********************************************************************************\n"
<< "********************************************************************************\n"
<< "*** 1- Create Database ***\n"
<< "*** 2- Insert Data ***\n"
<< "*** 3- Delete Data ***\n"
<< "*** 4- Search on Data ***\n"
<< "*** 5- Print Data ***\n"
<< "*** 6- Save ***\n"
<< "*** 7- Help ***\n"
<< "*** 8- Exit ***\n"
<< "********************************************************************************\n"
<< "********************************************************************************\n";
}
int menu()
{
int num_cases;
cout << " Do you want to do another operation?\n "
<< " 1- Yes 2-No \n";
int t = check_num(1);
if ( t == 1)
{
system("cls");
show();
cout << "Enter your choice: \n";
num_cases = check_num(3);
return num_cases;
}
else
return 8 ;
}
int login ()
{
//Low Protection Login Code
string userName;
string userPassword;
int loginAttempt = 0;
while (loginAttempt < 5)
{
cout << "\n\tPlease enter your user name: ";
cin >> userName;
cout << "\n\tPlease enter your user password: ";
cin >> userPassword;
if (userName == "Shuence" && userPassword == "pass")
{
cout << "\n\tWelcome There!\n";
//cp nsb
break;
}
else if (userName == "Prannn" && userPassword == "pass")
{
cout << "\n\tWelcome Admin!\n";
break;
}
else
{
cout << "Invalid login attempt. Please try again.\n" << '\n';
loginAttempt++;
}
}
if (loginAttempt == 5)
{
cout << "Too many login attempts! The program will now terminate.";
return 0;
}
cout << "\n\tThank you for logging in.\n\n";
system("PAUSE");
system("CLS");
/* string pass ="";
char ch;
cout << "\n\n\n\n\t\t\t\t Database Management System";
cout << "\n\n\n\n\n\n\n\t\t\t\t\tEnter Password: ";
ch = _getch();
while(ch != 13){//character 13 is enter
pass.push_back(ch);
cout << '*';
ch = _getch();
}
if(pass == "pass"){
cout << "\n\n\t\t\t\t\tAccess Granted \n";
}else{
cout << "\n\n\t\t\t\t\tAccess Aborted...Press Any Key To Try Again\n";
cout << "\n\n\n\n\n\n\n\t\t\t\t\ Thanks for using This System @Shuence ";
ch = _getch();
system("CLS");
login();
}
system("PAUSE");
system("CLS");*/
}