-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCitizensDatabase.h
1771 lines (1396 loc) · 39.3 KB
/
CitizensDatabase.h
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Muhammad Usman shahid
20i-1797
T
*/
#ifndef CITIZENSDB_H
#define CITIZENSDB_H
//for strings
#include <string>
#include <cstring>
//for file reading
#include <fstream>
using namespace std;
template <class T>
struct Data
{
//Node structure for data pipeline, implement data members as per requirement
/*will read the data from the CCID will be store in this and when reading from the CBID the
criminal portion will be made null mean in the respective node of dataline no entry of crime and
vice versa for CCID. */
//for storing CBID
// cnicCBID; //for holding the cninc that would be read from the file
// name; //name of the person from file
// fname; //father name
// gender; //gender
// age; //age
//age doesn't exisit age is the street in the address thus updation will be done accordingly
// address; //The address
// nationality; //to fetch nationality
//for storing CCID
// cnicCCID; //cnic fetched from the criminal database
// detailsofCrime; //details of crimes holder
// punishment; //punishment holder
// fine; //will carry the fine
//the above all mentoned data will be store in the array of 11
T infotoPopulate[11] = {};
//a pointer that will point to the next
Data<T> *next;
};
template <class T>
class DataPipeline
{
/* A linkedlist based queue to implement databases
Implement additional functions as per requirement
*/
//in qeue we will maintain the two pointers of front and and rear
Data<T> *front; //the first member in qeue
Data<T> *rear; //the last one in the qeue
public:
DataPipeline()
{
//no qeue so pointing to null --- no front and no rear as no qeue exist
front = NULL;
rear = NULL;
}
//returns the data of the first in the queue
T frontElement()
{
T toReturn = "null";
if (is_empty())
{
cout << "\n\t\t Data Pipeline is empty " << endl;
return toReturn;
}
else
{
toReturn = front->infotoPopulate[0];
//for making a string from data array of datapipeline
for (int i = 1; i < 11; i++)
{
toReturn += " " + front->infotoPopulate[i];
}
return toReturn;
}
}
//returning the last element
T back()
{
T toReturn = "null";
if (is_empty())
{
cout << "\n\t\t Data Pipeline is empty " << endl;
return toReturn;
}
else
{
toReturn = rear->infotoPopulate[0];
//for making a string from data array of datapipeline
for (int i = 1; i < 11; i++)
{
toReturn += " " + rear->infotoPopulate[i];
}
return toReturn;
}
}
T splitWords(T &tobreak, int &pos)
{
T word; //a temporary words
pos = tobreak.find(" "); //finding the space the first word
word = tobreak.substr(0, pos); //taking that and storing as word
pos++; //go to next index beacause word starts after space
return word; //returning the word
}
//will read the file and will enter the data
void enqueue(T fromFile, T type)
{
T temp;
T dataExtraxted[11] = {}; //11 data entries all CBID and CCID
int pos = 0;
int last = 0;
int i = 0;
//copying the string taken from the file
temp = fromFile;
// fromfile is the string having the whole data that will be furthure spilleted and will be added into the qeue
// type will see which data is going to be entered --- from which file
// for CBID
// 1. cninc 2.name 3.father name 4.gender 5.age 6.two words address 7.nationality
// breaking the string accordingly
if (type == "CBID")
{
//only first seven are related to the CBID
for (i = 0; i < 7; i++)
{
//after each space the new data in string but only address is of two words
//pos is containing the position where ended last
dataExtraxted[i] = splitWords(temp, pos); //returning the word from the string
// temp should be missing the starting one --- updating the temp in splitwords
//the address of two words
if (i == 5)
{
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
dataExtraxted[i] += " " + splitWords(temp, pos); //adding the next word
}
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
}
//making all other null --- starting the i left
for (; i < 11; i++)
{
dataExtraxted[i] = "null";
}
}
else //for CCID thus weiting data for that
{
//for ccid all CBID data will be null thus
for (i = 0; i < 7; i++)
{
dataExtraxted[i] = "null";
}
//the entries of crime thus --- starting i from the remaining
for (; i < 11; i++)
{
dataExtraxted[i] = splitWords(temp, pos); //returning the word from the string
//but the life punishment is 4 words thus
if (i == 9)
{
//add three more words
for (int j = 0; j < 3; j++)
{
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
dataExtraxted[i] += " " + splitWords(temp, pos);
}
}
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
}
}
//now entering the data accordingly
//the first entry
if (is_empty())
{
Data<string> *temp = new Data<string>; //the node to be enter
//enetring the info
for (int i = 0; i < 11; i++)
{
temp->infotoPopulate[i] = dataExtraxted[i];
}
temp->next = NULL; //no else
//front and rear pointing to only one element
front = temp;
rear = temp;
}
else //not the first member then
{
Data<string> *temp = new Data<string>; //the node to be enter
//enetring the info
for (int i = 0; i < 11; i++)
{
temp->infotoPopulate[i] = dataExtraxted[i];
}
rear->next = temp; //pointing to new added in qeue
temp->next = NULL; //pointing to none
rear = temp; //last one is now temp;
}
}
T dequeue()
{
//will deque the whole string of data
T toReturn = "null";
if (is_empty())
{
cout << "\n\t\t Data pipeline is already empty " << endl;
return toReturn;
}
else
{
toReturn = front->infotoPopulate[0];
//for making a string from data array of datapipeline
for (int i = 1; i < 11; i++)
{
toReturn += " " + front->infotoPopulate[i];
}
//deleting the element at start
Data<string> *temp = front;
front = front->next;
//mean last in the queue
if (front == NULL)
{
//the qeue is now empty
front = NULL;
rear = NULL;
}
delete temp;
return toReturn;
}
}
bool is_empty()
{
if (front == NULL && rear == NULL) //if front and rear are null mean no qeue
{
return true; //the qeue is empty
}
return false; //qeue is not empty
}
//the one that will read from the files and will enque in the datapipeline
//first 50 will be CBID and next 10 will be CCID
void moveIndataPipeline()
{
fstream toRead; //for reading
//temporary taking string
string tempData;
//entering data from CBID file to the data pipeline
//opening file CBID
toRead.open("CBID.txt", ios::in);
while (!toRead.eof())
{
getline(toRead, tempData);
//enqueing the data
enqueue(tempData, "CBID"); //CBID type
}
toRead.close();
//entering data from CCID file to the data pipeline
//opening file CCID
toRead.open("CCID.txt", ios::in);
while (!toRead.eof())
{
getline(toRead, tempData);
//enqueing the data
enqueue(tempData, "CCID"); //CCID type
}
toRead.close();
}
};
//forward decalaration --- to avoid dependencies
template <class T>
class CCID_NODE;
template <class T>
struct CBID_NODE
{
//Implement additional data members and functions as per requirement
//making the data that is given for the CBID
T cnicB;
T name;
T fatherName;
T gender;
//age is just the name for the street number also above in the code
T age; //age doesn't exisit age is the street in the address thus updation will be done accordingly
T address;
T nationality;
CBID_NODE<T> *next; //for next
CBID_NODE<T> *previous; //for pervious
CCID_NODE<T> *referencetoCCID; //refer to CCID
//initallay having no reference
CBID_NODE()
{
referencetoCCID = NULL;
}
};
template <class T>
class CBIDList
{
//Implement data members and functions as per requirement
CBID_NODE<T> *head;
CBID_NODE<T> *tail;
public:
CBIDList()
{
//at starting no list is there
head = NULL;
tail = NULL;
//also not pointing anywhere in CCID
}
//to acess head
CBID_NODE<T> *getHead() const
{
return head;
}
//addition will be done assendingly
void addTome(T toapend, DataPipeline<T> &D)
{
//making an array of 7 to hold the data
T dataHolder[7] = {};
int pos = 0, last = 0, indexofList = 0, curretIndex = 1;
T temp = toapend;
//only first seven are related to the CBID
for (int i = 0; i < 7; i++)
{
//after each space the new data in string but only address is of two words
//pos is containing the position where ended last
dataHolder[i] = D.splitWords(temp, pos); //returning the word from the string
// temp should be missing the starting one --- updating the temp in splitwords
//the address of two words
if (i == 5)
{
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
dataHolder[i] += " " + D.splitWords(temp, pos); //adding the next word
}
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
}
//now will enter the data in that index
//making the temp data to be inserted in the list
CBID_NODE<string> *newData = new CBID_NODE<string>;
//adding the data
newData->cnicB = dataHolder[0];
newData->name = dataHolder[1];
newData->fatherName = dataHolder[2];
newData->gender = dataHolder[3];
newData->age = dataHolder[4];
newData->address = dataHolder[5];
newData->nationality = dataHolder[6];
//cnic is in the first index --- finding the index of list where to insert
//index is hypothetical that insert after that -- for first the entry will be at first thus
if (!emptyList())
{
CBID_NODE<string> *iterator = head;
//finding the destination index
indexofList = findIndex_for_entry(dataHolder[0]);
//iterate till you find null aur the counting index icreases the destination index
while (iterator && curretIndex < indexofList)
{
curretIndex++;
iterator = iterator->next;
}
if (iterator == NULL)
{
//inserting at last
tail->next = newData;
newData->previous = tail;
newData->next = NULL;
tail = newData;
}
else if (indexofList == 0)
{
//inserting in the start
newData->next = head;
head->previous = newData;
newData->previous = NULL;
head = newData;
}
else
{
//inserting in between
//according to the iterator where iterator is stopeed
newData->next = iterator->next;
newData->previous = iterator;
iterator->next = newData;
iterator->next->previous = newData;
}
}
else
{
//the first element thus
newData->next = NULL; //nothing more
newData->previous = NULL;
head = newData; //stat and end are same
tail = newData;
}
}
//will be a finding index where to insert --- ascendingly
int findIndex_for_entry(T cnic)
{
//stoi for converting the string to int for comparision
int cnicEntered = stoi(cnic); //the enetered one --- the enqeueing
int cnicAlready = 0; //in list
int index = 0; //the iteration count where we are
//for iterating
CBID_NODE<string> *iterator = head;
while (iterator) //while iterator not equal to null
{
cnicAlready = stoi(iterator->cnicB);
if (cnicEntered < cnicAlready)
{
return index; //saving that lower value place --- fids any greter mean this is right place
}
index++; //going next
iterator = iterator->next; //assigning next
}
return index; //if not means last
}
//for populating the data from data pipeline
void populateMe(DataPipeline<T> &D)
{
T dataTopopulate;
//as static database was given first 50 were the CBID in the dataPipeline
for (int i = 0; i < 50; i++)
{
//the data will be whole string that would be breakdown further
dataTopopulate = D.dequeue(); //making enteries to come out from qeue
addTome(dataTopopulate, D); //adding in the list of CBID in a acsending manner of cnic
}
}
//for checking is list empty or not
bool emptyList()
{
//head and pointer pointing to nothing --- mean no list
if (head == NULL && tail == NULL)
{
return true;
}
return false;
}
//will validate whether the cnic exist in list or not
bool validateCNIC(int eCNIC)
{
int cnicR = 0;
CBID_NODE<T> *current = head; //to iterate in the list
while (current)
{
//taking the cnic in string
cnicR = stoi(current->cnicB);
if (cnicR == eCNIC)
{
return true; //found --- CNIC is correct
}
//going next
current = current->next;
}
return false; //no found have seen the list
}
//for adding the reference
void addReference(CCID_NODE<T> *C, int cnic)
{
//the cnic in database
int cnicDB = 0;
CBID_NODE<T> *temp = head;
//iterating till end
while (temp)
{
cnicDB = stoi(temp->cnicB); //the database cnic
//to add reference for
if (cnicDB == cnic)
{
//the required cnic
temp->referencetoCCID = C; //referring to ccid member
break; //done
}
//move next
temp = temp->next;
}
}
//serach it
T searchbyCNIC(int eCNIC)
{
T searchedResult = "null"; //final results storing
int cnicR = 0;
CBID_NODE<T> *current = head; //to iterate in the list
while (current)
{
//taking the cnic in string
cnicR = stoi(current->cnicB);
if (cnicR == eCNIC) //to aceess this one
{
//combining all data of the current other than cnic
searchedResult = current->name + " " + current->fatherName + " " + current->gender + " " + current->age + " " + current->address + " " + current->nationality;
if (current->referencetoCCID != NULL)
{
searchedResult += " " + current->referencetoCCID->crimesRead;
}
return searchedResult; //found --- CNIC is correct
}
//going next
current = current->next;
}
return searchedResult;
}
//updates the name
void updateName(int cnic, T nameE)
{
//the cnic in database
int cnicDB = 0;
CBID_NODE<T> *temp = head;
//iterating till end
while (temp)
{
cnicDB = stoi(temp->cnicB); //the database cnic
//to add reference for
if (cnicDB == cnic)
{
//the required cnic
//changing the name and breaking
temp->name = nameE;
break; //done
}
//move next
temp = temp->next;
}
}
//updates the father name
void updateFname(int cnic, T nameE)
{
//the cnic in database
int cnicDB = 0;
CBID_NODE<T> *temp = head;
//iterating till end
while (temp)
{
cnicDB = stoi(temp->cnicB); //the database cnic
//to add reference for
if (cnicDB == cnic)
{
//the required cnic
//changing the Father name and breaking
temp->fatherName = nameE;
break; //done
}
//move next
temp = temp->next;
}
}
//update the address
void updateAddress(int cnic, T nameE)
{
//the first part is the street number thus spliting the enter word ansd assigning accordingly
//the strret no is mentioned by age in the data
int pos = 0, last = 0;
T noinAddress; //a temporary words
pos = nameE.find(" "); //finding the space the first word
noinAddress = nameE.substr(0, pos); //taking that and storing as word
pos++; //go to next index beacause word starts after space
//now updating the string
//finding the end of string
last = nameE.size();
//updating the string
nameE = nameE.substr(pos, last); //now string contain address other tahn number
//the cnic in database
int cnicDB = 0;
CBID_NODE<T> *temp = head;
//iterating till end
while (temp)
{
cnicDB = stoi(temp->cnicB); //the database cnic
//to add reference for
if (cnicDB == cnic)
{
//the required cnic
//updating the address first part
temp->age = noinAddress;
//changing the remaining address and breaking
temp->address = nameE;
break; //done
}
//move next
temp = temp->next;
}
}
//updating nationality
void updateNationality(int cnic, T nameE)
{
//the cnic in database
int cnicDB = 0;
CBID_NODE<T> *temp = head;
//iterating till end
while (temp)
{
cnicDB = stoi(temp->cnicB); //the database cnic
//to add reference for
if (cnicDB == cnic)
{
//the required cnic
//changing the nationality and breaking
temp->nationality = nameE;
break; //done
}
//move next
temp = temp->next;
}
}
void display()
{
CBID_NODE<string> *iterator = head;
while (iterator)
{
cout << iterator->cnicB << " ";
iterator = iterator->next;
}
cout << endl;
}
};
//for crimes list
template <class T>
struct crimesNode
{
//making the singly list of crimes
T crimeDetails;
T crimePunishment;
T crimeFine;
crimesNode *next; //to trace the next --- singly linked list
};
//the singly linked list
template <class T>
class Crimes
{
private:
crimesNode<T> *head;
public:
Crimes()
{
head = NULL; //mean no list is there
}
crimesNode<T> *getHead() const
{
return head;
}
//will always append the data because only to make crimes list
void addCrime(T dataAdd, DataPipeline<T> &D, T &addCrimedata)
{
//making the array of 3 that will store the data of crimes
T crimeData[3] = {};
int pos = 0, last = 0, indexofList = 0, curretIndex = 1;
T temp = dataAdd;
//spliting the data and saving in the array
//the 3 data entry for CBID
for (int i = 0; i < 3; i++)
{
crimeData[i] = D.splitWords(temp, pos); //returning the word from the string
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
//the punishment of 4 words
if (i == 1)
{
//1 is taken already
for (int j = 0; j < 3; j++)
{
//will store first the data and will not add if year was missing as in first data entry
T temp2 = D.splitWords(temp, pos);
if (temp2 != "-") //if not this grabage then add
{
crimeData[i] += " " + temp2;
}
//finding the end of string
last = temp.size();
//updating the string
temp = temp.substr(pos, last);
}
}
}
//now adding that data into the crimes
//the data slot to be added
crimesNode<T> *tempC = new crimesNode<T>;
tempC->crimeDetails = crimeData[0];
tempC->crimePunishment = crimeData[1];
tempC->crimeFine = crimeData[2];
//now setting the pointer
if (isEmpty()) //mean first element in the list
{
tempC->next = NULL;
head = tempC;
}
else
{
//not the first
crimesNode<T> *iterator = head;
while (iterator->next) //go to the last element
{
iterator = iterator->next; //finding the last one so to append crimes list
}
//the last is found as iterator thus
iterator->next = tempC;
tempC->next = NULL;
}
if (addCrimedata == " ")
{
//saving things in addCrimedata --- that will update string of crime in the ccid
addCrimedata = crimeData[0] + " " + crimeData[1] + " " + crimeData[2];
}
else
{
addCrimedata += " " + crimeData[0] + " " + crimeData[1] + " " + crimeData[2];
}
}
void displayCrimes()
{
crimesNode<T> *tempC = head;
while (tempC)
{
cout << tempC->crimeDetails << " " << tempC->crimePunishment << " " << tempC->crimeFine << " " << endl;
tempC = tempC->next;
}
}
void update(T details, T punish, T fine)
{
//will update head data
head->crimeDetails = details;
head->crimePunishment = punish;
head->crimeFine = fine;
}
bool deleteme(T details, T punish, T fine)
{
//for iterating in the list
crimesNode<T> *tempDel = head;
T toDelete = details + " " + punish + " " + fine;
T already;
while (tempDel)
{
already = tempDel->crimeDetails + " " + tempDel->crimePunishment + " " + tempDel->crimeFine;
if (already == toDelete)
{
//delete the crime
//finding the previous of tempDel
crimesNode<T> *previousFinder = head;
if (previousFinder != tempDel)
{
//iterate till that is not the next element
while (previousFinder->next != tempDel)
{
previousFinder = previousFinder->next;
}
//deleteing the tempdel but setting pointers before that
previousFinder->next = tempDel->next;
delete tempDel;
return true;
}
else
{
//to remove head data
if(previousFinder->next != NULL)
{
delete previousFinder; //deleting
head = tempDel->next;
}
}
return true;
}
tempDel = tempDel->next;
}
return false;
}
//checking list is empty or not
bool isEmpty()
{
if (head == NULL)
{
return true; //is empty
}
return false; //not empty
}
};
template <class T>
struct CCID_NODE
{
//Implement additional data members and functions as per requirement
//amking the data for ccid node
T cnicC;
T crimesRead; //for storing whole crimes then will move to crime list
//the singly crime list
Crimes<T> crime;
CCID_NODE<T> *next; //for next
CCID_NODE<T> *previous; //for previous
//for reference to CBID
CBID_NODE<T> *referencetoCBID;
//initallay having no reference
CCID_NODE()