-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequirements_and_code.cpp
More file actions
188 lines (142 loc) · 4.86 KB
/
Requirements_and_code.cpp
File metadata and controls
188 lines (142 loc) · 4.86 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
2. Functional Requirements
✅ Actors
Payer (User1) — initiates the payment
Payee (User2) — receives the payment
Payment Gateway — handles routing, validation, and coordination
Bank — processes debit/credit operations for users
✅ Key Flows
Payer selects payment type (UPI, card, etc.)
Payer selects another user (payee) and enters amount
PaymentGateway validates payer/payee and basic rules
PaymentGateway delegates debit to payer’s bank
PaymentGateway then delegates credit to payee’s bank
If any step fails, rollback and refund are triggered
Return result (success or failure) to user
Entity Diagram
User
├── id : string
├── name : string
├── bankAccount : BankAccount*
└── void initiatePayment(User* payee, double amount, PaymentGateway* gateway)
BankAccount
├── accountId : string
├── balance : double
├── Bank* bank
├── void debit(double amount)
└── void credit(double amount)
Bank
├── id : string
├── map<string, BankAccount*> accounts
├── bool processDebit(BankAccount*, double)
└── bool processCredit(BankAccount*, double)
PaymentGateway
├── bool validateUsers(User* payer, User* payee, double amount)
├── bool processPayment(User* payer, User* payee, double amount)
PaymentManager
├── PaymentGateway* gateway
└── void initiatePayment(User* payer, User* payee, double amount)
PaymentIntent
├── id : string
├── payerId : string
├── payeeId : string
├── amount : double
├── status : string ("PENDING", "SUCCESS", "FAILED")
Code (Basic Brute)
class BankAccount {
public:
string accountId;
double balance;
Bank* bank;
bool debit(double amount) {
if (balance < amount) return false;
balance -= amount;
return true;
}
void credit(double amount) {
balance += amount;
}
};
class Bank {
public:
string bankId;
map<string, BankAccount*> accounts;
bool processDebit(BankAccount* acc, double amount) {
cout << "[Bank] Processing debit from " << acc->accountId << endl;
return acc->debit(amount);
}
bool processCredit(BankAccount* acc, double amount) {
cout << "[Bank] Processing credit to " << acc->accountId << endl;
acc->credit(amount);
return true;
}
};
class User {
public:
string id;
string name;
BankAccount* account;
User(string id, string name, BankAccount* acc)
: id(id), name(name), account(acc) {}
void initiatePayment(User* payee, double amount, PaymentGateway* gateway) {
cout << "\n[" << name << "] Initiating payment of " << amount << " to " << payee->name << endl;
gateway->processPayment(this, payee, amount);
}
};
class PaymentGateway {
public:
bool validateUsers(User* payer, User* payee, double amount) {
if (!payer || !payee) {
cout << "[Gateway] Invalid users!" << endl;
return false;
}
if (amount <= 0) {
cout << "[Gateway] Invalid amount!" << endl;
return false;
}
return true;
}
bool processPayment(User* payer, User* payee, double amount) {
if (!validateUsers(payer, payee, amount)) return false;
Bank* payerBank = payer->account->bank;
Bank* payeeBank = payee->account->bank;
cout << "[Gateway] Requesting debit from payer bank..." << endl;
bool debitSuccess = payerBank->processDebit(payer->account, amount);
if (!debitSuccess) {
cout << "[Gateway] Payment failed: insufficient balance." << endl;
return false;
}
cout << "[Gateway] Requesting credit to payee bank..." << endl;
bool creditSuccess = payeeBank->processCredit(payee->account, amount);
if (!creditSuccess) {
// refund logic in simple terms
cout << "[Gateway] Credit failed! Refunding payer..." << endl;
payer->account->credit(amount);
return false;
}
cout << "[Gateway] Payment successful!" << endl;
return true;
}
};
class PaymentManager {
public:
PaymentGateway* gateway;
PaymentManager(PaymentGateway* g) : gateway(g) {}
void initiatePayment(User* payer, User* payee, double amount) {
cout << "\n[Manager] Initiating transaction request..." << endl;
gateway->processPayment(payer, payee, amount);
}
};
// === DEMO ===
int main() {
Bank bank1{"B1"}, bank2{"B2"};
BankAccount acc1{"A1", 500, &bank1};
BankAccount acc2{"A2", 200, &bank2};
bank1.accounts["A1"] = &acc1;
bank2.accounts["A2"] = &acc2;
User user1{"U1", "Alice", &acc1};
User user2{"U2", "Bob", &acc2};
PaymentGateway gateway;
PaymentManager manager(&gateway);
manager.initiatePayment(&user1, &user2, 150);
manager.initiatePayment(&user1, &user2, 400); // should fail due to balance
}