js制作一个伪银行取款atm机

徘徊边缘 提交于 2021-01-26 08:24:27
运行环境node.js
 
const readline = require('readline-sync');
let num = 0;
let users = [['liuwujiang', '659'], ['yingyingying', '111'], ['1', '1']];
let isOk = true;
let userName, password;
while (true) { //判断是否退出
while (true) { //判断登录是否成功
console.log('1' + '.' + ' ' + '登录');
console.log('2' + '.' + ' ' + '注册');
console.log('请选择(1-2):')
let sel = readline.question();
if (sel === '1') {
console.log("请输入用户名:");
userName = readline.question();
console.log("请输入密码:");
password = readline.question();

let islogin = false;
for (let i = 0; i < users.length; i++) {
if (users[i][0] == userName && users[i][1] == password) { //判断数组users里是否有匹配数据
console.log('登录成功,欢迎您');
islogin = true;

}
}

if (islogin == false) {
num++; //记录登录错误次数
if (num >= 3) {
console.log('密码错误三次,锁卡~嗝~~');
isOk = false;
break;
}
else {
isOk = false;
console.log('登录失败,请重新登录');
}



}
else {
isOk = true;

}
}

else if (sel === '2') {
console.log("请输入用户名:");
let userName1 = readline.question();
console.log("请输入密码:");
let password2 = readline.question();
let repeatUserName = false; //用于判断用户名是否重复
for (let i = 0; i < users.length; i++) {
if (users[i][0] == userName1) { //判断用户名是否重复
console.log('已存在用户名,请重新注册');
repeatUserName = true;

}
}
if (repeatUserName == false) {
users.push([userName1, password2]);
console.log('注册成功,欢迎您');
}
isOk = false; //返回循环



}
else
console.log('输入错误,请输入1-2之间的数');

if (isOk) { //判断上面所有信息是否正确,是则终止循环
break;
}
}
if (isOk == false) //判断是否锁卡,如果已锁,则终止整个程序
{
break;
}
let saveMoney = 0, takeMoney = 0, smile = 0, getMoney, tempMoney;
while (true) { //根据需求重复选择
console.log('1' + '.' + " " + "存款");
console.log('2' + '.' + " " + "取款");
console.log('3' + '.' + " " + "查看余额");
console.log('4' + '.' + " " + "修改密码");
console.log('5' + '.' + " " + "退出");
console.log("请选择(1-5):");
let changepsdOk = true;
let sel2 = readline.question();
switch (sel2) {
case '1': {

console.log("请输入存款金额(元):");
getMoney = readline.question();
saveMoney += getMoney - 0;
if (getMoney > 10000) { //判断存款是否>10000,滑稽~
console.log('土豪,我们做朋友吧!')
}
break;
}

case '2': {
console.log("请选择取款金额(元):");
takeMoney = readline.question();
tempMoney = saveMoney; //临时存储存款,方便取款>存款时重新赋值
saveMoney -= takeMoney;
if (takeMoney > 5000) {
console.log('一次最多只能取5000元哦,请重新选择');
break;
}
else {
if (saveMoney < 0) { //判断取款后余额是否<0
smile++; //记录取款额>存款次数
if (smile == 2) {
console.log('别乱摁了,这不是你的幸运数字');
saveMoney = tempMoney;
break;
}
else if (smile == 3) {
console.log('啧啧啧,承认自己穷就那么难吗←_←');
saveMoney = tempMoney;
break;
}
else if (smile > 3) {
console.log('算了,你开心就好-_-\'');
saveMoney = tempMoney;
break;
}

else {
console.log('你有多少钱你心里没点那啥吗?');
saveMoney = tempMoney;
console.log('你的余额为:' + tempMoney + '元');
break;
}
}
}
}
case '3': {


console.log('您的余额为' + saveMoney + '元');
break;

}
case '4': {
while (true) {
console.log('请输入修改的密码:')
let changepsw = readline.question();
console.log('请确认修改的密码:')
let truepsw = readline.question();
if (changepsw == truepsw) {
for (let i = 0; i < users.length; i++) {
if (users[i][0] == userName) {
users[i][1] = changepsw;
console.log('修改密码成功,请重新登录');
changepsdOk = true;

}
}
}
else {
console.log('两次密码输入不一致,请重新输入');
changepsdOk = false;
readline.question();

}
if (changepsdOk) { //判断密码修改是否成功,成功则终止当前while循环
break;
}
}
}
case '5': {
isOk = false;
break;
}
default: {
console.log('输入错误,请输入1-5之间的数');
}
if (changepsdOk == true) { //判断密码修改是否成功,成功则终止当前while循环,重新登录
break;
}
}
readline.question();
if (!isOk) { //判断是否选择4.退出
break;
}
}


}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!