-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitchExample.js
More file actions
68 lines (64 loc) · 2.29 KB
/
switchExample.js
File metadata and controls
68 lines (64 loc) · 2.29 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
const readline = require("readline");
const input = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Simple in-memory accounts database for demo
const accounts = {
"12345": { pin: "1111", balance: 2000.0, name: "Chinwe" },
"67890": { pin: "2222", balance: 2555.5, name: "Oracle" }
};
// Prompt user for their ATM details
input.question("Enter your account number: ", (accountNumber) => {
input.question("Enter your PIN: ", (pin) => {
const account = accounts[accountNumber];
if (!account) {
console.log("Account not found.");
input.close();
return;
}
if (account.pin !== pin) {
console.log("Incorrect PIN.");
input.close();
return;
}
console.log(`Welcome, ${account.name}!`);
atmMachine(account);
});
});
function atmMachine(account) {
function showMenu() {
input.question("\nChoose an option:\n1) Check balance\n2) Withdraw\n3) Exit\nEnter choice: ", (choice) => {
switch (choice.trim()) {
case "1":
console.log(`Your balance: $${account.balance.toFixed(2)}`);
showMenu();
break;
case "2":
input.question("Enter amount to withdraw: ", (amtStr) => {
const amt = parseFloat(amtStr);
if (isNaN(amt) || amt <= 0) {
console.log("Invalid amount.");
showMenu();
} else if (amt > account.balance) {
console.log("Insufficient funds.");
showMenu();
} else {
account.balance -= amt;
console.log(`Success! New balance: $${account.balance.toFixed(2)}`);
showMenu();
}
});
break;
case "3":
console.log("Thank you. Goodbye.");
input.close();
break;
default:
console.log("Invalid choice.");
showMenu();
}
});
}
showMenu();
}