用javascript編寫簡單銀行取錢存錢流程(函式)

YKmaster發表於2018-05-26
 1 const readline = require(`readline-sync`)//引用readline-sync
 2 let arr = [[`zhang`, `123`, 2000], [`yang`, `123456`, 3000]];
 3 //登陸 
 4 let add = function () {
 5     let s = 2;//輸入錯誤的次數
 6     while (true) {
 7         console.log(`請輸入使用者名稱:`);
 8         let user = readline.question();
 9         console.log(`請輸入密碼:`);
10         let password = readline.question();
11         for (let i = 0; i < arr.length; i++) {     
12             if (user === arr[i][0] && password === arr[i][1]) {
13                 console.log(`歡迎你,登陸成功`);
14                 mean(i);
15                 return;
16             }
17         }
18         //輸入不正確,超過三次
19         if (s != 0) {
20             console.log(`密碼或賬戶有誤,你還有${s}次機會,請重新輸入!`);
21             s--;
22 
23         } else {
24             console.log(`對不起,賬戶鎖定!`);
25             break;
26         }
27     }
28 }
29 //存款
30 let fn1 = function (i) {
31 
32     console.log(`存款`);
33     console.log(`請輸入存款金額:`);
34     let cunkuan = readline.question() - 0;
35     arr[i][2]+= cunkuan;
36     console.log(`系統正在為你存錢請稍等。。。。。
存錢成功您當前餘額為` + arr[i][2]);
37 }
38 //取款
39 let fn2 = function (i) {
40     console.log(`取款`);
41     console.log(`請輸入取款金額:`);
42     let qukuan = readline.question() - 0;
43     if (qukuan > arr[i][2]) {
44         console.log(`對不起你的餘額不足!,請重新輸入金額或者退出`);
45     } else {
46         arr[i][2] = arr[i][2] - qukuan;
47         console.log(`正在為你取錢。。。。。
您當前餘額為:` + arr[i][2]);
48     }
49 }
50 //檢視
51 let fn3 = function (i) {
52     console.log(`檢視`);
53     console.log(`正在查詢。。。。
您當前的餘額為:` + arr[i][2]);
54 }
55 
56 //退出
57 
58 let fn4 = function () {
59     console.log(`退出`);
60     console.log(`歡迎你下次使用!`);
61 }
62 
63 //流程
64 let mean = function (i) {
65     for (let yewu = 1; yewu >= 1; yewu++) {
66         console.log(`請選擇業務:
1.存款: 
2.取款: 
3.檢視: 
4.退出:`);
67         let yewu = readline.question() - 0;
68         switch (yewu) {
69             case 1:
70                 fn1(i);
71                 continue;
72             case 2:
73                 fn2(i);
74                 continue;
75             case 3:
76                 fn3(i);
77                 continue;
78             case 4:
79                 fn4();
80                 return;
81         }break;
82     }
83 }
84 add();

 

相關文章