-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTollBooth_Management_System.cpp
More file actions
1731 lines (1532 loc) · 54.8 KB
/
TollBooth_Management_System.cpp
File metadata and controls
1731 lines (1532 loc) · 54.8 KB
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
/*
*************** Toll Booth Management System *************
TMF 1434- DATA STRUCTURE AND ALGORITHM
PROJECT
======================= Group Members ====================
1. ABU SAYED - 59395
2. MUHAMMAD HASSIF IMRAN BIN MUHAMMAD HARRIS IRWAN - 58818
3. MUHAMMAD FAREZ BIN SAID - 59727
4. MUHAMMAD ZULFIQAR BIN RAZALI - 59745
****************** DUE DATE - 24 MAY, 2017 ***************
*/
// Preprocessor directives or header files
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <limits>
#include <sstream>
#include <map>
#include <ctime>
#include <vector>
#include <algorithm>
// Constant variables declaration
#define RTYPES 4
#define SPACE ' '
#define UNDERLINE '-'
#define NAMEW 20
#define ADJUST 11
#define NUMW NAMEW
#define BOOLW 10
#define BSTATUS 2
#define SUCCESS 0
#define ERROR 1
#define MAX 10000
#define MIN 1000
/* This is an error recovery routine which recovers when you have typed an illegal character
where it is expecting a number.
*/
void inputNumber(int &data) {
std::cin >> data;
while (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid Input Expected Number" << std::endl;
std::cin >> data;
}
}
// Keeps a reference counting for every object allocated using malloc
class ReferenceCount
{
private:
int mCount;
public:
// Add reference to allocated object
void addReference()
{
mCount++;
}
// Release reference to allocated object
int releaseReference()
{
return --mCount;
}
};
// Keeps track of objects allocated on heap through reference counting
template < typename T >
class SmartPointer
{
private:
T* mData;
ReferenceCount* mReference;
public:
//Constructor's
SmartPointer() : mData(0), mReference(0)
{
mReference = new ReferenceCount();
mReference->addReference();
}
SmartPointer(T* pValue) : mData(pValue), mReference(0)
{
mReference = new ReferenceCount();
mReference->addReference();
}
SmartPointer(const SmartPointer<T>& sp) : mData(sp.mData), mReference(sp.mReference)
{
mReference->addReference();
}
//Destructor
~SmartPointer()
{
if (mReference->releaseReference() == 0)
{
delete mData;
delete mReference;
}
}
//Dereference object
T& operator* ()
{
return *mData;
}
//Reference object
T* operator-> ()
{
return mData;
}
//Overloaded equal to operator
SmartPointer<T>& operator = (const SmartPointer<T>& sp)
{
if (this != &sp)
{
if (mReference->releaseReference() == 0)
{
delete mData;
delete mReference;
}
mData = sp.mData;
mReference = sp.mReference;
mReference->addReference();
}
return *this;
}
//Increase reference count to object tracked by this smart pointer
void addReference()
{
mReference->addReference();
}
//Decrease reference count to object tracked by this smart pointer
void releaseReference()
{
mReference->releaseReference();
}
//Reference object
T* getData() {
return mData;
}
//Overloaded ostream for use with cout
friend std::ostream &operator<<(std::ostream &output, const SmartPointer &sp) {
return output << *(sp.mData);
}
};
//Array that dynamically resizes it's capacity
template<class T>
class DynamicArray {
private:
int mCount;
int mCapacity;
T **mArray;
//To calclulate the new size of the array
void resize(int capacity) {
T **mTempArray = new T*[capacity];
mCapacity = capacity;
for (int i = 0; i<mCount; i++) {
mTempArray[i] = mArray[i];
}
mArray = mTempArray;
}
public:
//Constructor
DynamicArray() {
mCount = 0;
mCapacity = 2;
mArray = new T*[mCapacity];
}
//Destructor
~DynamicArray() {
}
//Get the number of items in this array
int getLength() const {
return mCount;
}
void setItem(SmartPointer<T> data, int index) {
mArray[index] = data.getData();
}
//Get item at index
SmartPointer<T> getItem(int index) {
if (!(index >= 0 && index<mCount)) {
//error
}
SmartPointer<T> item(mArray[index]);
item.addReference();
return item;
}
//Delete item at index
void deleteItem(int index) {
SmartPointer<T> temp = mArray[index];
for (int i = index + 1; i<mCount; i++) {
mArray[i - 1] = mArray[i];
}
mCount--;
if (mCount == mCapacity / 4) {
resize(mCapacity / 2);
}
}
//Append an item at the last
void append(SmartPointer<T> item) {
item.addReference();
if (mCount == mCapacity) {
resize(mCapacity * 2);
}
mArray[mCount] = item.getData();
mCount++;
}
//Overloaded ostream for use with cout
friend std::ostream &operator<<(std::ostream &output, const DynamicArray &dArray) {
for (int i = 0; i<dArray.mCount; i++) {
output << std::left << std::setw(NUMW) << std::setfill(SPACE) << i + 1 << *(dArray.mArray[i]);
}
return output;
}
};
template <class T, class V>
SmartPointer<T> binarySearch(SmartPointer<DynamicArray<T > > data, V &search, int(*compare)(SmartPointer<T> a, V &b)) {
int first = 0;
int last = data->getLength() - 1;
int middle = (first + last) / 2;
while (first <= last)
{
if (compare(data->getItem(middle), search)<0)
first = middle + 1;
else if (compare(data->getItem(middle), search)>0)
last = middle - 1;
else
return data->getItem(middle);
middle = (first + last) / 2;
}
return NULL;
}
template <class T>
void sort(SmartPointer<DynamicArray<T > > data, int(*compare)(SmartPointer<T> a, SmartPointer<T> b)) {
for (int i = 1, j = 0; i<data->getLength(); i++)
{
SmartPointer<T> tempA = data->getItem(i);
tempA.addReference();
j = i - 1;
while (j >= 0)
{
SmartPointer<T> tempB = data->getItem(j);
tempB.addReference();
if (!(compare(tempA, tempB)<0)) {
break;
}
data->setItem(tempB, j + 1);
tempB.releaseReference();
j = j - 1;
}
data->setItem(tempA, j + 1);
tempA.releaseReference();
}
}
//Forward declaration of classes
template<class T>
class PositionalListIteraor;
template<class T>
class PositionalList;
template<class T>
class Position;
class Hotel;
class CustomerData;
//Double Linked List
template<class T>
class DoubleLinkedBase {
public:
//Data structure for double linked list
class Node {
public:
SmartPointer<T> mElement;
Node *mPrevious;
Node *mNext;
Node(SmartPointer<T> element, Node *previous, Node *next) {
mElement = element;
mPrevious = previous;
mNext = next;
}
~Node() {
}
friend class Position<T>;
};
Node *mHeader;
Node *mTrailer;
int mSize;
//Constructor
DoubleLinkedBase() {
mHeader = new Node(NULL, NULL, NULL);
mTrailer = new Node(NULL, NULL, NULL);
mHeader->mNext = mTrailer;
mTrailer->mPrevious = mHeader;
mSize = 0;
}
//Get the size of the list
int getLength() {
return mSize;
}
//Check if list is empty
bool isEmpty() {
return mSize == 0;
}
//Insert data between a predecessor and successor
Node* insertBetween(SmartPointer<T> element, Node *predecessor, Node *successor) {
Node *newest = new Node(element, predecessor, successor);
predecessor->mNext = newest;
successor->mPrevious = newest;
mSize += 1;
return newest;
}
//Delete the given node
void deleteNode(Node *node) {
Node *predecessor = node->mPrevious;
Node *successor = node->mNext;
predecessor->mNext = successor;
successor->mPrevious = predecessor;
//delete node;
mSize -= 1;
}
//Destructor
~DoubleLinkedBase() {
Node *header = mHeader->mNext;
while (mHeader != mTrailer->mPrevious) {
Node *tmp = header->mNext;
deleteNode(header);
header = tmp;
}
delete mHeader;
delete mTrailer;
}
};
//Container for a linked list node
template<class T>
class Position {
private:
PositionalList<T> *mContainer;
typename DoubleLinkedBase<T>::Node *mNode;
public:
friend class PositionalList<T>;
//Constructor
Position() {
mContainer = NULL;
mNode = NULL;
}
Position(const Position &p) {
mContainer = p.mContainer;
mNode = p.mNode;
}
Position(PositionalList<T> *list, typename DoubleLinkedBase<T>::Node *node) {
mContainer = list;
mNode = node;
}
//Overloaded equals operator
void operator=(const Position &p) {
mContainer = p.mContainer;
mNode = p.mNode;
}
//Get the element corresponding to this Position
SmartPointer<T> getElement() {
return mNode->mElement;
}
//Overloaded ostream for use with cout
friend std::ostream &operator<<(std::ostream &output, Position &pList) {
output << pList.mNode->mElement;
return output;
}
friend class PositionalListIteraor<T>;
};
template<class T>
class PositionalList : DoubleLinkedBase<T> {
private:
//Get the header node
typename DoubleLinkedBase<T>::Node * getHeader() {
return DoubleLinkedBase<T>::mHeader;
}
//Get the trailer node
typename DoubleLinkedBase<T>::Node * getTrailer() {
return DoubleLinkedBase<T>::mTrailer;
}
public:
//Get the length of the positional list
int getLength() {
return DoubleLinkedBase<T>::getLength();
}
//Get the first element corresponding to the list
Position<T> getFirst() {
return Position<T>(this, DoubleLinkedBase<T>::mHeader->mNext);
}
//Get the last element corresponding to the list
Position<T> getLast() {
return Position<T>(this, DoubleLinkedBase<T>::mTrailer->mPrevious);
}
//Get the element after given position
Position<T> getAfter(const Position<T> &p) {
return Position<T>(this, p.mNode->mNext);
}
//Add the element at first position
Position<T> addFirst(SmartPointer<T> element) {
return Position<T>(this, DoubleLinkedBase<T>::insertBetween(element, DoubleLinkedBase<T>::mHeader, DoubleLinkedBase<T>::mHeader->mNext));
}
//Add the element at last position
Position<T> addLast(SmartPointer<T> element) {
return Position<T>(this, DoubleLinkedBase<T>::insertBetween(element, DoubleLinkedBase<T>::mTrailer->mPrevious, DoubleLinkedBase<T>::mTrailer));
}
//Add the element before a given position
Position<T> addBefore(Position<T> &p, SmartPointer<T> element) {
return Position<T>(this, DoubleLinkedBase<T>::insertBetween(element, p.mNode->mPrevious, p.mNode));
}
//Add the element after a given position
Position<T> addAfter(Position<T> &p, SmartPointer<T> element) {
return Position<T>(this, DoubleLinkedBase<T>::insertBetween(element, p.mNode, p.mNode->mNext));
}
//Delete an item at the given position
void deleteItem(Position<T> &p) {
DoubleLinkedBase<T>::deleteNode(p.mNode);
}
//Delete an item at the given index
void deleteAtIndex(int index) {
SmartPointer<PositionalListIteraor<T> > list(new PositionalListIteraor<T>(this));
int i = 1;
do {
Position<T> search = list->next();
if (i == index) {
deleteItem(search);
break;
}
i++;
} while (!list->isLast());
}
//Get an item at the given index
Position<T> getItemAtIndex(int index) {
Position<T> search;
SmartPointer<PositionalListIteraor<T> > list(new PositionalListIteraor<T>(this));
int i = 1;
do {
search = list->next();
if (i == index)
break;
i++;
} while (!list->isLast());
return search;
}
//Overloaded ostream for use with cout
friend std::ostream &operator<<(std::ostream &output, PositionalList &pList)
{
if (!(pList.getLength() > 0))
return output;
SmartPointer<PositionalListIteraor<T> > list(new PositionalListIteraor<T>(&pList));
int index = 1;
do {
Position<T> search = list->next();
output << std::left << std::setw(NUMW) << std::setfill(SPACE) << index << search.getElement();
index++;
} while (!list->isLast());
return output;
}
friend class PositionalListIteraor<T>;
};
//Stack used in this case for task handling
template<class T>
class Stack : public PositionalList<T> {
public:
//Push an element to front of the stack
void push(SmartPointer<T> element) {
PositionalList<T>::addFirst(element);
}
//Get the topmost element
Position<T> top() {
return PositionalList<T>::getFirst();
}
//Pop the recently inserted element
void pop() {
Position<T> topElement = top();
PositionalList<T>::deleteItem(topElement);
}
};
//Stack used in this case for task handling
template<class T>
class Queue : public PositionalList<T> {
public:
//Push an element to front of the stack
void enqueue(SmartPointer<T> element) {
PositionalList<T>::addLast(element);
}
//Get the topmost element
Position<T> front() {
return PositionalList<T>::getFirst();
}
//Pop the recently inserted element
void dequeue() {
Position<T> topElement = front();
PositionalList<T>::deleteItem(topElement);
}
//Overloaded ostream for use with cout
friend std::ostream &operator<<(std::ostream &output, Queue &pList) {
if (!(pList.getLength() > 0))
return output;
SmartPointer<PositionalListIteraor<T> > list(new PositionalListIteraor<T>(&pList));
int index = 1;
do {
Position<T> search = list->next();
output << std::left << std::setw(NUMW) << std::setfill(SPACE) << index << search.getElement();
index++;
} while (!list->isLast());
return output;
}
};
//Iteraor for looping through the list
template<class T>
class PositionalListIteraor {
PositionalList<T> *mPlist;
Position<T> mCurrentPosition;
public:
//Constructor
PositionalListIteraor() {
}
PositionalListIteraor(PositionalList<T> *list) {
mPlist = list;
mCurrentPosition = (Position<T>(list, mPlist->getHeader()));
}
//Get the first element of the list
Position<T> first() {
return mPlist->getFirst();
}
//Get the last element of the list
Position<T> last() {
return mPlist->getLast();
}
//Get the next element
Position<T> next() {
mCurrentPosition = mPlist->getAfter(Position<T>(mCurrentPosition.mContainer, (mCurrentPosition.mNode)));
return mCurrentPosition;
}
//Check if we have reached end of the list
bool isLast() {
return mPlist->getTrailer() == mCurrentPosition.mNode->mNext;
}
//Destructor
~PositionalListIteraor() {
}
};
//Class for holding the date information
class Date {
private:
int mDay;
int mMonth;
int mYear;
public:
Date() {
mDay = mMonth = mYear = 0;
}
Date(int day, int month, int year) {
mDay = day; mMonth = month; mYear = year;
}
//Set the day
void setDay(int day) {
mDay = day;
}
//Set the month
void setMonth(int month) {
mMonth = month;
}
//Set the year
void setYear(int year) {
mYear = year;
}
//Get the day
int getDay() {
return mDay;
}
//Get the month
int getMonth() {
return mMonth;
}
//Get the year
int getYear() {
return mYear;
}
//Check if it is a leap year
bool isLeap(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
//Get the number of days passed in the given year
int getDaysPassedInYear() {
int days[] = { 31,(isLeap(mYear)) ? 29 : 28,31,30,31,30,31,31,30,31,30,31 };
int sum = mDay;
for (int i = 0; i<mMonth; i++) {
sum += days[i];
}
return sum;
}
//Compute max of two value
int max(int a, int b) {
return (a>b) ? a : b;
}
//Compute min of two value
int min(int a, int b) {
return (a<b) ? a : b;
}
//Compare two dates
int operator>=(Date &other) {
if (mYear > other.mYear)
return true;
if (mYear < other.mYear)
return false;
if (mMonth > other.mMonth)
return true;
if (mMonth < other.mMonth)
return false;
return mDay >= other.mDay;
}
bool operator==(Date &other) {
if (mYear == other.mYear && mMonth == other.mMonth && mDay == other.mDay)
return true;
return false;
}
//Get the difference between two dates in days
int operator-(Date &other)
{
int sum = 0;
for (int i = min(mYear, other.mYear); i<max(mYear, other.mYear); i++) {
sum += isLeap(i) ? 366 : 365;
}
int daysThis = getDaysPassedInYear();
int daysThat = other.getDaysPassedInYear();
sum += max(daysThat, daysThis) - min(daysThis, daysThat) + 1;
return sum;
}
//Overloaded istream for used with cin
friend std::istream& operator>>(std::istream &input, Date &date) {
std::cout << "Enter The Date(YYYY/MM/DD): ";
int days[] = { 31,(date.isLeap(date.mYear)) ? 29 : 28,31,30,31,30,31,31,30,31,30,31 };
while (true) {
input >> date.mYear;
if (std::cin.get() != '/')
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid Date" << std::endl;
std::cout << "Please Re-enter the Date:" << std::endl;
continue;
}
input >> date.mMonth;
if (date.mMonth < 1 || date.mMonth > 12) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
date.mMonth = 12;
std::cout << "Invalid Month" << std::endl;
std::cout << "Please Re-enter the Date:" << std::endl;
continue;
}
if (std::cin.get() != '/')
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid Date" << std::endl;
std::cout << "Please Re-enter the Date:" << std::endl;
continue;
}
input >> date.mDay;
if (input.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid Date" << std::endl;
std::cout << "Please Re-enter the Date:" << std::endl;
continue;
}
if (days[date.mMonth - 1] < date.mDay || date.mDay < 0) {
date.mDay = days[date.mMonth - 1];
std::cout << "Invalid Day" << date.mDay << std::endl;
std::cout << "Please Re-enter the Date:" << std::endl;
continue;
}
break;
}
return input;
}
//Overloaded ostream for use with cout
friend std::ostream &operator<<(std::ostream &output, const Date &date) {
std::string months[] = { "JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC" };
output << std::right << std::setw(2) << std::setfill('0') << date.mDay << "/" << std::right << std::setw(3) << std::setfill(SPACE) << months[date.mMonth - 1] << "/" << std::right << std::setw(4) << std::setfill('0') << date.mYear;
return output;
}
operator std::string() const {
std::string months[] = { "JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC" };
std::stringstream output;
output << std::right << std::setw(2) << std::setfill('0') << mDay << "/" << std::right << std::setw(3) << std::setfill(SPACE) << months[mMonth - 1] << "/" << std::right << std::setw(4) << std::setfill('0') << mYear;
return output.str();
}
};
std::vector<int> uid;
//Generate a random uid for passcards
int getRandom() {
srand(time(NULL));
int no = 0;
while ((no = rand() % (MAX - MIN + 1) + MIN) && std::find(uid.begin(), uid.end(), no) != uid.end()) {
}
uid.push_back(no);
return no;
}
class PassCard
{
public:
enum VehicleType { TWO_AXL, THREE_MORE_AXL, TAXI, BUS, UNKNOWN };
enum WheelerType { THREE_WHLR, FOUR_WHLR, FIVE_WHLR, SIX_WHLR, UNK_WHLR };
private:
static int uid; //generate a sequential id
int mTag; //store the sequential id to objects
long mPassNumber; //pass number for each pass card
float mRechargeAmount; //current balance
Date mDate;
public:
//overloaded constructors
PassCard() :mTag(uid++) {
}
PassCard(long regNo, Date date, float amount) :mTag(uid++) {
mPassNumber = regNo;
mRechargeAmount = amount;
mDate = date;
}
//get the current tag, associated with an transaction
int getTag() {
return mTag;
}
//get the current date
Date &getDate() {
return mDate;
}
//set the current date
void setDate(Date &date) {
mDate = date;
}
//get pass card number
long getPassNumber() {
return mPassNumber;
}
//set pass card number
void setPassNumber(long regNo) {
mPassNumber = regNo;
}
//set the balance
void setRechargeAmount(float rechargeAmt) {
mRechargeAmount = rechargeAmt;
}
//get the current balance
float getRechargeAmount() {
return mRechargeAmount;
}
//Print a header
static void printHeader() {
std::cout << std::left << std::setw(NAMEW) << "INDEX" << std::left << std::setw(NAMEW) << "PASS CARD NO" << std::left << std::setw(NAMEW) << "DATE" << std::setw(NAMEW) << "RECHARGE AMT(RM)" << std::endl;
}
//Overloaded cout
friend std::ostream &operator<<(std::ostream &output, PassCard &passcard) {
std::cout << std::left << std::setw(NAMEW) << passcard.mPassNumber << std::left << std::setw(NAMEW) << passcard.mDate << std::setfill(SPACE) << std::setw(NAMEW) << passcard.mRechargeAmount << std::endl;
return output;
}
//Overloaded ostream for use with cin
friend std::istream& operator>>(std::istream &input, PassCard &passcard) {
input >> passcard.mDate;
passcard.mPassNumber = getRandom();
std::cout << "Your Pass Card Number: " << passcard.mPassNumber << std::endl;
std::cout << "\nPlease Enter Recharge Amount: RM ";
// Check the Entered Amount and display the correct direction
while (std::cin >> passcard.mRechargeAmount && passcard.mRechargeAmount < 10.0) {
std::cout << "Sorry! You can not top-up less than RM 10.00" << std::endl;
std::cout << "\nPlease Re-enter Recharge Amount: RM \n";
}
return input;
}
};
class TollRate
{
static int uid;
int mTag; //A tag for toll Rate
PassCard::VehicleType mVType; //Vehicle type
PassCard::WheelerType mWType; //Wheeler type
float mToll;
public:
TollRate() :mTag(uid++) {}
TollRate(PassCard::VehicleType vType, PassCard::WheelerType wType, float toll) :mTag(uid++) {
mVType = vType;
mWType = wType;
mToll = toll;
}
//print the header for the toll rate
static void printHeader() {
std::cout << std::left << std::setw(NAMEW) << "INDEX" << std::setw(80) << "VEHICLE TYPE" << std::setw(NAMEW) <<"TOLL RATE" << std::endl;
}
//get the vehicle type
PassCard::VehicleType getVehicleType() {
return mVType;
}
//get the wheeler type
PassCard::WheelerType getWheelerType() {
return mWType;
}
//get the toll
float getToll() {
return mToll;
}
//set the toll
void setToll(float toll) {
mToll = toll;
}
//overloaded cout
friend std::ostream &operator<<(std::ostream &output, TollRate &rate) {
std::string description;
if (rate.mVType == PassCard::TAXI) {
description = "Taxis";
}
else if (rate.mVType == PassCard::BUS) {
description = "Buses";
}
else if (rate.mVType == PassCard::TWO_AXL && (rate.mWType == PassCard::THREE_WHLR || rate.mWType == PassCard::FOUR_WHLR)) {
description = "Vehicles with 2 axles and 3 or 4 wheels excluding taxis";
}
else if (rate.mVType == PassCard::TWO_AXL && (rate.mWType == PassCard::FIVE_WHLR || rate.mWType == PassCard::SIX_WHLR)) {
description = "Vehicles with 2 axles and 5 or 6 wheels excluding buses";
}
else if (rate.mVType == PassCard::THREE_MORE_AXL) {
description = "Vehicles with 3 or more axles";
}
output << std::left << std::setw(80) << description << std::setw(NAMEW) << rate.mToll << std::endl;
return output;
}
};
class TollTransaction
{
static int uid;
int mTag; //A tag for transaction
Date mDate; //Date of the transaction
int mPassCardTag; // Associated with a pass card
float mAmount; //Amount of the transaction
PassCard::VehicleType mVType; //Vehicle type associated with the transaction
PassCard::WheelerType mWType; //Wheeler type associated with the transaction
public:
TollTransaction(Date date, int tag, PassCard::VehicleType vType, PassCard::WheelerType wType, float amount) :mTag(uid++) {
mDate = date;
mPassCardTag = tag;
mAmount = amount;
mVType = vType;
mWType = wType;
}
// getters and setters for the associated data members
PassCard::VehicleType getVehicleType()
{
return mVType;
}
int getPassCardTag()
{
return mPassCardTag;
}
PassCard::WheelerType getWheelerType()
{
return mWType;
}
Date &getDate()
{
return mDate;
}
float getAmount()
{
return mAmount;
}
int getTag()
{
return mTag;
}
std::string getDescription()
{
std::string description;
if (mVType == PassCard::TAXI) {
description = "Taxis";
}
else if (mVType == PassCard::BUS) {
description = "Buses";
}
else if (mVType == PassCard::TWO_AXL && (mWType == PassCard::THREE_WHLR || mWType == PassCard::FOUR_WHLR)) {
description = "Vehicles with 2 axles and 3 or 4 wheels excluding taxis";
}
else if (mVType == PassCard::TWO_AXL && (mWType == PassCard::FIVE_WHLR || mWType == PassCard::SIX_WHLR)) {
description = "Vehicles with 2 axles and 5 or 6 wheels excluding buses";
}
else if (mVType == PassCard::THREE_MORE_AXL) {
description = "Vehicles with 3 or more axles";
}
return description;
}
};
/*static data initialization*/
int PassCard::uid = 0;
int TollRate::uid = 0;
int TollTransaction::uid = 0;
//Callback function for binary search by pass number
int searchByPassIdB(SmartPointer<PassCard> value, long &passId) {
if (value->getPassNumber() < passId) return -1;
if (value->getPassNumber() > passId) return 1;
return 0;
}
//Callback function for sorting by pass number
int searchByPassIdS(SmartPointer<PassCard> valueA, SmartPointer<PassCard> valueB) {
if (valueA->getPassNumber() < valueB->getPassNumber()) return -1;
if (valueA->getPassNumber() > valueB->getPassNumber()) return 1;
return 0;
}
//Callback function for sorting by balance
int searchByBalanceS(SmartPointer<PassCard> valueA, SmartPointer<PassCard> valueB)
{
if (valueA->getRechargeAmount() < valueB->getRechargeAmount()) return -1;
if (valueA->getRechargeAmount() > valueB->getRechargeAmount()) return 1;
return 0;