-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeAlpha-Task-4.cpp
More file actions
206 lines (163 loc) · 5.85 KB
/
Copy pathCodeAlpha-Task-4.cpp
File metadata and controls
206 lines (163 loc) · 5.85 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
#include <iostream>
#include <string>
using namespace std;
class Transaction {
string type;
double amount;
string description;
public:
Transaction() {
type = "";
amount = 0;
description = "";
}
Transaction(string type, double amount, string description) {
this->type = type;
this->amount = amount;
this->description = description;
}
void display() {
cout << " Type : " << type << endl;
cout << " Amount : Rs. " << amount << endl;
cout << " Description : " << description << endl;
cout << " ----------------------------" << endl;
}
};
class Account {
int accountNumber;
double balance;
Transaction history[100];
int transactionCount;
void recordTransaction(string type, double amount, string desc) {
if (transactionCount < 100) {
history[transactionCount] = Transaction(type, amount, desc);
transactionCount++;
}
else {
cout << " [!] Transaction history full." << endl;
}
}
public:
Account(int accountNumber, double openingBalance) {
this->accountNumber = accountNumber;
this->balance = openingBalance;
this->transactionCount = 0;
recordTransaction("deposit", openingBalance, "Account opened with Rs. " + to_string(openingBalance));
}
void deposit(double amount) {
if (amount <= 0) {
cout << " [!] Deposit amount must be greater than 0." << endl;
return;
}
balance += amount;
recordTransaction("deposit", amount, "Deposited Rs. " + to_string(amount));
cout << " [✓] Rs. " << amount << " deposited successfully." << endl;
}
void withdraw(double amount) {
if (amount <= 0) {
cout << " [!] Withdrawal amount must be greater than 0." << endl;
return;
}
if (amount > balance) {
cout << " [!] Insufficient balance. Current balance: Rs. " << balance << endl;
return;
}
balance -= amount;
recordTransaction("withdrawal", amount, "Withdrew Rs. " + to_string(amount));
cout << " [✓] Rs. " << amount << " withdrawn successfully." << endl;
}
void transfer(double amount, Account& targetAccount) {
if (amount <= 0) {
cout << " [!] Transfer amount must be greater than 0." << endl;
return;
}
if (amount > balance) {
cout << " [!] Insufficient balance for transfer. Current balance: Rs. " << balance << endl;
return;
}
balance -= amount;
recordTransaction("transfer", amount,
"Transferred Rs. " + to_string(amount) +
" to Account #" + to_string(targetAccount.accountNumber));
targetAccount.balance += amount;
targetAccount.recordTransaction("transfer", amount,
"Received Rs. " + to_string(amount) +
" from Account #" + to_string(accountNumber));
cout << " [✓] Rs. " << amount << " transferred to Account #"
<< targetAccount.accountNumber << " successfully." << endl;
}
void showBalance() {
cout << " Account #" << accountNumber
<< " | Balance: Rs. " << balance << endl;
}
void showHistory() {
cout << " === Transaction History (Account #" << accountNumber << ") ===" << endl;
if (transactionCount == 0) {
cout << " No transactions yet." << endl;
return;
}
for (int i = 0; i < transactionCount; i++) {
cout << " [" << i + 1 << "]" << endl;
history[i].display();
}
}
int getAccountNumber() {
return accountNumber;
}
};
class Customer {
string name;
int customerID;
Account account;
public:
Customer(string name, int customerID, int accountNumber, double openingBalance)
: account(accountNumber, openingBalance)
{
this->name = name;
this->customerID = customerID;
}
void showInfo() {
cout << " Customer Name : " << name << endl;
cout << " Customer ID : " << customerID << endl;
account.showBalance();
}
Account& getAccount() {
return account;
}
string getName() {
return name;
}
};
int main() {
cout << "========================================" << endl;
cout << " WELCOME TO THE BANK " << endl;
cout << "========================================" << endl;
Customer c1("Ali", 1001, 2001, 10000);
Customer c2("Sara", 1002, 2002, 5000);
cout << "\n--- Customer Profiles ---" << endl;
c1.showInfo();
cout << endl;
c2.showInfo();
cout << "\n--- Deposits ---" << endl;
c1.getAccount().deposit(3000);
c2.getAccount().deposit(1500);
cout << "\n--- Withdrawals ---" << endl;
c1.getAccount().withdraw(2000);
c2.getAccount().withdraw(500);
cout << "\n--- Failed Withdrawal Test ---" << endl;
c2.getAccount().withdraw(99999);
cout << "\n--- Fund Transfer ---" << endl;
c1.getAccount().transfer(1000, c2.getAccount());
cout << "\n--- Final Balances ---" << endl;
c1.showInfo();
cout << endl;
c2.showInfo();
cout << "\n--- " << c1.getName() << "'s Transaction History ---" << endl;
c1.getAccount().showHistory();
cout << "\n--- " << c2.getName() << "'s Transaction History ---" << endl;
c2.getAccount().showHistory();
cout << "\n========================================" << endl;
cout << " THANK YOU! " << endl;
cout << "========================================" << endl;
return 0;
}