-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
197 lines (176 loc) · 6.25 KB
/
index.ts
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
#!/usr/bin/env node
import inquirer from "inquirer";
import chalk from "chalk";
import chalkAnimation from "chalk-animation";
import figlet from "figlet";
import gradient from "gradient-string";
const sleep=(ms=1500)=>{
return new Promise((resolve,rejects)=>{
setTimeout(resolve,ms)
})
}
async function shine() {
console.clear()
const msg=`ATM M A C H I N E!!!`
figlet(msg,(err,Data)=>{
console.log(gradient.pastel.multiline(Data))
})
};
async function getStarted() {
let rainbowTitle=chalkAnimation.rainbow(`
/$$$$$$ /$$$$$$$$ /$$ /$$ /$$ /$$ /$$ /$$
/$$__ $$|__ $$__/| $$$ /$$$ | $$$ /$$$ | $$ |__/
| $$ \ $$ | $$ | $$$$ /$$$$ | $$$$ /$$$$ /$$$$$$ /$$$$$$$| $$$$$$$ /$$ /$$$$$$$ /$$$$$$
| $$$$$$$$ | $$ | $$ $$/$$ $$ | $$ $$/$$ $$ |____ $$ /$$_____/| $$__ $$| $$| $$__ $$ /$$__ $$
| $$__ $$ | $$ | $$ $$$| $$ | $$ $$$| $$ /$$$$$$$| $$ | $$ \ $$| $$| $$ \ $$| $$$$$$$$
| $$ | $$ | $$ | $$\ $ | $$ | $$\ $ | $$ /$$__ $$| $$ | $$ | $$| $$| $$ | $$| $$_____/
| $$ | $$ | $$ | $$ \/ | $$ | $$ \/ | $$| $$$$$$$| $$$$$$$| $$ | $$| $$| $$ | $$| $$$$$$$
|__/ |__/ |__/ |__/ |__/ |__/ |__/ \_______/ \_______/|__/ |__/|__/|__/ |__/ \_______/
\n`)
await sleep()
rainbowTitle.stop()
};
await getStarted()
// variable declarations
let usernames:string[] = ["Rubab","admin","F"]
let passwords:string[] = ["2704","admin","F"]
let amounts:any=["500","1000","1500","2500","3000","4000","5000",]
//chalk animation messages
let welcome_msg = "\n<== Welcome to ATM ==>\n"
let closing_msg = "\n\t<== Transaction Done ==>"
// authentication questions
const authenticate = [
{
type: "input",
name: "username",
message: "Enter User ID:",
validate: validUser
},
{
type: "password",
name: "password",
message:"Enter Password",
when:(answers:any)=>answers.username,
validate: validPassword
}
]
// transaction questions
const selectOperation = [
{
type: "list",
name: "operation",
message: "Choose an Operation",
choices:["Withdraw Cash","Deposit Cash","Check Balance","Exit"]
},
{
type:"input",
name:"withDrawAmount",
message:"Enter Amount to Withdraw Cash",
validate:numberValidation,
when:(answers2:any)=> answers2.operation === "Withdraw Cash"
},
{
type:"input",
name:"depositCash",
message:"Enter Amount to Deposit",
validate:numberValidation,
when:(answers2:any)=> answers2.operation === "Deposit Cash"
},
{
type:"list",
name:"exit",
when:(answers2:any)=> answers2.operation === "Exit",
choices:()=>process.exit() // this process can be done like "check balance"
},
]//again prompt
const again = [
{
type:"input",
name:"ask",
message:"Do You Want Another Transaction (Y|N):"
}
]
// transactions operations
async function transOperations(authnAnswers:any,operationAnswers:any){
let userIndex = usernames.indexOf(authnAnswers.username)
let currentUserAmount = parseFloat(amounts[userIndex])
let newAmount
if (operationAnswers.operation === "Check Balance"){
console.log(`${chalk.greenBright(`Current Balance`)} = ${chalk.yellowBright(currentUserAmount)}`)
}else if (operationAnswers.depositCash){
console.log(`${chalk.redBright(`Previous Balance`)} = ${chalk.yellowBright(currentUserAmount)}`)
currentUserAmount = currentUserAmount + parseFloat(operationAnswers.depositCash)
amounts[userIndex] = currentUserAmount
console.log(`${chalk.greenBright(`New Balance`)} = ${chalk.yellowBright(currentUserAmount)}`)
}else if (operationAnswers.withDrawAmount){
console.log(`${chalk.redBright(`Previous Balance`)} = ${chalk.yellowBright(currentUserAmount)}`)
let enteredAmount = parseFloat(operationAnswers.withDrawAmount)
newAmount = currentUserAmount - enteredAmount
if(newAmount<0){
console.log(chalk.redBright("Transaction Invalid !!!\nWithdraw Amount cant Exceed from Available Balance"))
}else{
amounts[userIndex] = newAmount
console.log(`${chalk.greenBright(`New Balance`)} = ${chalk.yellowBright(newAmount)}`)
}
}
}
// validate number
function numberValidation(amount:string){
if(amount){
if (isNaN(parseFloat(amount))){
return "Enter Amount in Digits"
}else{
return true
}
}else{
return "Enter some Value"
}
}
// validate user ID
function validUser(inUser:string){
if(inUser){
if(usernames.includes(inUser)){
// console.log("Congratulations")
return true
}else{
return "Username in not valid"
}
}else{
return "Enter your User ID"
}
}
// validate password w.r.t user ID
function validPassword(inPass:any,answers:any){
if(inPass){
if(inPass === passwords[usernames.indexOf(answers.username)]){
// if(inPass === "rizi"){
// return authenticate[0].name
return true
}else{
return "Password in Invalid"
}
}else{
return "Enter Your Password"
}
}
// welcome function
async function welcome(input:any){
let msg = chalkAnimation.rainbow(input)
await new Promise<void>((resolve, reject) => {
setTimeout(()=>{
msg.stop()
resolve()
},1000)
})
}
// start of ATM
async function main(){
const authnAnswers = await inquirer.prompt(authenticate)
await welcome(welcome_msg)
do{
const operationAnswers = await inquirer.prompt(selectOperation)
await transOperations(authnAnswers,operationAnswers);
var agask = await inquirer.prompt(again)
}while(agask === "Y" || agask.ask === "y")
}
main()