-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
367 lines (306 loc) · 14.6 KB
/
index.js
File metadata and controls
367 lines (306 loc) · 14.6 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import readlineSync from 'readline-sync';
import fs from 'fs';
let appData = {};
console.log('***Welcome to Console Bank App***\n');
// Initialize the appData for the application
try{
let currentData = JSON.parse(fs.readFileSync('database.json', 'utf8'));
appData = {...currentData};
}
catch(err){
console.log('Database not found. Creating a new database...', err);
appData = {
users: [],
transactions: []
};
fs.writeFileSync('database.json', JSON.stringify(appData));
}
const bankingOptions = ['Create Account', 'Deposit', 'Withdraw', 'Transfer', 'View Account Details', 'View Transaction History'];
// Display Banking Options
const displayBankingOptions = () => {
console.log('\nBanking Menu: ')
console.log('*******************')
const bankingOptionIndex = readlineSync.keyInSelect(bankingOptions, 'Select an option: ');
switch(bankingOptionIndex){
case 0:
createAccount();
break;
case 1:
deposit();
break;
case 2:
withdraw();
break;
case 3:
transfer();
break;
case 4:
viewAccountDetails();
break;
case 5:
viewTransactionHistory();
break;
default:
console.log('\nThank you for using this app. Goodbye!...\n');
process.exit();
}
}
// Create Account
const createAccount = () => {
let accountName = readlineSync.question('Enter Account Name: ');
// Check if account name already exists
let accountNameExists = appData.users.find(account => account.accountName.toLowerCase() === accountName.toLowerCase());
if(accountNameExists){
console.log('Account name already exists\n');
const tryAgain = readlineSync.keyInYNStrict('Do you want to try again? Press Y for Yes and N for No: ');
tryAgain ? createAccount() : displayBankingOptions();
}
let pin = readlineSync.question('Enter PIN - (Four Digits) : ');
while(pin.length !== 4 || isNaN(pin)){
console.log('Invalid PIN. PIN must be four digits');
pin = readlineSync.question('Enter PIN - (Four Digits) : ');
}
let confirmPin = readlineSync.question('Confirm PIN - (Four Digits) : ');
while(confirmPin.length !== 4 || isNaN(confirmPin)){
console.log('Invalid PIN. PIN must be four digits');
confirmPin = readlineSync.question('Confirm PIN - (Four Digits) : ');
}
while(confirmPin !== pin){
console.log('PINs do not match');
confirmPin = readlineSync.question('Confirm PIN - (Four Digits) : ');
}
let accountNumber = generateAccountNumber();
let accountDetails = {
accountName,
accountNumber,
pin,
accountBalance : 0,
transactionHistory: [],
createdAt: new Date(),
status: 'active'
}
appData.users.push(accountDetails);
fs.writeFileSync('database.json', JSON.stringify(appData));
console.log('\n*** Account created successfully ***\n');
console.log(`Account Name: ${accountName} \nAccount Number: ${accountNumber} \nAccount Balance: ${accountDetails.accountBalance}`);
// Prompt for banking options
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to create another account? Press Y for Yes and N for No: ');
performAnotherOperation ? createAccount() : displayBankingOptions();
}
// Generate Account Number - I'm making it four digits for simplicity
const generateAccountNumber = () => {
let accountNumber = Math.floor(Math.random() * 1000);
let accountNumberExists = appData.users.find(account => account.accountNumber === accountNumber);
if(accountNumberExists){
generateAccountNumber();
}
return accountNumber;
}
// Deposit
const deposit = () => {
let accountNumber = readlineSync.question('Enter Account Number: ');
let account = appData.users.find(account => account.accountNumber === Number(accountNumber));
if(!account){
console.log('\nAccount not found');
const tryAgain = readlineSync.keyInYNStrict('\nDo you want to try again? Press Y for Yes and N for No: ');
tryAgain ? deposit() : displayBankingOptions();
}
let amount = readlineSync.question('Enter Amount: ');
while(isNaN(amount) || Number(amount) <= 0){
console.log('\nInvalid amount. Amount must be a number greater than 0');
amount = readlineSync.question('Enter Amount: ');
}
let confirmDeposit = readlineSync.keyInYNStrict(`\nConfirm Deposit of ${amount} to ${account.accountName} with account Number - ${account.accountNumber} - (Press Y for Yes and N for No) : `);
if(!confirmDeposit){
console.log('\n*** Deposit cancelled ***');
displayBankingOptions();
}
account.accountBalance += Number(amount);
account.transactionHistory.push({type: 'Deposit', amount, date: new Date(), senderAccountNumber: account.accountNumber, senderAccountName: account.accountName});
appData.transactions.push({type: 'Deposit', amount, date: new Date(), senderAccountNumber: account.accountNumber, senderAccountName: account.accountName});
fs.writeFileSync('database.json', JSON.stringify(appData));
console.log('\n*** Deposit successful ***');
console.log(`Account Name: ${account.accountName} \nAccount Number: ${account.accountNumber} \nAccount Balance: ${account.accountBalance}`);
// Prompt for banking options
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to perform another deposit operation? Press Y for Yes and N for No: ');
performAnotherOperation ? deposit() : displayBankingOptions();
}
// Transfer
const transfer = () => {
let accountNumber = readlineSync.question('Enter Sender Account Number: ');
let account = appData.users.find(account => account.accountNumber === Number(accountNumber));
if(!account){
console.log('\n*** Account not found ***');
const tryAgain = readlineSync.keyInYNStrict('\nDo you want to try again? Press Y for Yes and N for No: ');
tryAgain ? transfer() : displayBankingOptions();
}
if(account.status === 'locked'){
console.log('\nYou cannot perform this transaction because your account is locked. Kindly contact customer care');
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to perform another operation? Press Y for Yes and N for No: ');
if(performAnotherOperation){
displayBankingOptions()
} else{
console.log('\nThank you for using this app. Goodbye!...');
return;
}
}
let recipientAccountNumber = readlineSync.question('Enter Recipient Account Number: ');
let recipientAccount = appData.users.find(account => account.accountNumber === Number(recipientAccountNumber));
if(!recipientAccount){
console.log('\n*** Recipient Account not found ***');
const tryAgain = readlineSync.keyInYNStrict('\nDo you want to try again? Press Y for Yes and N for No: ');
if(tryAgain){
recipientAccountNumber = readlineSync.question('Enter Recipient Account Number: ');
recipientAccount = appData.users.find(account => account.accountNumber === Number(recipientAccountNumber));
}
else{
console.log('\n*** Transfer cancelled ***');
displayBankingOptions();
}
}
let amount = readlineSync.question('Enter Amount: ');
while(isNaN(amount) || Number(amount) <= 0){
console.log('Invalid amount. Amount must be a number greater than 0');
amount = readlineSync.question('Enter Amount: ');
}
while(Number(amount) > account.accountBalance){
console.log('\nInsufficient funds. Kindly deposit more funds into your account or enter a lesser amount');
amount = readlineSync.question('Enter Amount: ');
}
let confirmTransfer = readlineSync.keyInYNStrict(`\nConfirm Transfer of ${amount} to ${account.accountName} with account Number - ${account.accountNumber} - (Press Y for Yes and N for No) : `);
if(!confirmTransfer){
console.log('\n*** Transfer cancelled ***');
displayBankingOptions();
return;
}
// Verify PIN
let pin = readlineSync.question('Enter PIN - (Four Digits) : ');
let pinConfirmed = false;
for(let i = 0; i < 2; i++){
if(account.pin === pin) {
pinConfirmed = true;
break;
}else{
console.log('Wrong PIN. You have ' + (2 - i) + ' attempts left\n');
pin = readlineSync.question('Enter PIN - (Four Digits) : ');
}
}
if(!pinConfirmed){
console.log('\n *** Maximum number of attempts exceeded. Your account has been locked. Kindly contact customer care ***\n');
account.status = 'locked';
fs.writeFileSync('database.json', JSON.stringify(appData));
displayBankingOptions();
return;
}
account.accountBalance -= Number(amount);
account.transactionHistory.push({type: 'Transfer', amount, date: new Date(), recipientAccountNumber, recipientAccountName: recipientAccount.accountName});
recipientAccount.transactionHistory.push({type: 'Deposit', amount, date: new Date(), senderAccountNumber: account.accountNumber, senderAccountName: account.accountName});
appData.transactions.push({type: 'Transfer', amount, date: new Date(), accountNumber, recipientAccountNumber, recipientAccountName: recipientAccount.accountName, senderAccountNumber: account.accountNumber, senderAccountName: account.accountName});
appData.transactions.push({type: 'Deposit', amount, date: new Date(), senderAccountNumber: account.accountNumber, senderAccountName: account.accountName, recipientAccountNumber, recipientAccountName: recipientAccount.accountName});
recipientAccount.accountBalance += Number(amount);
fs.writeFileSync('database.json', JSON.stringify(appData));
console.log('\n*** Transfer successful ***');
console.log(`Account Name: ${account.accountName} \nAccount Number: ${account.accountNumber} \nAccount Balance: ${account.accountBalance}`);
// Prompt for banking options
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to perform another transfer operation? Press Y for Yes and N for No: ');
performAnotherOperation ? transfer() : displayBankingOptions();
}
// Withdraw
const withdraw = () => {
let accountNumber = readlineSync.question('Enter Account Number: ');
let account = appData.users.find(account => account.accountNumber === Number(accountNumber));
if(!account){
console.log('\n*** Account not found ***');
const tryAgain = readlineSync.keyInYNStrict('Do you want to try again? Press Y for Yes and N for No: ');
tryAgain ? withdraw() : displayBankingOptions();
}
if(account.status === 'locked'){
console.log('\n*** You cannot perform this transaction because your account is locked. Kindly contact customer care ***');
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to perform another operation? Press Y for Yes and N for No: ');
if(performAnotherOperation){
displayBankingOptions()
} else{
console.log('\nThank you for using this app. Goodbye!...');
return;
}
}
let amount = readlineSync.question('Enter Amount: ');
while(isNaN(amount) || Number(amount) <= 0){
console.log('\nInvalid amount. Amount must be a number greater than 0');
amount = readlineSync.question('Enter Amount: ');
}
while(Number(amount) > account.accountBalance){
console.log('\n*** Insufficient funds. Kindly deposit more funds into your account or enter a lesser amount ***');
amount = readlineSync.question('Enter Amount: ');
}
let confirmWithdrawal = readlineSync.keyInYNStrict(`\nConfirm Withdrawal of ${amount} from ${account.accountName} with account Number - ${account.accountNumber} - (Press Y for Yes and N for No) : `);
if(!confirmWithdrawal){
console.log('\n*** Withdrawal cancelled ***');
displayBankingOptions();
return;
}
// Verify PIN
let pin = readlineSync.question('Enter PIN - (Four Digits) : ');
let pinConfirmed = false;
for(let i = 0; i < 2; i++){
if(account.pin === pin) {
pinConfirmed = true;
break;
}else{
console.log('\nWrong PIN. You have ' + (2 - i) + ' attempts left');
pin = readlineSync.question('Enter PIN - (Four Digits) : ');
}
}
if(!pinConfirmed){
console.log('\n*** Maximum number of attempts exceeded. Your account has been locked. Kindly contact customer care ***\n');
account.status = 'locked';
fs.writeFileSync('database.json', JSON.stringify(appData));
displayBankingOptions();
return;
}
account.accountBalance -= Number(amount);
account.transactionHistory.push({type: 'Withdrawal', amount, date: new Date(), senderAccountNumber: account.accountNumber, senderAccountName: account.accountName});
appData.transactions.push({type: 'Withdrawal', amount, date: new Date(), senderAccountNumber: account.accountNumber, senderAccountName: account.accountName});
fs.writeFileSync('database.json', JSON.stringify(appData));
console.log('\n*** Withdrawal successful ***');
console.log(`\nAccount Name: ${account.accountName} \nAccount Number: ${account.accountNumber} \nAccount Balance: ${account.accountBalance}`);
// Prompt for banking options
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to perform another withdrawal operation? Press Y for Yes and N for No: ');
performAnotherOperation ? withdraw() : displayBankingOptions();
}
// View Account Details
const viewAccountDetails = () => {
let accountNumber = readlineSync.question('Enter Account Number: ');
let account = appData.users.find(account => account.accountNumber === Number(accountNumber));
if(!account){
console.log('\n*** Account not found ***');
const tryAgain = readlineSync.keyInYNStrict('\nDo you want to try again? Press Y for Yes and N for No: ');
tryAgain ? viewAccountDetails() : displayBankingOptions();
}
console.log(`\nAccount Name: ${account.accountName} \nAccount Number: ${account.accountNumber} \nAccount Balance: ${account.accountBalance === 0 ? '0.00' : account.accountBalance} \nAccount Status: ${account.status}`);
// Prompt for banking options
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to view another account details? Press Y for Yes and N for No: ');
performAnotherOperation ? viewAccountDetails() : displayBankingOptions();
}
// View Transaction History
const viewTransactionHistory = () => {
let accountNumber = readlineSync.question('Enter Account Number: ');
let account = appData.users.find(account => account.accountNumber === Number(accountNumber));
if(!account){
console.log('\n*** Account not found ***');
const tryAgain = readlineSync.keyInYNStrict('\nDo you want to try again? Press Y for Yes and N for No: ');
tryAgain ? viewTransactionHistory() : displayBankingOptions();
}
let transactionHistory = account.transactionHistory;
if(transactionHistory.length === 0){
console.log('\n*** No transaction history found ***');
}
else{
console.log('\n*** Transaction History ***');
console.log(transactionHistory);
}
// Prompt for banking options
const performAnotherOperation = readlineSync.keyInYNStrict('\nDo you want to view another account transaction history? Press Y for Yes and N for No: ');
performAnotherOperation ? viewTransactionHistory() : displayBankingOptions();
}
displayBankingOptions();