-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path08_bank.cpp
273 lines (223 loc) · 7.02 KB
/
08_bank.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// In a bank, different customers have savings account. Some customers may have taken a loan from the bank. So bank always maintain information about bank depositors and borrowers. Design a Base class Customer (name, phoneNumber). Derive a class Depositor(accountNumber, balance) from Customer. Again, derive a class Borrower (loanNumber, loanAmount) from Depositor. Write necessary member functions to read and display the details of ‘n’ customers.
// // Header files
#include <iostream>
#include <string.h>
#include <stdlib.h>
// // use namespace
using namespace std;
// // define Macro
#define MAX_CUSTOMERS 20
// // define class Customer
class Customer
{
public:
// // static member variable
static const int MAX_CHARS_IN_NAME = 31;
static const int MAX_DIGITS_IN_NUMBER = 11;
private:
// // instance member variables
char *name, *phoneNumber;
public:
// // instance member function to set name
void setName(const char *name)
{
int length = strlen(name);
this->name = new char[length > MAX_CHARS_IN_NAME - 1 ? MAX_CHARS_IN_NAME : length + 1];
if (length > MAX_CHARS_IN_NAME - 1)
{
strncpy(this->name, name, MAX_CHARS_IN_NAME - 1);
this->name[MAX_CHARS_IN_NAME - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->name, name);
}
}
// // instance member function to get name
const char *getName() const
{
return name;
}
// // instance member function to set phoneNumber
void setPhoneNumber(const char *phoneNumber)
{
this->phoneNumber = new char[MAX_DIGITS_IN_NUMBER];
if (strlen(phoneNumber) > MAX_DIGITS_IN_NUMBER - 1)
{
strncpy(this->phoneNumber, phoneNumber, MAX_DIGITS_IN_NUMBER - 1);
this->phoneNumber[MAX_DIGITS_IN_NUMBER - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->phoneNumber, phoneNumber);
}
}
// // instance member function to get phoneNumber
const char *getPhoneNumber() const
{
return phoneNumber;
}
// // instance member function to input and set details
void setDetails()
{
char name[MAX_CHARS_IN_NAME], phoneNumber[MAX_DIGITS_IN_NUMBER];
cout << "\nEnter Name (MAX_CHARS " << MAX_CHARS_IN_NAME - 1 << ") => ";
cin.ignore();
cin.getline(name, MAX_CHARS_IN_NAME);
setName(name);
cout << "\nEnter Phone Number (MAX_CHARS " << MAX_DIGITS_IN_NUMBER - 1 << ") => ";
cin.getline(phoneNumber, MAX_DIGITS_IN_NUMBER);
setPhoneNumber(phoneNumber);
}
// // instance member function to show details
void showDetails() const
{
cout << "\nName => " << name;
cout << "\nPhone Number => " << phoneNumber;
}
// // destructor
~Customer()
{
delete[] name;
delete[] phoneNumber;
}
};
// // define class Employee by inheriting class Customer
class Depositor : public Customer
{
public:
// // static member variable
static const int MAX_DIGITS_IN_ACC_NUMBER = 6;
private:
// // instance member variables
char *accountNumber;
double balance;
public:
// // instance member function to set accountNumber
void setAccountNumber(const char *accountNumber)
{
int length = strlen(accountNumber);
this->accountNumber = new char[length > MAX_DIGITS_IN_ACC_NUMBER - 1 ? MAX_CHARS_IN_NAME : length + 1];
if (length > MAX_DIGITS_IN_ACC_NUMBER - 1)
{
strncpy(this->accountNumber, accountNumber, MAX_DIGITS_IN_ACC_NUMBER - 1);
this->accountNumber[MAX_DIGITS_IN_ACC_NUMBER - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->accountNumber, accountNumber);
}
}
// // instance member function to get accountNumber
const char *getAccountNumber()
{
return accountNumber;
}
// // instance member function to set balance
void setBalance(double balance)
{
this->balance = balance;
}
// // instance member function to get balance
double getBalance()
{
return balance;
}
// // instance member function to input and set details
void setDetails()
{
char accountNumber[MAX_DIGITS_IN_ACC_NUMBER];
Customer::setDetails();
cout << "\nEnter Account Number (MAX_DIGITS " << MAX_DIGITS_IN_ACC_NUMBER - 1 << ") => ";
cin.ignore();
cin.getline(accountNumber, MAX_DIGITS_IN_ACC_NUMBER);
setAccountNumber(accountNumber);
cout << "\nEnter Balance => ";
cin >> balance;
}
// // instance member function to show details
void showDetails()
{
Customer::showDetails();
cout << "\nAccount Number => " << accountNumber;
cout << "\nBalance => " << balance;
}
};
// // define class Borrower by inheriting class Depositor
class Borrower : public Depositor
{
private:
// // instance member variables
int loanNumber;
double loanAmount;
public:
// // instance member function to set loanNumber
void setLoanNumber(double loanNumber)
{
this->loanNumber = loanNumber;
}
// // instance member function to get loanNumber
double getLoanNumber() const
{
return loanNumber;
}
// // instance member function to set loanAmount
void setLoanAmount(double loanAmount)
{
this->loanAmount = loanAmount;
}
// // instance member function to get loanAmount
double getLoanAmount()
{
return loanAmount;
}
// // instance member function to input and set details
void setDetails()
{
Depositor::setDetails();
cout << "\nEnter Loan Number => ";
cin >> loanNumber;
cout << "\nEnter Loan Amount => ";
cin >> loanAmount;
}
// // instance member function to show details
void showDetails()
{
Depositor::showDetails();
cout << "\nLoan Number => " << loanNumber;
cout << "\nLoan Amount => " << loanAmount;
}
};
// // Main Function Start
int main()
{
int n;
cout << "\nHow Many Customers Details You Want to Enter (MAX " << MAX_CUSTOMERS << ") => ";
cin >> n;
// // invalid input
if (n < 1 || n > MAX_CUSTOMERS)
{
cout << "\n!!! Invalid Input...";
exit(0);
}
// // create an array of instances of Borrower
Borrower customers[n];
// // get details
cout << "\n>>>>>>>>> Enter Details of " << n << " Customers <<<<<<<<<<<\n";
for (int i = 0; i < n; i++)
{
cout << "\n>>>>>>>> Enter Details of Customer-" << i + 1 << " <<<<<<<<<<\n";
customers[i].setDetails();
}
// // show details
cout << "\n>>>>>>>>> Details of " << n << " Customers <<<<<<<<<<<\n";
for (int i = 0; i < n; i++)
{
cout << "\n\n>>>>>>>> Details of Customer-" << i + 1 << " <<<<<<<<<<";
customers[i].showDetails();
}
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End