-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (167 loc) · 4.51 KB
/
index.js
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
#!/usr/bin/env node
const inquirer = require('inquirer');
const boxen = require('boxen');
const chalk = require('chalk');
const userCredentials = [
{ ID: 1, username: 'sags', fName: 'Oliver', password: '1234', balance: 2000 },
{ ID: 2, username: 'ace', fName: 'James', password: '0000', balance: 1500 },
{ ID: 3, username: 'jane', fName: 'Jane', password: '1212', balance: 5000 },
];
const userLogin = {
type: 'input',
name: 'username',
message: 'Enter your username:',
};
const userPassword = {
type: 'password',
name: 'password',
message: 'Enter your password',
mask: '*',
};
const transactions = {
type: 'list',
name: 'transaction',
message: 'What would you like to do?',
choices: ['Check balance', 'Deposit', 'Withdraw'],
};
const continueTransaction = {
type: 'list',
name: 'proceed',
message: 'Would you like to continue?',
choices: ['Yes', 'No'],
};
const deposit = {
type: 'input',
name: 'deposit',
message: 'Enter amount to deposit',
};
const withdraw = {
type: 'input',
name: 'withdraw',
message: 'Enter amount to withdraw',
};
checkUsername = () => {
inquirer.prompt(userLogin).then(({ username }) => {
const users = userCredentials.filter((user) => {
if (username == user.username) {
return user;
}
});
if (users.length === 0) {
console.log(`\n\t${chalk.red('Account not found')}. Try again\n`);
checkUsername();
return;
}
console.log(chalk.green('\n\tSuccess!\n'));
checkPassword(users);
});
};
checkPassword = ([users]) => {
inquirer.prompt(userPassword).then(({ password }) => {
if (password == users.password) {
console.log(chalk.green('\n\tSuccess!\n'));
setTimeout(() => {
console.log(
`\tWelcome to our bank ${users.fName}, you have Ksh.${users.balance} in your account\n`
);
transact(users);
}, 500);
return;
}
console.log(
`\n\t${chalk.red(
'Incorrect password'
)}. Enter your username and try again\n`
);
checkUsername();
});
};
transact = (users) => {
inquirer.prompt(transactions).then(({ transaction }) => {
if (transaction == 'Deposit') {
depositTransaction(users);
return;
}
if (transaction == 'Withdraw') {
withdrawTransaction(users);
return;
}
checkBalance(users);
});
};
checkBalance = (users) => {
console.log(chalk.blue('\n\tChecking balance. Please wait...\n'));
setTimeout(() => {
console.log(
boxen(
`${chalk.green.bold(
'Transaction successful!'
)}\n\nYour account balance is Ksh.${users.balance}`,
{ padding: 1 }
)
);
transactionContinue(users);
}, 1500);
};
depositTransaction = (users) => {
let { balance } = users;
inquirer.prompt(deposit).then(({ deposit }) => {
deposit = parseInt(deposit, 10);
balance += deposit;
console.log(chalk.blue('\n\tDepositing amount. Please wait...\n'));
setTimeout(() => {
console.log(
boxen(
`${chalk.green.bold(
'Transaction successful!'
)}\n\nKsh.${deposit} has been deposited to your account. Current balance is Ksh.${balance}`,
{ padding: 1 }
)
);
users.balance = balance;
transactionContinue(users);
}, 1500);
});
};
withdrawTransaction = (users) => {
let { balance } = users;
inquirer.prompt(withdraw).then(({ withdraw }) => {
withdraw = parseInt(withdraw, 10);
console.log(chalk.blue('\n\tWithdrawing amount. Please wait...\n'));
setTimeout(() => {
if (withdraw > balance) {
console.log(
boxen(
`${chalk.bold.red(
'Transaction failed!'
)}\n\nYou do not have enough money to withdraw Ksh.${withdraw}. Current balance is Ksh.${balance}`,
{ padding: 1 }
)
);
withdrawTransaction(users);
return;
}
balance -= withdraw;
console.log(
boxen(
`${chalk.green.bold(
'Transaction successful!'
)}\n\nKsh.${withdraw} has been withdrawn from your account. Current balance is Ksh.${balance}`,
{ padding: 1 }
)
);
users.balance = balance;
transactionContinue(users);
}, 1500);
});
};
transactionContinue = (users) => {
inquirer.prompt(continueTransaction).then(({ proceed }) => {
if (proceed === 'Yes') transact(users);
else quitTransaction();
});
};
quitTransaction = () => {
console.log(chalk.cyan.bold(`\n\tThank you for banking with us. Goodbye`));
};
checkUsername();