-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBanking.cpp
More file actions
324 lines (284 loc) · 8.03 KB
/
Copy pathBanking.cpp
File metadata and controls
324 lines (284 loc) · 8.03 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//214
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <sstream>
using namespace std;
struct _Account { // acc = Account
int acc_no;
char name[50];
int balance;
char type;
};
struct Account { // acc = Account
int acc_no;
char name[50];
int balance;
char type;
Account *next;
};
/*************** GLOBAL VARIABLES ***************/
struct Account *Prev, *Curr;
struct Account *Head = NULL; // Head of the Linked List
// will write something here
/********************* FUNCTIONS PROTOTYPES ********************/
void create_account(void); //Adding new account
void DeleteAccount(void);
void show_account(int);
void dep(int); // deposit amount
void draw(int); // withdraw amount
Account* search(int);
void LoadList(void);
void SaveList(void);
// void modify();
/****************** FUNCTION DEFINITIONS **********************/
/*
bool is_empty(Account *head) {
return head == nullptr;
} */
/****************** CREATE Function **********************/
void create_account() {
struct Account *NewNode = new Account;
// Account *NewNode = new Account;
cout << "Enter the account ID\n";
cin >> NewNode->acc_no;
cout << "Enter the account name\n";
//cin >> NewNode->name;
cin.ignore();
cin.getline(NewNode->name, 50);
cout << "Enter the amount of balance\n";
cin >> NewNode->balance;
cout << "Enter the type of Saving or Current (S/C)";
cin >> NewNode->type;
NewNode->next = NULL;
if (Head == NULL) {
Head = NewNode;
cout << "Account Added\n";
return;
}
if(NewNode->acc_no == Head->acc_no){
cout<<"\nAccount ID already exists\n\n";
return;
}
if (NewNode->acc_no < Head->acc_no) {
NewNode->next = Head;
Head = NewNode;
cout <<"Account Added\n";
return;
}
Prev = Curr = NULL; // To find the pre and cur position, Loop
for (Curr = Head; Curr != NULL; Curr = Curr->next) {
if(NewNode->acc_no == Head->acc_no){
cout<<"\nAccount ID already exists\n\n";
return;
}
if (NewNode->acc_no < Curr->acc_no)
break;
Prev = Curr;
}
NewNode->next = Prev->next;
Prev->next = NewNode;
cout << "\nAccount Added\n\n";
} // End of Creation function
/****************** SHOW FUNCTION **********************/
void DeleteAccount(void){
if(Head == NULL){
cout<< "Empty\n\n";
return;
}
int key;
cout << "Enter account ID to delete: ";
cin >> key;
if(Head->acc_no == key){
Head = Head->next;
return;
cout <<"Account Deleted\n\n";
}
for(Curr = Head; Curr != NULL; Curr = Curr->next){
if (Curr->acc_no == key)
break;
Prev = Curr;
} if(Curr = NULL){
cout << "Account not Found\n\n";
}else{
Prev->next = Curr->next;
delete(Curr);
cout <<"Account Deleted\n\n";
}
}
/****************** Show All Accounts **********************/
void show_account(void) {
if (Head == NULL) {
cout << "Empty Linked List!";
cout << "\nNothing to Show\n";
return;
}
Curr = Head;
while (Curr != NULL) {
cout << "Account No. : " << Curr->acc_no;
cout << "\tAccount Name: " << Curr->name;
cout << "\tAccount Balance: " << Curr->balance;
cout << "\tAccount Type: " << Curr->type;
cout << "\n";
Curr = Curr->next;
}
}
/****************** Show Single Account **********************/
void show_one() {
int key;
cout << "Enter the Account No. using ID\n";
cin >> key;
if(Curr = search(key)){
cout << "Account No. : " << Curr->acc_no;
cout << "\tAccount Name: " << Curr->name;
cout << "\tAccount Deposit: " << Curr->balance;
cout << "\tAccount Type: "<< Curr->type;
}
}
/****************** DEPOSIT FUNCTION **********************/
void dep(int x) { // Balance
// how to access to the client
int key;
cout << "Enter the search key now\n";
cin >> key;
Curr = search(key);
if (Curr != NULL)
Curr->balance += x;
else
cout << key << " Not found in database \n";
}
/****************** WITHDRAW FUNCTION **********************/
void draw(int x) {
int key;
cout << "Enter the search key now\n";
cin >> key;
Curr = search(key);
if (Curr != NULL)
Curr->balance -= x;
else
cout << key << " Not found in database \n";
}
/****************** SEARCH FUNCTION **********************/
Account * search(int key) {
Curr = Head;
// Iterate till last element until key is not found
while (Curr != NULL && Curr->acc_no != key) {
Curr = Curr->next;
}
if (Curr != NULL) {
return Curr;
} else
return NULL;
}
/****************** SAVE LIST **********************/
void SaveList(void) {
ofstream pFile;
pFile.open("Account.dat", ios::binary);
if (!pFile.is_open()) {
cout << "Can't Open File\n Updated data not saved into file\n";
return;
}
_Account AC;
Account *Curr;
// copy record from linked list into file record structure
for (Curr = Head; Curr != NULL; Curr->next) {
AC.acc_no = Curr->acc_no;
strcpy(AC.name, Curr->name);
AC.balance = Curr->balance;
AC.type = Curr->type;
pFile.write((char *) &AC, sizeof (AC)); //write record into a file
}
pFile.close();
cout << "Saved to File!\n\n";
}
/****************** LOAD LIST **********************/
void LoadList(void) {
ifstream iFile;
iFile.open("Account.dat", ios::binary);
if (!iFile.is_open()) {
Head = NULL;
cout << "File not Exist\n";
return;
}
_Account AC; //Structure variable to hold data from file
Account *NewNode, *Tail = Head =NULL;; // Structure pointer to add data into linked list
// Read First Record from file
iFile.read((char*) &AC, sizeof (AC));
while(!iFile.eof())
{
NewNode = new Account;
NewNode->acc_no = AC.acc_no;
strcpy(NewNode->name, AC.name);
NewNode->balance = AC.balance;
NewNode->type = AC.type;
NewNode->next = NULL;
if(Head == NULL){
Head = NewNode;
Tail = Head;
}else {
Tail->next = NewNode;
Tail = NewNode;
}
// read next record from file
iFile.read((char*) &AC, sizeof (AC));
}
iFile.close();
cout << "Successfully Loaded File\n";
}
/*************************MAIN FUNCTION****************************/
int main() {
int choice;
int val;
//LoadList();
while (true) {
cout << "\n1.Create account\n";
cout << "2.Show account\n";
cout << "3.To Deposit\n";
cout << "4.To Withdraw\n";
cout << "5.To LoadList\n6.To SaveList\n";
cout << "7.To Delete Account\n8.To Show Single Account\n";
cout << "9.To Exist\n\n";
cin>> choice;
switch (choice) {
case 1:
cout << "Lets Go:\n\n";
create_account();
break;
case 2:
show_account();
break;
case 3:
cout << "You have to enter the value\n";
cin >> val;
dep(val);
break;
case 4:
cout << "You have to enter the value\n";
cin >> val;
draw(val);
break;
case 5:
LoadList();
break;
case 6:
SaveList();
break;
case 7:
DeleteAccount();
break;
case 8:
show_one();
break;
case 9:
cout << "Program Ended Byee~\n";
exit(0);
break;
default:
cout << "Nothing Entered Byee~\n";
break;
}
}
}