-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanking system.cpp
More file actions
207 lines (205 loc) · 4.88 KB
/
banking system.cpp
File metadata and controls
207 lines (205 loc) · 4.88 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
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Account{
private:
string AccountCode;
string Name;
string Surname;
float Balance;
public:
Account(){}
Account(string AccountCode0, string Name0, string Surname0, float Balance0 = 0){
AccountCode = AccountCode0;
Name = Name0;
Surname = Surname0;
Balance = Balance0;
}
string getCode(){return AccountCode;}
string getName(){return Name;}
string getSurname(){return Surname;}
float getBalance(){return Balance;}
void setAccountCode(string code){AccountCode = code;}
void setName(string name){Name = name;}
void setSurname(string surname){Surname = surname;}
void setBalance(float bal){Balance = bal;}
friend ofstream & operator<<(ofstream &ofs, Account &acc);
friend ostream & operator<<(ostream &ofs, Account &acc);
friend ifstream & operator>>(ifstream &ofs, Account &acc);
friend istream & operator>>(istream &ofs, Account &acc);
};
istream & operator>>(istream &ofs, Account &acc){
string AccountCode;
string Name;
string Surname;
float Balance;
ofs >> AccountCode >> Name >> Surname >> Balance;
acc.setAccountCode(AccountCode);
acc.setName(Name);
acc.setSurname(Surname);
acc.setBalance(Balance);
}
ifstream & operator>>(ifstream &ofs, Account &acc){
string AccountCode;
string Name;
string Surname;
float Balance;
ofs >> AccountCode >> Name >> Surname >> Balance;
acc.setAccountCode(AccountCode);
acc.setName(Name);
acc.setSurname(Surname);
acc.setBalance(Balance);
}
ofstream & operator<<(ofstream &ofs, Account &acc){
ofs << acc.getCode() << " " << acc.getName() << " " << acc.getSurname() << " " << acc.getBalance() << endl;
}
ostream & operator<<(ostream &ofs, Account &acc){
ofs << acc.getCode() << " " << acc.getName() << " " << acc.getSurname() << " " << acc.getBalance() << endl;
}
vector<Account> accs;
void FindByID(string ID){
for(int i = 0; i < accs.size();i++){
if(ID == accs[i].getCode()){
cout << "Balance of this account: "<<accs[i].getBalance()<<endl;
return;
}
}
cout << "Account not found.\n\n";
}
void SaveData(){
ofstream ofs("banking.txt");
ofs << accs.size()<<endl;
for(int i = 0; i < accs.size();i++){
ofs << accs[i];
}
ofs.close();
}
void LoadData(){
ifstream ifs;
ifs.open("banking.txt");
accs.clear();
Account acc;
int x;
ifs >> x;
for(int i = 0; i < x;i++){
ifs >> acc;
accs.push_back(acc);
}
ifs.close();
}
bool isUnique(string s){
if(s.size() == 0)return false;
LoadData();
for(int i = 0; i < accs.size();i++){
if(s == accs[i].getCode())return false;
}
return true;
}
string gen(){
string s;
srand(time(NULL));
while(!isUnique(s)){
s.clear();
for(int i =0;i < 10;i++){
int x = rand() % 10;
s+=(char)x+'0';
}
}
return s;
}
void OpenAccount(){
cout << "Please enter Name:\n";
string n;
cin >> n;
cout << "Enter Surname:\n";
string s;
cin >> s;
cout << "Enter starting balance:\n";
float bal;
cin >> bal;
Account acc(gen(), n, s, bal);
accs.push_back(acc);
SaveData();
}
void ShowBalance(){
cout << "Enter ID of Account:\n";
string m;
cin >> m;
FindByID(m);
}
void Deposit(){
cout << "Enter ID of Account:\n";
string s;
cin >> s;
cout << "How much to deposit?\n";
float b;
cin >> b;
for(int i = 0; i < accs.size();i++){
if(s == accs[i].getCode()){
accs[i].setBalance(accs[i].getBalance()+b);
cout << "Deposited.\n\n";
SaveData();
return;
}
}
cout << "Account not found.\n";
}
void Withdraw(){
cout << "Enter ID of Account:\n";
string s;
cin >> s;
cout << "How much to withdraw?\n";
float b;
cin >> b;
for(int i = 0; i < accs.size();i++){
if(s == accs[i].getCode()){
accs[i].setBalance(accs[i].getBalance()-b);
cout << "Withdrawn.\n\n";
SaveData();
return;
}
}
cout << "Account not found.\n";
}
void CloseAccount(){
cout << "Enter ID of Account:\n";
string s;
cin >> s;
vector<Account> ac;
for(int i = 0; i < accs.size();i++){
if(s != accs[i].getCode()){
ac.push_back(accs[i]);
}
}
accs = ac;
SaveData();
LoadData();
cout << "Account deleted\n";
}
void ShowAccounts(){
LoadData();
cout << "All accounts:\n";
for(int i = 0; i < accs.size();i++){
cout << accs[i];
}
cout << endl;
return;
}
int main(){
LoadData();
while(1){
int x;
cout << "\nHello, welcome dear User! Choose an option:\n1. Open an account\n2. Show Balance\n3. Deposit\n4. Withdraw\n5. Close an account\n6. Show all accounts\n7. Quit\n";
cin >> x;
switch(x){
case 1: OpenAccount();break;
case 2: ShowBalance();break;
case 3: Deposit();break;
case 4: Withdraw();break;
case 5: CloseAccount();break;
case 6: ShowAccounts();break;
case 7: cout << "Thank you for using our services. Goodbye!\n";return 0;
}
}
}