-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
268 lines (224 loc) · 7.64 KB
/
Copy pathmain.cpp
File metadata and controls
268 lines (224 loc) · 7.64 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
#include <iostream>
#include <vector>
#include <sstream>
#include <ctime>
#ifdef _WIN32
#define CLEAR_SCREEN "cls"
#else
#define CLEAR_SCREEN "clear"
#endif
using namespace std;
class BankAccount{
private:
string name;
int accountNum;
double balance;
vector <string> transactions ;
public:
// Constructor
BankAccount(string n,int ac, double bal): name(n), accountNum(ac), balance(bal)
//{
//name = n;
//accountNum = ac;
//balance = bal;
//}
// Setter
void setName(string n) {
name = n;
}
void setBalance(int newBalance){
balance = newBalance;
}
//Getter
string getName(){
return name;
}
int getAccountNum(){
return accountNum;
}
double getBalance(){
return balance ;
}
// Tu dois ajouter un destructor, très important parce-que si après l'utilisation de tes objets tu ne les détruis as, ils continuent de prendre de l'espace en mémoire
//Other Methods
void savetransaction(string transactiontype,const double money){
time_t now; // variable for store datetime format
time(&now); // get current time
ostringstream oss;// For concatening many variables,expressions into string
oss << "Time: " << ctime(&now) << " | " <<"Transaction Type : " <<transactiontype << " | "<< "Money : " << money<< " | " <<" Bal. : "<< balance ;
//ctime(&now) ==> convert time into String
string transaction = oss.str(); // Convert stream into String
//save
transactions.push_back(transaction);
}
void displayTransactions(){
if (!transactions.empty()) {
cout << "\t\t**********************************Transactions Info**********************************";
cout << "\t\t\tAccount Number: " << accountNum << endl;
for (int i=0 ;i<transactions.size();i++) {
cout <<"\t\t\tno "<<i+1 <<" ==> "<< transactions[i]<<endl;
}
cout << "\t\t**************************************************************************************";
}
else{
cout << "No transactions Found";
}
}
void depositMoney(double money){
balance += money;
cout << "Successful Deposit Operation . New Balance: "<<balance<<endl;
this->savetransaction("DEPOSIT",money);
}
void withdrawMoney(double taken) {
if (taken <= balance) {
balance -= taken;
this->savetransaction("WITHDRAW",taken);
cout << "Successful WithDraw Operation . New Balance: " << balance << endl;
}
else
cout << "Insufficient Balance....";
}};
class BankManagement {
private:
vector<BankAccount> accounts;
public:
void AddAccount(string name, int accountNum, double balance) {
accounts.push_back(BankAccount(name, accountNum, balance));
}
void ShowAccount(int i) {
cout << "Name : " << accounts[i].getName() << " Account Number : " << accounts[i].getAccountNum()
<< " Balance : " << accounts[i].getBalance() << endl;
cout << "\t\t***************INFO***************" << endl;
cout << "\t\t\t♠ Name : " << accounts[i].getName() << endl;
cout << "\t\t\t♠ Account Number : " << accounts[i].getAccountNum() << endl;
cout << "\t\t\t♠ Balance : " << accounts[i].getBalance() << endl;
cout << "\t\t**********************************" << endl;
}
void ShowAllAccounts() {
cout << "\t\tAll Account Holders" << endl;
for (int i = 0; i < accounts.size(); i++)
ShowAccount(i);
}
int SearchAccount(int accountnum) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNum() == accountnum)
return i;
}
return -1;
}
BankAccount *findAccount(int accountnum) {
if (this->SearchAccount(accountnum) >= 0) {
int found = this->SearchAccount(accountnum);
return &accounts[found];
}
}
};
int main() {
std::cout << "Hello, World!" << std::endl;
BankManagement bank; // Tu fais une déclaration sans affectattion alors que tu n'as pas de constructeurs par défaut.
int choice;
char option;
do{
system(CLEAR_SCREEN);
cout << "\t\t Bank Management System" << endl;
cout << "\t\t\tMain Menu" << endl;
cout << "\t\t 1. Create New Account "<<endl;
cout << "\t\t 2. Show All Account "<<endl;
cout << "\t\t 3. Search Account "<< endl;
cout << "\t\t 4. Deposit Money"<< endl;
cout << "\t\t 5. Withdraw Money" << endl;
cout << "\t\t 6. Display Transactions" << endl;
cout << "\t\t 7. Exit" << endl;
cout << "---------------------------------------------"<<endl;
cout << "\tEnter Your Choice : ";
cin >> choice;
switch (choice) {
case 1:
{
string name;
int accountNum;
double balance;
cout << "\t\tEnter Name :";
cin >> name;
cout << "\t\tEnter the account number : ";
cin >> accountNum;
cout << "\t\tEnter Initial Balance : ";
cin >> balance;
bank.AddAccount(name,accountNum,balance);
cout << "\t\t Account Created Succesfully....."<< endl;
break;
}
case 2:{
bank.ShowAllAccounts();
break;
}
case 3 : {
int num;
int found;
cout << "\t\tEnter the account number : ";
cin >>num;
found= bank.SearchAccount(num);
if (found >0)
bank.ShowAccount(found);
else
cout<< " Account not found ";
break;
}
case 4:
{
int num;
double money;
cout << "\t\tEnter the account number : ";
cin >>num;
BankAccount* account = bank.findAccount(num);
if (account !=NULL){
cout<< "\t\tEnter the money to deposit : ";
cin>> money;
account->depositMoney(money);
}
else
cout<< " Account not found ";
break;
}
case 5:
{
int num;
double money;
cout << "\t\tEnter your account number : ";
cin >> num;
BankAccount* account=bank.findAccount(num);
if (account !=NULL){
cout << "Enter the money to withdraw";
cin >> money;
account->withdrawMoney(money);
}
else{
cout <<" Account not Found";
}
break;
}
case 6:
{
int num;
cout << "\t\tEnter your account number : ";
cin >> num;
BankAccount* account=bank.findAccount(num);
if (account !=NULL){
account->displayTransactions();
}
else{
cout <<" Account not Found";
}
break;
}
case 7:
{
option = 'n';
break;
}
}
cout << "\t\t Do you want to continue [Y/N] ?";
cin >> option;
}while(option== 'y' || option =='Y');
return 0;
}