-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (61 loc) · 1.52 KB
/
app.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
var readline = require('readline');
var items = require('./items.json');
let order = {
item: 0,
qty: 0
}
let billing_amount = 0;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const question1 = () => {
return new Promise((resolve, reject) => {
var ques = '';
items.map(function(item){
ques += item.id+') '+item.name+' - '+item.price+' rupees/item\n';
})
rl.question(`Hey there, We have the following items in our shop.\n${ques}What do you want to purchase today?`, (answer) => {
order.item = answer;
resolve()
})
})
}
const question2 = () => {
return new Promise((resolve, reject) => {
rl.question('How many? ', (answer) => {
order.qty = answer;
resolve()
})
})
}
const question3 = () => {
return new Promise((resolve, reject) => {
rl.question('Anything else? (Yes/No)', (answer) => {
calculateBilling()
if(answer === 'Yes' || answer === 'yes' ){
main()
} else {
resolve()
console.log('*******************************');
console.log(`Calculating your bill...\n`);
console.log(`Your bill is ${billing_amount} rupees`);
console.log('*******************************');
}
})
})
}
const calculateBilling = () => {
let choosenItem = items.filter(function(item){
if(item.id == order.item)
return item
})
billing_amount += choosenItem[0].price * order.qty;
}
const main = async () => {
await question1()
await question2()
await question3()
rl.close()
}
main()