模拟ATM机系统
Posted 小霞blogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟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();
以上是关于模拟ATM机系统的主要内容,如果未能解决你的问题,请参考以下文章