const rs = require("readline-sync");
//创建账户(多账户)
let users = [
"1", "1", 1000,
"2", "2", 2000,
"3", "3", 3000,
];
//判断用户是否登录成功
function islogin(ischange = true, changeIndex = 0) {
//设置登录次数
let count = 3;
//设置是否登录成功的变量
let login = false;
//登录成功账号下标
let index = 0;
while (count) { //判断3次机会是否用完
let inputName = users[changeIndex];
if (ischange) {
console.log("input your name:");
inputName = rs.question();
}
for (let i = 0; i < users.length; i++) {
if (inputName == users[i]) { //账号是否存在
console.log("input your password:");
let inputPWD = rs.question();
if (inputPWD == users[i + 1]) { //判断密码是否正确
login = true; //登录成功
index = i; //修改账号下标
// //设置下次登录的次数(恢复默认次数)
// count = 3;
return [login, index];
} else if (count == 1) {
console.log(`密码输入输入错误,您的3次机会已经用完,账号冻结!`);
count = 0;
break;
} else {
console.log(`密码输入错误,请重新输入,您还有${--count}次机会!`);
break;
}
} else if (i == users.length - 1 && count > 1) { //
console.log(`账号输入错误,请重新输入,您还有${--count}次机会!`);
ischange = true;
} else if (i == users.length - 1 && count == 1) {
console.log(i, count);
console.log(`账号输入错误,您的3次机会已经用完,账号冻结!`);
count = 0; //当前count的值是1,重新赋值为0,终止登录功能。
break;
}
}
}
return [false];
}
//存钱
function saveMoney(index) {
console.log(`请输入存入金额:`);
let saveMoney = rs.question() - 0;
if (!isNaN(saveMoney)) {
users[index + 2] += saveMoney;
console.log(`存入成功!!!`);
} else {
console.log(`输入金额有误!!!`);
}
}
//取钱
function dropMoney(index) {
console.log(`请输入取出金额:`);
let dropMoney = rs.question() - 0;
if (!isNaN(dropMoney)) {
if (dropMoney <= users[index + 2]) {
users[index + 2] -= dropMoney;
console.log(`取钱成功!!!`);
} else {
console.log(`余额不足!!`);
}
} else {
console.log(`输入金额有误!!!`);
}
}
//转账
function transMoney(index) {
console.log(`请输入转账账号:`);
let transName = rs.question();
if (transName != users[index]) {
//返回转账账号下标
let transIndex = users.indexOf(transName);
if (transIndex != -1) { //transIndex转账账号下标
console.log(`请输入转账金额:`);
let transMoney = rs.question() - 0;
if (!isNaN(transMoney)) {
if (transMoney <= users[index + 2]) {
//当前账号减钱
users[index + 2] -= transMoney;
//转帐账号加钱
users[transIndex + 2] += transMoney;
console.log(`转帐成功!!!`);
} else {
console.log(`余额不足!!`);
}
} else {
console.log(`输入金额有误!!!`);
}
} else {
console.log(`输入的${transName},不存在`);
}
} else {
console.log(`不能给自己转账!!`);
}
}
//修改密码
function changePWD(index) { //当前下标
//当前账号和密码
// console.log(users[index], users[index + 1]);
console.log(`请输入原密码:`);
let pwd = rs.question();
if (pwd == users[index + 1]) {
console.log(`请输入新密码:`);
let newPWD = rs.question();
//新密码覆盖原密码
users[index + 1] = newPWD;
//当前账号和新密码
// console.log(users[index], users[index + 1]);
return true;
} else {
console.log(`原密码输入错误!!!`);
}
return false;
}
//切换账户
function changeAccount(index) {
console.log(`请输入切换的账户`);
let changeAccount = rs.question();
//获取切换账户下标(存在获取-1的情况)
let changeIndex = users.indexOf(changeAccount);
if (changeIndex != index) {
let temp = islogin(false, changeIndex);
if (temp[0]) {
return temp[1];
}
} else {
console.log(`您已经在${users[index]}`);
}
}
//登录注册
function system() {
while (true) {
console.log(`1、登录 2、注册 0、退出`);
let choice = rs.question() - 0;
switch (choice) {
case 1:
let result = main();
if (result) {
return;
}
break;
case 2:
reg();
break;
case 0:
return;
default:
return;
}
}
}
//注册
function reg() {
while (true) {
console.log(`请输入创建账号`);
let newAccount = rs.question();
if (users.indexOf(newAccount) == -1) {
console.log(`请输入您创建账号的密码`);
let newPWD = rs.question();
users.push(newAccount, newPWD, 0);
console.log(`注册成功!`);
return true;
} else {
console.log(`账号已存在,请重新设输入`);
let option = rs.question() - 0;
if (option == 2) {
return;
}
}
}
}
function main() {
let arr = islogin(); //当前登录账号下标
if (arr[0]) { //判断登录是否成功
let index = arr[1];
console.log(`${users[index]}欢迎光临XXX银行!!!`);
//[判断用户是否选择"0"]
let state = false;
while (!state) {
console.log(`请选择功能:1、查询 2、存钱 3、取钱 4、转账 5、切换账户 6、修改密码 0、退出`);
let option = rs.question() - 0;
switch (option) {
case 1:
console.log(`您当前的余额为:${users[index + 2]}`);
break;
case 2:
saveMoney(index);
break;
case 3:
dropMoney(index);
break;
case 4:
transMoney(index);
break;
case 5:
let changeIndex = changeAccount(index);//出来就切换成功
if (changeIndex || changeIndex == 0) {
index = changeIndex;
}
break;
case 6:
let ischang = changePWD(index);
if (ischang) { //修改密码成功后,重新登录
let temp = islogin();
console.log(temp);
if (temp[0]) {
index = temp[1];
} else {
return;
}
}
break;
case 0:
console.log(`欢迎下次光临`);
state = true;
return true;
}
}
}
}
system();
2020-09-16
最新推荐文章于 2024-08-13 22:07:58 发布