-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitwise.cpp
More file actions
553 lines (452 loc) · 15 KB
/
Copy pathsplitwise.cpp
File metadata and controls
553 lines (452 loc) · 15 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
#include<iostream>
//#include<iomanip>
#include<bits/stdc++.h>
#include<queue>
using namespace std;
struct Account {
int acc_no;
string name;
double balance;
// Constructor to initialize account details
Account(int acc, string n, double bal) : acc_no(acc), name(n), balance(bal) {}
// Display account details
void display() {
cout << "Account Number: " << acc_no << "\n";
cout << "Name: " << name << "\n";
cout << "Balance: $" << balance << "\n\n";
}
};
void create_account(queue<Account>& accounts) {
system("cls");
int accNo;
string name;
double initialBalance;
cout << "Enter Account Number: ";
cin >> accNo;
cout << "Enter Name: ";
cin >> name;
cout << "Enter Initial Balance: $";
cin >> initialBalance;
Account newAccount(accNo, name, initialBalance);
accounts.push(newAccount);
cout << "Account created successfully!\n";
}
void display_all_accounts(queue<Account> accounts) {
system("cls");
if (accounts.empty()) {
cout << "No accounts found!\n";
} else {
cout << "===== ACCOUNT DETAILS =====\n";
while (!accounts.empty()) {
Account currentAccount = accounts.front();
accounts.pop();
currentAccount.display();
}
}
}
void deposit(queue<Account>& accounts, int accNo, double amount) {
system("cls");
queue<Account> tempQueue;
bool found = false;
while (!accounts.empty()) {
Account currentAccount = accounts.front();
accounts.pop();
if (currentAccount.acc_no == accNo) {
currentAccount.balance += amount;
tempQueue.push(currentAccount);
found = true;
} else {
tempQueue.push(currentAccount);
}
}
accounts = tempQueue;
if (!found) {
cout << "Account not found!\n";
} else {
cout << "Amount deposited successfully!\n";
}
}
void withdraw(queue<Account>& accounts, int accNo, double amount) {
system("cls");
queue<Account> tempQueue;
bool found = false;
while (!accounts.empty()) {
Account currentAccount = accounts.front();
accounts.pop();
if (currentAccount.acc_no == accNo) {
if (currentAccount.balance >= amount) {
currentAccount.balance -= amount;
tempQueue.push(currentAccount);
found = true;
} else {
cout << "Insufficient balance!\n";
tempQueue.push(currentAccount);
}
} else {
tempQueue.push(currentAccount);
}
}
accounts = tempQueue;
if (!found) {
cout << "Account not found!\n";
} else {
cout << "Amount withdrawn successfully!\n";
}
}
void deleteAccount(queue<Account>& accounts, int accNo)
{
system("cls");
queue<Account> tempQueue;
// Search for the account to delete and enqueue other accounts to tempQueue
while (!accounts.empty())
{
Account currentAccount = accounts.front();
accounts.pop();
if (currentAccount.acc_no != accNo)
{
tempQueue.push(currentAccount);
}
else
{
// Display a message indicating that the account has been deleted
cout << "Account with Account Number " << accNo << " has been deleted.\n";
}
}
// Copy back the customers from tempQueue to customerQueue
while (!tempQueue.empty())
{
accounts.push(tempQueue.front());
tempQueue.pop();
}
}
void update_account(queue<Account>& accounts, int accNo) {
system("cls");
queue<Account> tempQueue;
bool found = false;
while (!accounts.empty()) {
Account currentAccount = accounts.front();
accounts.pop();
if (currentAccount.acc_no == accNo) {
// Assuming "update" means modifying the account name
string newName;
cout << "Enter new name for the account: ";
cin >> newName;
currentAccount.name = newName; // Updating account name
tempQueue.push(currentAccount);
found = true;
} else {
tempQueue.push(currentAccount);
}
}
accounts = tempQueue; // Restoring the modified queue
if (!found) {
cout << "Account not found!\n";
} else {
cout << "Account updated successfully!\n";
}
}
class bank
{
public:
string name;
int netAmount;
set<string> types;
};
int getMinIndex(bank listOfNetAmounts[],int numBanks)
{
int min=INT_MAX, minIndex=-1;
for(int i=0;i<numBanks;i++)
{
if(listOfNetAmounts[i].netAmount == 0)
{
continue;
}
if(listOfNetAmounts[i].netAmount < min)
{
minIndex = i;
min = listOfNetAmounts[i].netAmount;
}
}
return minIndex;
}
int getSimpleMaxIndex(bank listOfNetAmounts[],int numBanks)
{
int max=INT_MIN, maxIndex=-1;
for(int i=0;i<numBanks;i++)
{
if(listOfNetAmounts[i].netAmount == 0)
{
continue;
}
if(listOfNetAmounts[i].netAmount > max)
{
maxIndex = i;
max = listOfNetAmounts[i].netAmount;
}
}
return maxIndex;
}
pair<int,string> getMaxIndex(bank listOfNetAmounts[],int numBanks,int minIndex,bank input[],int maxNumTypes)
{
int max=INT_MIN;
int maxIndex=-1;
string matchingType;
for(int i=0;i<numBanks;i++)
{
if(listOfNetAmounts[i].netAmount == 0)
{
continue;
}
if(listOfNetAmounts[i].netAmount < 0)
{
continue;
}
vector<string> v(maxNumTypes);
vector<string>::iterator ls=set_intersection(listOfNetAmounts[minIndex].types.begin(),listOfNetAmounts[minIndex].types.end(), listOfNetAmounts[i].types.begin(),listOfNetAmounts[i].types.end(), v.begin());
if((ls-v.begin())!=0 && max<listOfNetAmounts[i].netAmount )
{
max=listOfNetAmounts[i].netAmount;
maxIndex=i;
matchingType = *(v.begin());
}
}
//if there is NO such max which has a common type with any remaining banks then maxIndex has -1
// also return the common payment type
return make_pair(maxIndex,matchingType);
}
void printAns(vector<vector<pair<int,string>>> ansGraph, int numBanks,bank input[]){
cout<<"\nThe transactions for minimum cash flow are as follows : \n\n";
for(int i=0;i<numBanks;i++)
{
for(int j=0;j<numBanks;j++)
{
if(i==j)
{
continue;
}
if(ansGraph[i][j].first != 0 && ansGraph[j][i].first != 0)
{
if(ansGraph[i][j].first == ansGraph[j][i].first)
{
ansGraph[i][j].first=0;
ansGraph[j][i].first=0;
}
else if(ansGraph[i][j].first > ansGraph[j][i].first)
{
ansGraph[i][j].first -= ansGraph[j][i].first;
ansGraph[j][i].first =0;
cout<<input[i].name<<" pays Rs" << ansGraph[i][j].first<< "to "<<input[j].name<<" via "<<ansGraph[i][j].second<<endl;
}
else
{
ansGraph[j][i].first -= ansGraph[i][j].first;
ansGraph[i][j].first = 0;
cout<<input[j].name<<" pays Rs "<< ansGraph[j][i].first<<" to "<<input[i].name<<" via "<<ansGraph[j][i].second<<endl;
}
}
else if(ansGraph[i][j].first != 0)
{
cout<<input[i].name<<" pays Rs "<<ansGraph[i][j].first<<" to "<<input[j].name<<" via "<<ansGraph[i][j].second<<endl;
}
else if(ansGraph[j][i].first != 0)
{
cout<<input[j].name<<" pays Rs "<<ansGraph[j][i].first<<" to "<<input[i].name<<" via "<<ansGraph[j][i].second<<endl;
}
ansGraph[i][j].first = 0;
ansGraph[j][i].first = 0;
}
}
cout<<"\n";
}
void minimizeCashFlow(int numBanks,bank input[],unordered_map<string,int>& indexOf,int numTransactions,vector<vector<int>>& graph,int maxNumTypes)
{
//Find net amount of each bank has
bank listOfNetAmounts[numBanks];
for(int b=0;b<numBanks;b++)
{
listOfNetAmounts[b].name = input[b].name;
listOfNetAmounts[b].types = input[b].types;
int amount = 0;
//incoming edges
//column traversals
for(int i=0;i<numBanks;i++)
{
amount += (graph[i][b]);
}
//outgoing edges
//row traverse
for(int j=0;j<numBanks;j++)
{
amount += ((-1) * graph[b][j]);
}
listOfNetAmounts[b].netAmount = amount;
}
vector<vector<pair<int,string>>> ansGraph(numBanks,vector<pair<int,string>>(numBanks,{0,""}));//adjacency matrix
//find min and max net amount
int numZeroNetAmounts=0;
for(int i=0;i<numBanks;i++)
{
if(listOfNetAmounts[i].netAmount == 0)
{
numZeroNetAmounts++;
}
}
while(numZeroNetAmounts!=numBanks)
{
int minIndex=getMinIndex(listOfNetAmounts, numBanks);
pair<int,string> maxAns = getMaxIndex(listOfNetAmounts, numBanks, minIndex,input,maxNumTypes);
int maxIndex = maxAns.first;
if(maxIndex == -1)
{
(ansGraph[minIndex][0].first) += abs(listOfNetAmounts[minIndex].netAmount);
(ansGraph[minIndex][0].second) = *(input[minIndex].types.begin());
int simpleMaxIndex = getSimpleMaxIndex(listOfNetAmounts, numBanks);
(ansGraph[0][simpleMaxIndex].first) += abs(listOfNetAmounts[minIndex].netAmount);
(ansGraph[0][simpleMaxIndex].second) = *(input[simpleMaxIndex].types.begin());
listOfNetAmounts[simpleMaxIndex].netAmount += listOfNetAmounts[minIndex].netAmount;
listOfNetAmounts[minIndex].netAmount = 0;
if(listOfNetAmounts[minIndex].netAmount == 0) numZeroNetAmounts++;
if(listOfNetAmounts[simpleMaxIndex].netAmount == 0) numZeroNetAmounts++;
}
else
{
int transactionAmount = min(abs(listOfNetAmounts[minIndex].netAmount), listOfNetAmounts[maxIndex].netAmount);
(ansGraph[minIndex][maxIndex].first) += (transactionAmount);
(ansGraph[minIndex][maxIndex].second) = maxAns.second;
listOfNetAmounts[minIndex].netAmount += transactionAmount;
listOfNetAmounts[maxIndex].netAmount -= transactionAmount;
if(listOfNetAmounts[minIndex].netAmount == 0)
{
numZeroNetAmounts++;
}
if(listOfNetAmounts[maxIndex].netAmount == 0)
{
numZeroNetAmounts++;
}
}
}
printAns(ansGraph,numBanks,input);
// cout<<"HI\n";
}
void cashflow()
{
system("cls");
cout<<"\n\t\t\t\t********************* Welcome to CASH FLOW MINIMIZER SYSTEM ***********************\n\n\n";
cout<<"This system minimizes the number of transactions among multiple banks in the different corners of the world that use different modes of payment.There is one world bank (with all payment modes) to act as an intermediary between banks that have no common mode of payment. \n\n";
cout<<"Enter the number of banks participating in the transactions.\n";
int numBanks;cin>>numBanks;
bank input[numBanks];
unordered_map<string,int> indexOf;//stores index of a bank
cout<<"Enter the details of the banks and transactions as stated:\n";
cout<<"Bank name ,number of payment modes it has and the payment modes.\n";
cout<<"Bank name and payment modes should not contain spaces\n";
int maxNumTypes;
for(int i=0; i<numBanks;i++)
{
if(i==0)
{
cout<<"World Bank : ";
}
else
{
cout<<"Bank "<<i<<" : ";
}
cin>>input[i].name;
indexOf[input[i].name] = i;
int numTypes;
cin>>numTypes;
if(i==0)
{
maxNumTypes = numTypes;
}
string type;
while(numTypes--)
{
cin>>type;
input[i].types.insert(type);
}
}
cout<<"Enter number of transactions.\n";
int numTransactions;
cin>>numTransactions;
vector<vector<int>> graph(numBanks,vector<int>(numBanks,0));//adjacency matrix
cout<<"Enter the details of each transaction as stated:";
cout<<"Debtor Bank , creditor Bank and amount\n";
cout<<"The transactions can be in any order\n";
for(int i=0;i<numTransactions;i++)
{
cout<<(i)<<" th transaction : ";
string s1,s2;
int amount;
cin >> s1>>s2>>amount;
graph[indexOf[s1]][indexOf[s2]] = amount;
}
//settle
minimizeCashFlow(numBanks,input,indexOf,numTransactions,graph,maxNumTypes);
}
int main() {
queue<Account> accountQueue;
int choice;
do {
system("cls");
cout << "\n===== BANKING MANAGEMENT SYSTEM =====\n";
cout << "1. Create Account\n";
cout << "2. Display All Accounts\n";
cout << "3. Deposit\n";
cout << "4. Withdraw\n";
cout << "5. cashflow\n";
cout << "6. Delete\n";
cout<<"7 update\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
system("cls");
switch (choice) {
case 1:
create_account(accountQueue);
break;
case 2:
display_all_accounts(accountQueue);
break;
case 3:
int depositAccNo;
double depositAmt;
cout << "Enter Account Number to Deposit: ";
cin >> depositAccNo;
cout << "Enter Amount to Deposit: $";
cin >> depositAmt;
deposit(accountQueue, depositAccNo, depositAmt);
break;
case 4:
int withdrawAccNo;
double withdrawAmt;
cout << "Enter Account Number to Withdraw: ";
cin >> withdrawAccNo;
cout << "Enter Amount to Withdraw: $";
cin >> withdrawAmt;
withdraw(accountQueue, withdrawAccNo, withdrawAmt);
break;
case 5:
cashflow();
case 6:
int account_no;
cout<<"Enter the account number to be deleted : ";
cin>>account_no;
deleteAccount(accountQueue,account_no);
case 7:
int acco_no;
cout<<"Enter the account no to be updated :";
cin>>acco_no;
update_account(accountQueue,acco_no);
case 8:
cout << "Exiting the system.\n";
break;
default:
cout << "Invalid choice! Please try again.\n";
break;
}
cin.ignore();
cin.get();
} while (choice != 8);
return 0;
}