-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.cpp
259 lines (246 loc) · 7.53 KB
/
account.cpp
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
#include "account.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cctype>
#include<cmath>
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
account::account(){siz=64;num=0;}
void account::create_accout(){//take info from customer and determin offest of record in file using rrn
customer i;
string name,balance;
string type,number;
cout<<"enter your account number ";//must be unique
cin>>i.account_number;
cout<<endl;
cout<<"enter your name :- ";
cin>>i.name;
cout<<endl;
cout<<"enter type of account (c/s): -";
cin>>i.type_of_account;
cout<<endl;
cout<<"enter your balance :- ";
cin>>i.deposit;
string h="0";
strcpy(i.withdraw,h.c_str());
write_in_file(i);
}
void account::write_in_file(customer i){//write in file and determine offest of this record and return it
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
f.seekg(num*siz,ios::beg);
f.write((char*)&i,sizeof(i));
f.close();
num++;
}
void account::show(){//list all accounts in files
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
cout<<"=======================================================\n";
cout<<"A/c no. NAME Type Balance\n";
cout<<"=======================================================\n";
for(int y=0;y<num;y++){
customer i;
f.seekg(y*siz,ios::beg);
f.read((char*)&i,sizeof(i));
string number(i.account_number);
for(int i=number.size()-1;i>=0;i--){if(number[i]==' '){number.erase(i--,1);}else{break;}}
if(number=="-1"){continue ;}
cout<<y+1<<" "<<i.account_number<<" "<<i.name;
cout<<" "<<i.type_of_account<<" "<<i.deposit<<endl;
}
f.close();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
bool account::delete_record(){//delete record from vector not from file
int n;
cout<<"enter your account number "<<endl;
cin>>n;
customer i;
int z=make_offest(n);
if(z==-1){return 0;}
else{
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
f.seekg(z,ios::beg);
f.read((char*)&i,sizeof(i));
string number(i.account_number);
for(int i=number.size()-1;i>=0;i--){if(number[i]==' '){number.erase(i--,1);}else{break;}}
if(number=="-1"){
cout<<"sorry not found "<<endl;
}else{
string newnum="-1";
strcpy(i.account_number,newnum.c_str());
f.seekg(z,ios::beg);
f.write((char*)&i,sizeof(i));
cout<<"done , is deleted now "<<endl;
}
}
}
void account::show_state(int i){//show account
int offest=make_offest(i);
if(offest==-1){cout<<"not found try again "<<endl;}
else{
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
customer w;
f.seekg(offest,ios::beg);
f.read((char*)&w,sizeof(w));
cout<<"------------- account state--------------------"<<endl;
cout<<" account number :- "<<w.account_number<<endl;
cout<<" your money :- "<<w.deposit<<endl;
cout<<" your name :- "<<w.name<<endl;
f.close();
}
}
void account::WITHDRAW_AMOUNT(){//to withdraw from account
cout<<"enter your account number"<<endl;
int n;
cin>>n;
int offet=make_offest(n);
if(offet==-1){cout<<"not found "<<endl;}
else{
customer i;
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
f.seekg(offet,ios::beg);
f.read((char*)&i,sizeof(i));
string h(i.account_number);
if(h!="-1"){
cout<<"enter the amount of withdraw"<<endl;
int d;
cin>>d;
string dep(i.deposit);
int deposti=atoll(dep.c_str());
if(deposti==d ||deposti>d){
cout<<"you account now are "<<deposti-d<<endl;
int newdeposit=deposti-d;
string new_deposit;
ostringstream convert;
convert << newdeposit;
new_deposit = convert.str();
strcpy(i.deposit,new_deposit.c_str());
int z=make_offest(n);
if(z!=-1){
f.seekg(z,ios::beg);
f.write((char*)&i,sizeof(i));
}
f.close();
}else{cout<<"cant take this amount "<<endl;}
}
}
}
void account::DEPOSIT_AMOUNT(){//to deposit
cout<<"enter your account number"<<endl;
int n;
cin>>n;
customer i;
string nu;
ostringstream convert;
convert << n ;
nu = convert.str();
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
bool b=0;
for(int y=0;y<num;y++){
f.seekg(y*siz,ios::beg);
f.read((char*)&i,sizeof(i));
string number(i.account_number);
for(int i=number.size()-1;i>=0;i--){if(number[i]==' '){number.erase(i--,1);}else{break;}}
if(number=="-1"){continue ;}
if(number==nu){
b=1;
break;
}
}
if(b==0){
cout<<"sorry not found "<<endl;
f.close();
}else{
string h(i.account_number);
if(h!="-1"){
cout<<"enter the amount of deposit"<<endl;
int d;
cin>>d;
string dep(i.deposit);
int deposita=atoll(dep.c_str());
cout<<"you account now are "<<d+deposita<<endl;
int newdeposti=d+deposita;
cout<<"new --- "<<newdeposti<<endl;
string new_deposti;
ostringstream convert;
convert << newdeposti ;
new_deposti = convert.str();
strcpy(i.deposit,new_deposti.c_str());
int z=make_offest(n);
cout<<"balance "<<i.deposit<<endl;
if(z!=-1){
f.seekg(z,ios::beg);
f.write((char*)&i,sizeof(i));
}
f.close();
}}
}
int account::make_offest(int n){//return offest to search about it
string q;
customer i;
int z,d=0;
ostringstream convert;
convert << n;
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
q = convert.str();
for(int y=0;y<num;y++){
f.seekg(y*siz,ios::beg);
f.read((char*)&i,sizeof(i));
string number(i.account_number);
for(int i=number.size()-1;i>=0;i--){if(number[i]==' '){number.erase(i--,1);}else{break;}}
if(number=="-1"){continue ;}
if(number==q){
d=1;
return y*siz;
break;
}
}
if(d==0){cout<<"not found try again "<<endl;return -1;}
}
void account::modefiy(){// to modify in account
cout<<"enter your account number"<<endl;
int n;
cin>>n;
int offest=make_offest(n);
if(offest==-1){cout<<"sorry not found "<<endl;}
else{
fstream f;
f.open("info_about_customer.txt",ios::in|ios::out);
customer i;
f.seekg(offest,ios::beg);
f.read((char*)&i,sizeof(i));
string h(i.account_number);
if(h!="-1"){
string name,type;
cout<<"enter new "<<endl;
cout<<"in name"<<endl;
cin>>name;
cout<<"in type"<<endl;
cin>>type;
strcpy(i.name,name.c_str());
strcpy(i.type_of_account,type.c_str());
cout<<i.account_number<<"."<<i.name<<"."<<i.type_of_account<<endl;
f.seekg(offest,ios::beg);
f.write((char*)&i,sizeof(i));
f.close();}
else{
cout<<"sorry not found"<<endl;
}
}
}