Q32. Program to implement File Handling With Class Objects:
Define a class to represent a bank account. Include the following members:
i) Depositor Name
ii) Account Number
iii) Balance Amount
Member Function
i) To Assign Initial values(opening balance Rs. 1000 by default)
ii) To deposit an amount
iii) To withdraw an amount(if possible)
iv) To display the current balance
Write a file based program to store the records of at least ten accounts in “ACCOUNT_DETAIL” data file and perform the required manipulations- DEPOSIT, WITHDRAW & BAL_ENQUIRY using the file on a given account. The changes after each deposit and withdrawal should be updated in the given file.
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
#include
#include
#include
#include
const int MAX = 100;
enum bool{_false, _true};
class Cbank{
private:
int accNo;
char name[20];
float bal;
public:
Cbank() : accNo(100){}
void getAcc(int );
bool accPresent(int );
void saveBal(int, float );
void dispAcc() const;
void dispAll() const;
};
void Cbank :: getAcc(int i){
accNo += i;
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Amount for opening Acc.: ";
cin>>bal;
}
bool Cbank :: accPresent(int _acnt){
if(accNo == _acnt)
return _true;
else
return _false;
}
void Cbank :: saveBal(int type, float _amt){
if(type == 2){ // Deposit.
bal += _amt;
cout<<"\n Balance Updated.";
}
else if(type == 3){ // Withdrawl.
if(bal >= _amt){
bal -= _amt;
cout<<"\n Balance Updated.";
}
else
cout<<"\n Not Enough Funds on Account, SORRY !!";
}
}
void Cbank :: dispAcc() const{
cout<<"\n Account Information:-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~";
cout<<"\n Account No.: "< cout<<"\n Holders Name: "< cout<<"\n Balance: "< }
void Cbank :: dispAll() const{
cout< }
void main(){
int ch, i, _tot = 0;
int _acc;
float _amt;
char nx;
bool found;
Cbank *Obank = new(Cbank[MAX]);
fstream fstr;
fstr.open("Bank.txt", ios::in | ios::nocreate);
if(fstr){
while(fstr){
fstr.read((char *) &Obank[_tot],
sizeof(Obank[_tot]));
_tot++;
}
_tot -= 2;
}
fstr.close();
// Data Retrieved form Disk.
while(1){
clrscr();
cout<<"\n BANK AUTOMATION";
cout<<"\n ~~~~~~~~~~~~~~~";
cout<<"\n 1 -> Create Account.";
cout<<"\n 2 -> Deposit Money.";
cout<<"\n 3 -> Withdrawl Money.";
cout<<"\n 4 -> Balance Inqury.";
cout<<"\n 5 -> Display Accounts.";
cout<<"\n 6 -> Exit.";
cout<<"\n\n Enter your choice: ";
cin>>ch;
clrscr();
switch(ch){
case 1: // Create Acc.
while(_tot < MAX){
cout<<"\n Account No.: "<<_tot p=""> Obank[_tot].getAcc(_tot);
cout<<"\n Want to add more (y/n): ";
cin>>nx;
_tot++;
if(nx == 'n')
break;
}
break;
case 2: // Deposit Money.
found = _false;
cout<<"\n Enter Account No: ";
cin>>_acc;
for(i=0; i<=_tot; i++){
if(Obank[i].accPresent(_acc)){
found = _true;
break;
}
}
if(!found){
cout<<"\n Account Not Found.";
break;
}
else{
cout<<"\n Enter Amount to Deposit: ";
cin>>_amt;
Obank[i].saveBal(ch, _amt);
}
break;
case 3: // Withdrawl Money.
found = _false;
cout<<"\n Enter Account No: ";
cin>>_acc;
for(i=0; i<=_tot; i++){
if(Obank[i].accPresent(_acc)){
found = _true;
break;
}
}
if(!found){
cout<<"\n Account Not Found.";
break;
}
else{
cout<<"\n Enter Amount to Withdrawl:";
cin>>_amt;
Obank[i].saveBal(ch, _amt);
}
break;
case 4: // Balance Inquiry.
found = _false;
cout<<"\n Enter Account No: ";
cin>>_acc;
for(i=0; i<=_tot; i++){
if(Obank[i].accPresent(_acc)){
found = _true;
break;
}
}
if(!found){
cout<<"\n Account Not Found.";
break;
}
else{
Obank[i].dispAcc();
}
break;
case 5: // Display Acc.
cout<<"\n Account Information:-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~";
cout<<"\n ACC No."< <<"NAME"< cout<<"\n``````````````````````````````````
``````````````````````````````````";
for(i=0; i<_tot i="" p=""> Obank[i].dispAll();
break;
case 6: // Exit;
fstr.open("Bank.txt", ios::out);
for(i=0; i<=_tot; i++)
fstr.write((char *) &Obank[i],
sizeof(Obank[i]));
fstr.close();
// Data Saved to Disk.
delete []Obank;
exit(1);
default:
cout<<"\n Enter Appropriate Choice.";
} // end of switch.
getch();
} // end of while.
} // end of main.
Define a class to represent a bank account. Include the following members:
i) Depositor Name
ii) Account Number
iii) Balance Amount
Member Function
i) To Assign Initial values(opening balance Rs. 1000 by default)
ii) To deposit an amount
iii) To withdraw an amount(if possible)
iv) To display the current balance
Write a file based program to store the records of at least ten accounts in “ACCOUNT_DETAIL” data file and perform the required manipulations- DEPOSIT, WITHDRAW & BAL_ENQUIRY using the file on a given account. The changes after each deposit and withdrawal should be updated in the given file.
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
#include
#include
#include
#include
const int MAX = 100;
enum bool{_false, _true};
class Cbank{
private:
int accNo;
char name[20];
float bal;
public:
Cbank() : accNo(100){}
void getAcc(int );
bool accPresent(int );
void saveBal(int, float );
void dispAcc() const;
void dispAll() const;
};
void Cbank :: getAcc(int i){
accNo += i;
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Amount for opening Acc.: ";
cin>>bal;
}
bool Cbank :: accPresent(int _acnt){
if(accNo == _acnt)
return _true;
else
return _false;
}
void Cbank :: saveBal(int type, float _amt){
if(type == 2){ // Deposit.
bal += _amt;
cout<<"\n Balance Updated.";
}
else if(type == 3){ // Withdrawl.
if(bal >= _amt){
bal -= _amt;
cout<<"\n Balance Updated.";
}
else
cout<<"\n Not Enough Funds on Account, SORRY !!";
}
}
void Cbank :: dispAcc() const{
cout<<"\n Account Information:-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~";
cout<<"\n Account No.: "<
void Cbank :: dispAll() const{
cout<
void main(){
int ch, i, _tot = 0;
int _acc;
float _amt;
char nx;
bool found;
Cbank *Obank = new(Cbank[MAX]);
fstream fstr;
fstr.open("Bank.txt", ios::in | ios::nocreate);
if(fstr){
while(fstr){
fstr.read((char *) &Obank[_tot],
sizeof(Obank[_tot]));
_tot++;
}
_tot -= 2;
}
fstr.close();
// Data Retrieved form Disk.
while(1){
clrscr();
cout<<"\n BANK AUTOMATION";
cout<<"\n ~~~~~~~~~~~~~~~";
cout<<"\n 1 -> Create Account.";
cout<<"\n 2 -> Deposit Money.";
cout<<"\n 3 -> Withdrawl Money.";
cout<<"\n 4 -> Balance Inqury.";
cout<<"\n 5 -> Display Accounts.";
cout<<"\n 6 -> Exit.";
cout<<"\n\n Enter your choice: ";
cin>>ch;
clrscr();
switch(ch){
case 1: // Create Acc.
while(_tot < MAX){
cout<<"\n Account No.: "<<_tot p=""> Obank[_tot].getAcc(_tot);
cout<<"\n Want to add more (y/n): ";
cin>>nx;
_tot++;
if(nx == 'n')
break;
}
break;
case 2: // Deposit Money.
found = _false;
cout<<"\n Enter Account No: ";
cin>>_acc;
for(i=0; i<=_tot; i++){
if(Obank[i].accPresent(_acc)){
found = _true;
break;
}
}
if(!found){
cout<<"\n Account Not Found.";
break;
}
else{
cout<<"\n Enter Amount to Deposit: ";
cin>>_amt;
Obank[i].saveBal(ch, _amt);
}
break;
case 3: // Withdrawl Money.
found = _false;
cout<<"\n Enter Account No: ";
cin>>_acc;
for(i=0; i<=_tot; i++){
if(Obank[i].accPresent(_acc)){
found = _true;
break;
}
}
if(!found){
cout<<"\n Account Not Found.";
break;
}
else{
cout<<"\n Enter Amount to Withdrawl:";
cin>>_amt;
Obank[i].saveBal(ch, _amt);
}
break;
case 4: // Balance Inquiry.
found = _false;
cout<<"\n Enter Account No: ";
cin>>_acc;
for(i=0; i<=_tot; i++){
if(Obank[i].accPresent(_acc)){
found = _true;
break;
}
}
if(!found){
cout<<"\n Account Not Found.";
break;
}
else{
Obank[i].dispAcc();
}
break;
case 5: // Display Acc.
cout<<"\n Account Information:-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~";
cout<<"\n ACC No."<
``````````````````````````````````";
for(i=0; i<_tot i="" p=""> Obank[i].dispAll();
break;
case 6: // Exit;
fstr.open("Bank.txt", ios::out);
for(i=0; i<=_tot; i++)
fstr.write((char *) &Obank[i],
sizeof(Obank[i]));
fstr.close();
// Data Saved to Disk.
delete []Obank;
exit(1);
default:
cout<<"\n Enter Appropriate Choice.";
} // end of switch.
getch();
} // end of while.
} // end of main.
Comments
Post a Comment
share your thoughts ....