-
Notifications
You must be signed in to change notification settings - Fork 0
/
Online E-commerce Shop Project.cpp
1365 lines (1095 loc) · 37.2 KB
/
Online E-commerce Shop Project.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
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
#include <iostream>
#include <conio.h>
using namespace std;
#define V 6
int INF = 1e9 + 10;
string addressF;
class Shop;
class TakeAwayOrders {
public:
double bill;
int productID; //the number to access product
string name;
string product;
int quantity;
int height;
int orderId; //to maintain order in tree
Shop* st;
TakeAwayOrders* left;
TakeAwayOrders* next;
TakeAwayOrders* right;
TakeAwayOrders();
TakeAwayOrders(string n, int q, int ID, Shop* s);
void billing();
};
class HomeDeliveryNode {
public:
double bill;
int productID; //the number to access product
string name;
string product;
int quantity;
int height;
int orderId; //searching
Shop* st;
HomeDeliveryNode();
HomeDeliveryNode(string n, int q, int ID, Shop* s);
};
class DeliveryOrders { // home delivery
public:
HomeDeliveryNode delivery;
string address;
double deliveryCharges;
int deliveryDistance;
int priority;
DeliveryOrders();
DeliveryOrders(string name, int quantity, int productID, Shop* s, string add, double charges, int distance, int bill, int p);
};
class HomeDeliveryHeap { // min heap for home delivery
public:
int count;
int size;
DeliveryOrders** deliveries;
HomeDeliveryHeap() {
deliveries = new DeliveryOrders * [20]{ NULL };
count = 0;
size = 20;
}
void insert(DeliveryOrders*& d) { //send address
if (count == size)
cout << "Delivery limit reached. Please try again later." << endl;
else {
count++;
deliveries[count - 1] = d;
heapify_up(count - 1);
}
}
DeliveryOrders* remove() {
DeliveryOrders* temp = deliveries[0];
deliveries[0] = deliveries[count - 1];
deliveries[count - 1] = NULL;
count--;
heapify_down(0);
return temp;
}
void heapify_up(int i) {
int parent = (i - 1) / 2;
if (parent >= 0) {
if (deliveries[i]->priority < deliveries[parent]->priority) {
DeliveryOrders* temp = deliveries[i];
deliveries[i] = deliveries[parent];
deliveries[parent] = temp;
heapify_up(parent);
}
}
}
void heapify_down(int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < count && deliveries[left] != NULL && deliveries[left]->priority < deliveries[smallest]->priority) {
smallest = left;
}
if (right < count && deliveries[right] != NULL && deliveries[right]->priority < deliveries[smallest]->priority) {
smallest = right;
}
if (smallest != i) {
DeliveryOrders* temp = deliveries[i];
deliveries[i] = deliveries[smallest];
deliveries[smallest] = temp;
heapify_down(smallest);
}
}
};
class HashTable {
public:
DeliveryOrders** Hasharray;
int size;
bool checkforEmpty = false;
HashTable() {
this->size = 20;
Hasharray = new DeliveryOrders * [size];
for (int i = 0; i < size; i++) {
Hasharray[i] = NULL;
}
}
void insert(DeliveryOrders* value) {
int Hashkey = hashFunction(value->delivery.orderId);
if (Hasharray[Hashkey] == NULL) {
Hasharray[Hashkey] = value;
checkforEmpty = true;
}
}
void search(int orderID) {
int hashkey = hashFunction(orderID);
if (Hasharray[hashkey] != NULL && Hasharray[hashkey]->delivery.orderId == orderID) {
cout << "********* Your order is yet to be delivered **********" << endl << endl;
cout << "\n***************Order Details***************";
cout << "\n Customer Name : " << Hasharray[hashkey]->delivery.name << "\n Order name : " << Hasharray[hashkey]->delivery.product << "\n Order ID : " << Hasharray[hashkey]->delivery.orderId << "\n Quantity : " << Hasharray[hashkey]->delivery.quantity << "\n Total bill : " << Hasharray[hashkey]->delivery.bill;
cout << "\n Address : " << Hasharray[hashkey]->address << "\n Distance in km : " << Hasharray[hashkey]->deliveryDistance << "\n Delivery charges : " << Hasharray[hashkey]->deliveryCharges << endl;
}
else {
cout << "******* No such order found! *******" << endl << endl;
}
}
bool isEmpty() {
if (checkforEmpty == true) {
return false;
}
else
return true;
}
void delete_val(int val) {
int hashkey = hashFunction(val);
if (Hasharray[hashkey] != NULL && Hasharray[hashkey]->delivery.orderId == val) {
Hasharray[hashkey] = NULL;
}
}
int hashFunction(int val) {
return val % size;
}
};
class Shop {
public:
string name;
string* product;
int* price;
string address;
HomeDeliveryHeap homedeliveryqueue; // min heap for home deliveries
HashTable DeliveryOrderSearching;
Shop(string name, string* product, int* price, string address) {
this->name = name;
this->product = product;
this->price = price;
this->address = address;
}
void display_products() {
cout << "\n------------------------------------------------------------------------";
cout << "\n | ITEM NO. | ITEM LIST |ORIGINAL PRICE|";
cout << "\n------------------------------------------------------------------------";
cout << "\n | 1 |\t " << product[0] << " | " << price[0] << " |";
cout << "\n | 2 |\t " << product[1] << " | " << price[1] << " |";
cout << "\n | 3 |\t " << product[2] << " | " << price[2] << " |";
cout << "\n | 4 |\t " << product[3] << " | " << price[3] << " |";
cout << "\n | 5 |\t " << product[4] << " | " << price[4] << " |";
cout << "\n | 6 |\t " << product[5] << " | " << price[5] << " |";
cout << "\n | 7 |\t " << product[6] << " | " << price[6] << " |";
cout << "\n | 8 |\t " << product[7] << " | " << price[7] << " |";
cout << "\n | 9 |\t " << product[8] << " | " << price[8] << " |";
cout << "\n | 10 |\t " << product[9] << " | " << price[9] << " |";
cout << "\n-------------------------------------------------------------------------" << endl;
}
};
TakeAwayOrders::TakeAwayOrders() {}
TakeAwayOrders::TakeAwayOrders(string n, int q, int ID, Shop* s) { //send address of shop object
static int id = 1;
name = n;
product = s->product[ID];
quantity = q;
productID = ID;
orderId = id++;
st = s;
right = NULL;
left = NULL;
next = NULL;
}
void TakeAwayOrders::billing() {
bill = quantity * (st->price[productID]);
}
HomeDeliveryNode::HomeDeliveryNode() {}
HomeDeliveryNode::HomeDeliveryNode(string n, int q, int ID, Shop* s) { //send address of shop object
static int id = 1;
name = n;
product = s->product[ID];
quantity = q;
productID = ID;
orderId = id++;
st = s;
}
DeliveryOrders::DeliveryOrders() {}
DeliveryOrders::DeliveryOrders(string name, int quantity, int productID, Shop* s, string add, double charges, int distance, int bill, int p) {
HomeDeliveryNode o(name, quantity, productID, s);
delivery = o;
address = add;
deliveryCharges = charges;
deliveryDistance = distance;
priority = p;
delivery.bill = bill;
}
DeliveryOrders* deliveryCustomer;
void placeOrderHomeDeliveryCustomer(string name, int quantity, int productID, Shop* s, string address, int deliveryCharges, int distanceDelivery, int bill)
{
static int priority = 0;
priority++;
char option;
cout << "Would you like to place an urgent order? Press [Y] for urgent, else press [N]: ";
cin >> option;
while (option != 'Y' && option != 'N') {
cout << "Invalid option entered. Enter a valid option: ";
cin >> option;
}
if (option == 'Y') {
cout << endl << "***** Extra Charges for urgent delivery: Rs. 500 *****" << endl;
bill += 500;
deliveryCharges += 500;
deliveryCustomer = new DeliveryOrders(name, quantity, productID, s, address, deliveryCharges, distanceDelivery, bill, 0);
}
else {
deliveryCustomer = new DeliveryOrders(name, quantity, productID, s, address, deliveryCharges, distanceDelivery, bill, priority);
}
s->homedeliveryqueue.insert(deliveryCustomer);
s->DeliveryOrderSearching.insert(deliveryCustomer);
cout << "\n------------------------------------------------------\n";
cout << "\n***************Order Details***************";
cout << "\n------------------------------------------------------\n";
cout << "\n Customer Name : " << name << "\n Order name : " << s->product[productID] << "\n Quantity : " << quantity << "\n Total bill : " << deliveryCustomer->delivery.bill;
cout << "\n Address : " << address << "\n Distance in km : " << distanceDelivery << "\n Delivery charges : " << deliveryCharges << endl;
}
void StartDeliveries(Shop* s) { // start delivery
if (s->homedeliveryqueue.deliveries[0] == NULL) {
cout << "No home delivery orders placed yet!" << endl;
}
else {
int counter = s->homedeliveryqueue.count;
for (int i = 0; i < counter; i++) {
DeliveryOrders* p = s->homedeliveryqueue.remove();
s->DeliveryOrderSearching.delete_val(p->delivery.orderId);
cout << "\n------------------------------------------------------";
cout << "\n*************** Delivery Complete! ***************";
cout << "\n------------------------------------------------------\n";
cout << "\n Customer Name : " << p->delivery.name << "\n Order name : " << p->delivery.product << "\n Order ID : " << p->delivery.orderId << "\n Quantity : " << p->delivery.quantity << "\n Total bill : " << p->delivery.bill;
cout << "\n Address : " << p->address << "\n Distance in km : " << p->deliveryDistance << "\n Delivery charges : " << p->deliveryCharges << endl;
cout << "------------------------------------------------------\n";
}
s->DeliveryOrderSearching.checkforEmpty = false; // when check for empty flag is true , means orders otherwise no orders
}
}
void displayAllOrdersHomeDeliveryCustomers(Shop* s)
{
if (s->homedeliveryqueue.count == 0)
{
cout << "There is no Order for Home Delivery Customer till yet" << endl;
}
else {
int n = s->homedeliveryqueue.count;
for (int i = 0; i < n; i++) {
cout << "\n------------------------------------------------------\n";
cout << "-----------------------------------------------------" << endl;
cout << "Home Delivery Customer : " << s->homedeliveryqueue.deliveries[i]->delivery.name << endl;
cout << "Product Name : " << s->homedeliveryqueue.deliveries[i]->delivery.product << endl;
cout << "Quantity : " << s->homedeliveryqueue.deliveries[i]->delivery.quantity << endl;
cout << "Delivery Distance : " << s->homedeliveryqueue.deliveries[i]->deliveryDistance << "KM" << endl;
cout << "Delivery Charges : " << s->homedeliveryqueue.deliveries[i]->deliveryCharges << endl;
cout << "Bill : " << s->homedeliveryqueue.deliveries[i]->delivery.bill << " RS/_" << endl;
cout << "Delivery Address : " << s->homedeliveryqueue.deliveries[i]->address << endl;
//cout << "Priority: " << s->homedeliveryqueue.deliveries[i]->priority << endl;
cout << "-----------------------------------------------------" << endl;
cout << "\n------------------------------------------------------\n";
}
}
}
class AVLTreeforOrders {
public:
TakeAwayOrders* root;
AVLTreeforOrders() {
root = NULL;
}
AVLTreeforOrders(TakeAwayOrders* n) {
root = n;
}
int max(int a, int b) {
if (a > b)return a;
else return b;
}
int height(TakeAwayOrders* root) {
if (root == NULL)
return -1;
else {
int leftH = height(root->left);
int rightH = height(root->right);
return (1 + max(leftH, rightH));
}
}
TakeAwayOrders* rightRotate(TakeAwayOrders* y)
{
TakeAwayOrders* x = y->left;
TakeAwayOrders* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left),
height(y->right)) + 1;
x->height = max(height(x->left),
height(x->right)) + 1;
// Return new root
return x;
}
TakeAwayOrders* leftRotate(TakeAwayOrders* x)
{
TakeAwayOrders* y = x->right;
TakeAwayOrders* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left),
height(x->right)) + 1;
y->height = max(height(y->left),
height(y->right)) + 1;
// Return new root
return y;
}
// Get Balance factor of TakeAwayOrders N
int getBalance(TakeAwayOrders* N)
{
if (N == NULL)
return 0;
return height(N->left) -
height(N->right);
}
TakeAwayOrders* search(TakeAwayOrders*& root, int id)
{
if (root == NULL)
{
return NULL;
}
else if (root->orderId == id)
{
return root;
}
else if (root->orderId < id)
{
return search(root->right, id);
}
else
{
return search(root->left, id);
}
}
TakeAwayOrders* insert(TakeAwayOrders*& root, TakeAwayOrders* newNode)
{
if (root == NULL)
return newNode;
if (newNode->orderId < root->orderId)
root->left = insert(root->left, newNode);
else if (newNode->orderId > root->orderId)
root->right = insert(root->right, newNode);
else
return root;
root->height = 1 + max(height(root->left),
height(root->right));
int balance = getBalance(root);
// Left Left Case
if (balance > 1 && newNode->orderId < root->left->orderId)
return rightRotate(root);
// Right Right Case
if (balance < -1 && newNode->orderId > root->right->orderId)
return leftRotate(root);
// Left Right Case
if (balance > 1 && newNode->orderId > root->left->orderId)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
// Right Left Case
if (balance < -1 && newNode->orderId < root->right->orderId)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
// predecessor
TakeAwayOrders* minValueOrders(TakeAwayOrders* Order)
{
TakeAwayOrders* current = Order;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}
TakeAwayOrders* deleteOrders(TakeAwayOrders* root, int orderId)
{
if (root == NULL)
return root;
if (orderId < root->orderId)
root->left = deleteOrders(root->left, orderId);
else if (orderId > root->orderId)
root->right = deleteOrders(root->right, orderId);
else
{
// Orders with only one child or no child
if ((root->left == NULL) ||
(root->right == NULL))
{
TakeAwayOrders* temp = root->left ?root->left :root->right;
// No child case
if (temp == NULL)
{
temp = root;
root = NULL;
}
else // One child case
*root = *temp;
delete(temp);
}
else
{
TakeAwayOrders* temp = minValueOrders(root->right);
root->orderId = temp->orderId;
root->name = temp->name;
root->bill = temp->bill;
root->product = temp->product;
root->productID = temp->productID;
root->quantity = temp->quantity;
// Delete the inorder successor
root->right = deleteOrders(root->right,temp->orderId);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left), height(root->right));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->left) < 0)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
void display(TakeAwayOrders*& root)
{
cout << "\n----------------------------------" << endl;
cout << "Name :" << root->name << endl;
cout << "Product :" << root->product << endl;
cout << "Quantity : " << root->quantity << endl;
cout << "Bill : " << root->bill << endl;
cout << "Order ID: " << root->orderId << endl;
cout << "-----------------------------------\n" << endl;
}
void preOrder(TakeAwayOrders*& root)
{
if (this->root == NULL) {
cout << "********** No Takeaway Orders currently **********" << endl;
}
else
{
display(root);
if (root->left)
preOrder(root->left);
if (root->right)
preOrder(root->right);
}
}
};
int DeliveryChoice = -1;
int DeliveryChoice1 = -1;
int* distanceP = new int[6];
int* dist = new int[6]; // for cities dijkstra
string CityName[] = { "Karachi", "Islamabad", "Lahore", "Multan", "Faisalabad", "Abbotabad" };
string Multan[] = { "Multan", "Fort Kohna Qasim Garden", "eid Ghah", "Garrison Public Library", "Bosan Town", "Sher Shah Town" };
string Abbotabad[] = { "Abbotabad", "Jinnahabad", "PMA", "Mandian", "Ayub Medical College", "Fawara Chowk" };
string Islamabad[] = { "Islamabad", "I-8" ,"Askari-14", "F-10", "F-7", "H-12" };
string Lahore[] = { "Lahore", "Johar Town", "Garden Town", "DHA", "Model Town", "Gulberg" };
string Karachi[] = { "karachi", "Bahria Town", "North Nazimabad", "Defence", "Clifton", "Liyari" };
string Faisalabad[] = { "Faisalabad", "Citi Housing Society", "Satiana Road" , "Wapda City" , "FDA City" , "Canal Road" };
int graph[V][V] = { {0, 4, 0, 0, 6, 1}, //Karachi
{4, 0, 3, 8, 0, 0}, //Islamabad
{0, 3, 0, 2, 9, 4}, //Lahore
{0, 8, 2, 0, 1, 4}, //multan
{6, 0, 9, 1, 0, 8}, //Faisalabad
{1, 6, 0, 4, 8, 0} }; //Abbotabad
//----------------------Abbotabad--------------------------------
int AtdGraph[V][V] = { { 0, 4, 4, 0, 0, 0 },
{ 4, 0, 2, 0, 0, 0 }, //Jinnahabad
{ 4, 2, 0, 3, 2, 4 }, //PMA
{ 0, 0, 3, 0, 0, 3 }, //Mandian
{ 0, 0, 2, 0, 0, 3 }, //Ayub
{ 0, 0, 4, 3, 3, 0 } }; //Fawara
//---------------------Islamabad------------------------------------
int IsbGraph[V][V] = { { 0, 4, 0, 2, 0, 0 },
{ 4, 0, 3, 1, 0, 6 }, //I-8
{ 0, 3, 0, 4, 0, 7 }, //Askari
{ 2, 1, 4, 0, 9, 0 }, //F-10
{ 0, 0, 0, 9, 0, 5 }, //F-7
{ 0, 6, 7, 0, 5, 0 } }; //H-12
//-----------------------Lahore---------------------------------------
int LhrGraph[V][V] = { { 0, 9, 15, 6, 0, 0 },
{ 9, 0, 7, 0, 0, 8 },
{ 15, 7, 0, 8, 9, 5 },
{ 6, 0, 8, 0, 11, 0 },
{ 0, 0, 9, 11, 0, 4 },
{ 0, 8, 5, 0, 4, 0 } };
//----------------------Karachi----------------------------------------
int KarGraph[V][V] = { { 0, 6, 3, 0, 0, 1 },
{ 6, 0, 0, 2, 0, 5 }, //Bahria Town
{ 3, 0, 0, 0, 3, 5 }, //North Nazimabad
{ 0, 2, 0, 0, 6, 4 }, //Defence
{ 0, 0, 3, 6, 0, 6 }, //Clifton
{ 1, 5, 5, 4, 6, 0 } }; //Liyari
//----------------------------------------------------------------------------------
//----------------------Faisalabad----------------------------------------
int FsdGraph[V][V] = { { 0, 2, 3, 0, 7, 1 },
{ 2, 0, 4, 2, 0, 8 }, //Citi Housing Society
{ 3, 4, 0, 5, 3, 0 }, //Satiana Road
{ 0, 2, 5, 0, 6, 5 }, //Wapda City
{ 7, 0, 3, 6, 0, 6 }, //FDA City
{ 1, 8, 0, 5, 6, 0 } }; //Canal Road
//----------------------------------------------------------------------------------
//----------------------Multan----------------------------------------
int MulGraph[V][V] = { { 0, 0, 1, 5, 3, 1 },
{ 0, 0, 2, 7, 0, 8 }, //Fort Kohna Qasim Garden
{ 1, 2, 0, 5, 5, 0 }, //Satiana Road
{ 5, 7, 5, 0, 6, 1 }, //Wapda City
{ 3, 0, 5, 6, 0, 3 }, //FDA City
{ 1, 8, 0, 1, 3, 0 } }; //Canal Road
class Edge {
public:
int data;
int weight;
bool visited;
Edge() : data(0), weight(0), visited(false) {}
Edge(int data, int weight) : data(data), weight(weight), visited(false) {}
};
class MinHeapforDijkstra {
public:
int count;
int size;
Edge** edges;
MinHeapforDijkstra(int maxSize) : count(0), size(maxSize) {
edges = new Edge*[size];
for (int i = 0; i < size; ++i) {
edges[i] = NULL;
}
}
void insert(int data, int weight) {
edges[count] = new Edge(data, weight);
count++;
heapify_up(count - 1);
}
int remove() {
if (count <= 0) {
cout << "Heap is empty." << endl;
return -1;
}
Edge* temp = edges[0];
edges[0] = edges[count - 1];
edges[count - 1] = NULL;
count--;
heapify_down(0);
int data = temp->data;
delete temp;
return data;
}
void heapify_up(int i) {
int parent = (i - 1) / 2;
if (parent >= 0 && edges[i] != NULL && edges[parent] != NULL &&
edges[i]->weight < edges[parent]->weight) {
swap(edges[i], edges[parent]);
heapify_up(parent);
}
}
void heapify_down(int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < count && edges[left] != NULL &&
edges[left]->weight < edges[smallest]->weight) {
smallest = left;
}
if (right < count && edges[right] != NULL &&
edges[right]->weight < edges[smallest]->weight) {
smallest = right;
}
if (smallest != i) {
swap(edges[i], edges[smallest]);
heapify_down(smallest);
}
}
bool isEmpty() {
return count == 0;
}
~MinHeapforDijkstra() {
for (int i = 0; i < count; ++i) {
delete edges[i];
}
delete[] edges;
}
};
int* dijkstra(int start, int graph[][V], string g[])
{
bool visited[V] = { false };
int* distance = new int[6];
for (int i = 0; i < V; ++i) {
distance[i] = INF; // Set initial distances to infinity
}
distance[start - 1] = 0; // Distance to the start vertex is 0
MinHeapforDijkstra heap(V);
heap.insert(start - 1, 0);
while (!heap.isEmpty())
{
int u = heap.remove();
visited[u] = true;
for (int v = 0; v < V; ++v)
{
if (graph[u][v] != 0 && !visited[v])
{
int alt = distance[u] + graph[u][v];
if (alt < distance[v])
{
distance[v] = alt;
heap.insert(v, alt);
}
}
}
}
// Output the calculated distances
cout << "Shortest distances from " << g[0] << ":\n";
for (int i = 1; i < V; ++i)
{
cout << g[i] << " : " << distance[i] << "km " << endl;
}
return distance;
}
int city() {
int opt;
cout << "\nThe delivery is available for following Areas : \n" << endl;
cout << "\n --------------------------------";
cout << "\n |CITY CODE | City |";
cout << "\n --------------------------------";
cout << "\n | 1 | Karachi |" << endl;
cout << "\n | 2 | Islamabad |" << endl;
cout << "\n | 3 | Lahore |" << endl;
cout << "\n | 4 | Multan |" << endl;
cout << "\n | 5 | Faisalabad |" << endl;
cout << "\n | 6 | Abbotabad |" << endl;
cout << "---------------------------------\n" << endl;
dist = dijkstra(1, graph, CityName);
cout << "Enter your option :" << endl;
cin >> opt;
return opt;
}
//string Multan[] = { "Multan", "Fort Kohna Qasim Garden", "eid Ghah", "Garrison Public Library", "Bosan Town", "Sher Shah Town" };
int Mul() {
int opt;
cout << "\nThe delivery is available for following Areas in Multan: " << endl;
cout << "\n---------------------------------------------";
cout << "\n |CITY CODE | AREA |";
cout << "\n---------------------------------------------";
cout << "\n | 1 | Fort Kohna Qasim Garden |" << endl;
cout << "\n | 2 | Eid Ghah |" << endl;
cout << "\n | 3 | Garrison Public Library |" << endl;
cout << "\n | 4 | Bosan Town |" << endl;
cout << "\n | 5 | Sher Shah Town |" << endl;
cout << "-----------------------------------------------\n\n" << endl;
distanceP = dijkstra(1, MulGraph, Multan);
cout << "\nEnter your option :" << endl;
cin >> opt;
return opt;
}
int isb() {
int opt;
cout << "\nThe delivery is available for following Areas in Islamabad: " << endl;
cout << "\n---------------------------------";
cout << "\n |CITY CODE | AREA |";
cout << "\n---------------------------------";
cout << "\n | 1 | I-8 |" << endl;
cout << "\n | 2 | Askari-14 |" << endl;
cout << "\n | 3 | F-10 |" << endl;
cout << "\n | 4 | F-7 |" << endl;
cout << "\n | 5 | H-12 |" << endl;
cout << "---------------------------------\n\n" << endl;
distanceP = dijkstra(1, IsbGraph, Islamabad);
cout << "\nEnter your option :" << endl;
cin >> opt;
return opt;
}
int Atd() {
int opt;
cout << "\nThe delivery is available for following Areas in Abbotabad: " << endl;
cout << "\n---------------------------------";
cout << "\n |CITY CODE | AREA |";
cout << "\n---------------------------------";
cout << "\n | 1 | Jinnahabad |" << endl;
cout << "\n | 2 | PMA |" << endl;
cout << "\n | 3 | Mandian |" << endl;
cout << "\n | 4 | Ayub Medical |" << endl;
cout << "\n | 5 | Fawara Chowk |" << endl;
cout << "---------------------------------\n\n" << endl;
distanceP = dijkstra(1, AtdGraph, Abbotabad);
cout << "\nEnter your option :" << endl;
cin >> opt;
return opt;
}
int Lhr() {
int opt;
cout << "\nThe delivery is available for following Areas in Lahore: " << endl;
cout << "\n---------------------------------";
cout << "\n |CITY CODE | AREA |";
cout << "\n---------------------------------";
cout << "\n | 1 | Johar Town |" << endl;
cout << "\n | 2 | Garden Town |" << endl;
cout << "\n | 3 | DHA |" << endl;
cout << "\n | 4 | Model Town |" << endl;
cout << "\n | 5 | Gulberg |" << endl;
cout << "---------------------------------\n\n" << endl;
distanceP = dijkstra(1, LhrGraph, Lahore);
cout << "\nEnter your option :" << endl;
cin >> opt;
return opt;
}
int Kar() {
int opt;
cout << "\nThe delivery is available for following Areas in Karachi: " << endl;
cout << "\n---------------------------------";
cout << "\n |CITY CODE | AREA |";
cout << "\n---------------------------------";
cout << "\n | 1 | Bahria Town |" << endl;
cout << "\n | 2 | North Nazimabad|" << endl;
cout << "\n | 3 | Defence |" << endl;
cout << "\n | 4 | Clifton |" << endl;
cout << "\n | 5 | Liyari |" << endl;
cout << "---------------------------------\n\n" << endl;
distanceP = dijkstra(1, KarGraph, Karachi);
cout << "\nEnter your option :" << endl;
cin >> opt;
return opt;
}
int fslabd() {
int opt;
cout << "\nThe delivery is available for following Areas in Faisalabad: " << endl;
cout << "\n----------------------------------------";
cout << "\n |CITY CODE | AREA |";
cout << "\n----------------------------------------";
cout << "\n | 1 | Citi Housing Society |" << endl;
cout << "\n | 2 | Satiana Road |" << endl;
cout << "\n | 3 | Wapda City |" << endl;
cout << "\n | 4 | FDA City |" << endl;
cout << "\n | 5 | Canal Road |" << endl;
cout << "-------------------------------------------\n\n" << endl;
distanceP = dijkstra(1, FsdGraph, Faisalabad);
cout << "\nEnter your option :" << endl;
cin >> opt;
return opt;
}
int FurthurArea(int option) {
int opt;
switch (option) {
//distanceP[6] = { 0 };
case 0:
{
opt = Kar();
addressF = Karachi[opt];
break;
}
case 1:
{
opt = isb();
addressF = Islamabad[opt];