模拟ATM机系统

const readline = require('readline-sync');

let users = [
['zhangsan', '123123', 1000],
['lisi', '000', 2000],
['1', '2', 3000]
];
// 登录
let login = function () {
let error = 3;
while (true) {
console.log('请输入用户名:');
let username = readline.question();
console.log('请输入密码:');
let password = readline.question();
for (let i = 0; i < users.length; i++) {
if (username === users[i][0] && password === users[i][1]) {
console.log('登录成功!');
menu(i);
return;
}
}
error--;
if (error <= 0) {
console.log('错误次数超过三次,登录失败!再见!');
break;
} else {
console.log(`账号或密码错误,你还有${error}次机会,请重新登录。`);
}
}
}
// 存钱
let saveMoney = function (i) {
while (true) {
console.log('请输入存款金额:');
let money = readline.question() - 0;
users[i][2] += money;
console.log(`存款金额为${money},当前账户余额为${users[i][2]}。`);
console.log('继续存款请按1,返回主菜单请按2。');
let option = readline.question();
if (option === '2') {
break;
}
}
}
// 取钱
let getMoney = function (i) {
while (true) {
console.log('请输入取款金额:');
let money = readline.question() - 0;
users[i][2] -= money;
console.log(`取款金额为${money},当前账户余额为${users[i][2]}。`);
console.log('继续取款请按1,返回主菜单请按2。');
let option = readline.question();
if (option === '2') {
break;
}
}
}
// 主流程
let menu = function (i) {
while (true) {
console.log('请选择业务:1.存款 2.取款 3.查询 4.退出');
let option = readline.question() - 0;
switch (option) {
case 1: saveMoney(i); break;
case 2: getMoney(i); break;
case 3: console.log(`当前账户余额为${users[i][2]}。`); break;
case 4: console.log('谢谢使用,再见!'); return;
}
}
}

login();
posted on 2018-05-27 16:46  小霞blogs  阅读(146)  评论(0编辑  收藏  举报