-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
280 lines (233 loc) · 6.74 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
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
// npm init -y
//nm install chalk@4.1.2 inquire
//nexado script "start: node index.js" na package.json
//modulos externos
const inquirer = require('inquirer')
const chalk = require('chalk')
//modulos internos
const fs = require('fs')
operation ()
function operation() {
inquirer.prompt([
{
type: 'list',
name: 'action',
message: 'O que voce deseja fazer?',
choices : ['Criar Conta','Consultar Saldo','Depositar','Sacar','Emprestimo','Sair'],
},
])
.then((answer) =>{
const action = answer['action']
if(action === 'Criar Conta'){
createAccount()
}else{ if ( action ==='Depositar'){
deposit()
}else if(action === 'Consultar Saldo'){
getAccountBalance()
}else if(action === 'Sacar') {
widthdraw()
}else if(action === 'Emprestimo'){
loan()
}else if(action === 'Sair'){
console.log(chalk.bgBlue.black('Obrigado por usar o Accounts!'))
process.exit()
}
}
})
.catch((err) => console.log(err))
}
//CREATE ACCOUNT
function createAccount(){
console.log(chalk.bgGreen.black('Parabens por escolher o nosso banco!'))
console.log(chalk.green('Defina as opcoes da sua conta a seguir'))
buildAccount()
}
function buildAccount(){
inquirer.prompt([
{
name: 'accountName',
message: 'Digite um nome para sua conta:',
},
])
.then((answer) =>{
const accountName = answer['accountName']
console.info(accountName)
if(!fs.existsSync('accounts')){
fs.mkdirSync('accounts')
}
if (fs.existsSync(`accounts/${accountName}.json`)){
console.log(chalk.bgRed.black(`Esta conta ja existe, escolha outro nome!`),
)
buildAccount()
return
}
fs.writeFileSync(
`accounts/${accountName}.json`,
'{"balance": 0}',
function (err){
console.log(err)
},
)
console.log(chalk.green('Parabens, a sua conta foi criada'))
operation()
})
.catch((err) => console.log(err))
}
//depositar dinheoro na conta do usuario
function deposit(){
inquirer.prompt([
{
name: 'accountName',
message: 'Qual o nome da sua conta?',
},
])
.then((answer) => {
const accountName = answer['accountName']
//verificando se a conta existe - verify if account exists
if(!checkAccount(accountName)){
return deposit()
}
inquirer.prompt([
{
name: 'amount',
message: 'Quanto voce deseja depositar?',
},
]).then((answer) => {
const amount = answer['amount']
//add an amount
addAmount(accountName, amount)
operation()
})
.catch(err => console.log(err))
})
.catch(err => console.log(err))
}
function checkAccount(accountName){
if(!fs.existsSync(`accounts/${accountName}.json`)){
console.log(chalk.bgRed.black('Esta conta nao existe, escolha outro nome!'))
return false
}
return true
}
function addAmount(accountName, amount){
const accountData = getAccount(accountName)
if(!amount){
console.log('Ocorreu um erro, tente novamente mais tarde!')
return deposit()
}
accountData.balance = parseFloat(amount) + parseFloat(accountData.balance)
fs.writeFileSync(
`accounts/${accountName}.json`,
JSON.stringify(accountData),
function (err){
console.log(err)
},
)
console.log(
chalk.green(`Foi depositado o valor de R$${amount}na sua conta`),
)
}
function getAccount (accountName){
const accountJSON = fs.readFileSync(`accounts/${accountName}.json`,{
encoding: 'utf8',
flag: 'r',
})
return JSON.parse(accountJSON)
}
// show account balance
function getAccountBalance(){
inquirer.prompt([
{
name:'accountName',
message:'Qual o nome da sua conta?',
},
]).then((answer) =>{
const accountName = answer['accountName']
//verify if account exists
if (!checkAccount(accountName)){
return getAccountBalance()
}
const accountData = getAccount(accountName)
console.log(chalk.bgBlue.black(
`Ola, o saldo da sua conta e de $${accountData.balance}`
),
)
operation()
}).catch(err => console.log(err))
}
// withdraw an amount from user account
function widthdraw(){
inquirer.prompt([
{
name: 'accountName',
message: 'Qual o nome da sua conta?',
},
]).then((answer) =>{
const accountName = answer['accountName']
if(!checkAccount(accountName)){
return widthdraw()
}
inquirer.prompt([
{
name: 'amount',
message: 'Quanto voce deseja sacar?',
},
]).then((answer) => {
const amount = answer['amount']
removeAmount(accountName, amount)
})
.catch((err) => console.log(err))
})
.catch(err => console.log(err))
}
function removeAmount(accountName, amount){
const accountData = getAccount(accountName)
if(!amount){
console.log(
chalk.bgRed.black('Ocorreu um erro, tente novamete mais tarde!'),
)
return widthdraw()
}
if(accountData.balance < amount){
console.log(chalk.bgRed.black('Valor indisponivel!'))
return widthdraw
}
accountData.balance = parseFloat(accountData.balance) - parseFloat(amount)
fs.writeFileSync(
`accounts/${accountName}.json`,
JSON.stringify(accountData),
function (err) {
console.log(err)
},
)
console.log(
chalk.green(`Foi realizado um saque com de ${amount} da sua conta!`)
)
operation()
}
function loan(){
inquirer.prompt([
{
name: 'accountName',
message: 'Qual o nome da sua conta?',
},
]).then((answer) =>{
const accountName = answer['accountName']
if(!checkAccount(accountName)){
return loan()
}
inquirer.prompt([
{
name: 'amount',
message: 'Quanto voce deseja emprestado?',
},
]).then((answer) => {
const amount = answer['amount']
//add an amount
addAmount(accountName, amount)
operation()
})
.catch(err => console.log(err))
})
.catch(err => console.log(err))
}