const rs = require("readline-sync");
let users = user();
console.log(`欢迎光临老八银行!!!`);
function user() {
users1 = [{
name: "1",
pwd: "1",
banlace: 1000
},
{
name: "2",
pwd: "2",
banlace: 2000
},
{
name: "3",
pwd: "3",
banlace: 3000
}
]
return users1;
}
function saveMoney(index) {
console.log(`请输入存钱金额`);
let saveMoeny = rs.question() - 0;
if (!isNaN(saveMoeny)) {
users[index].banlace += saveMoeny;
console.log(`存款成功!您的余额为${ users[index].banlace }元`);
} else {
console.log(`请输入正确数字`);
}
}
function dropMoney(index) {
console.log(`请输入取钱金额`);
let dropMoney = rs.question() - 0;
if (dropMoney <= users[index].banlace && dropMoney > 0 && dropMoney % 100 == 0) {
users[index].banlace -= dropMoney;
console.log(`取款成功!您的余额为${users[index].banlace}元`);
} else if (dropMoney < 0) {
console.log(`请输入正数`);
} else if (isNaN(dropMoney)) {
console.log(`请输入数字`);
} else if (dropMoney % 100 !== 0) {
console.log(`请输入100的倍数`);
} else {
console.log(`余额不足`);
}
}
function tranMoney(index) {
console.log(`请输入转账用户名`);
let tranName = rs.question();
for (j = 0; j < users.length; j++) {
if (tranName != users[index].name) {
if (tranName == users[j].name) {
console.log(`请输入转账金额`);
let tranMoney = rs.question() - 0;
if (tranMoney <= users[index].banlace && tranMoney > 0 && tranMoney % 100 == 0) {
console.log(`转账成功`);
users[index].banlace -= tranMoney;
console.log(`您的余额为${users[index].banlace}元`);
users[j].banlace += tranMoney;
console.log(`您转账账户的余额为${users[j].banlace}元 不是吧???这也能获取`);
break;
} else if (tranMoney < 0) {
console.log(`请输入正数`);
} else if (isNaN(tranMoney)) {
console.log(`请输入数字`);
} else if (tranMoney % 100 !== 0) {
console.log(`请输入100的倍数`);
break;
} else {
console.log(`余额不足`);
break;
}
} else if (j == users.length - 1) {
console.log(`账户不存在,请重新输入`);
break;
}
} else {
console.log(`不能给自己转账!!`);
break;
}
}
}
function qieHuanAccount(index) {
console.log(`请输入切换的账户`);
let changeAccount = rs.question();
if (changeAccount != users[index].name) {
for (i = 0; i < users.length; i++) {
if (changeAccount == users[i].name) {
let temp = account();
if (temp[0]) {
return temp[1];
}
} else if (i == users.length - 1) {
console.log(`账号不存在`);
}
}
} else {
console.log(`您已经在本账户`);
}
}
function changePWD(index) {
console.log(`请输入原密码`);
let pwd = rs.question();
if (pwd == users[index].pwd) {
console.log(`请输入新密码`);
let newPWD = rs.question();
users[index].pwd = newPWD;
console.log(`密码已修改,请重新登录`);
return true;
} else {
console.log(`密码不正确`);
}
}
function account() {
for (let count = 3; count > 0; count--) {
console.log(`请输入账号`);
let inputAccount = rs.question();
console.log(`请输入密码`);
let inputPWD = rs.question();
for (let i = 0; i < users.length; i++) {
if (inputAccount == users[i].name) {
if (inputPWD == users[i].pwd) {
console.log(`登录成功,欢迎${users[i].name}用户`);
login = true;
count = 3;
let index = i;
return [login, index];
} else if (count == 1) {
console.log(`密码错误!三次机会已用完!账号被冻结`);
break;
} else {
console.log(`密码错误!请重新输入,您还有${count-1}次机会`);
break;
}
} else if (count == 1 && i == users.length - 1) {
console.log(`账号不存在!三次机会已用完!账号被冻结`);
count = -1;
return false;
} else if (i == users.length - 1) {
console.log(`账号不存在!请重新输入,您还有${count-1}次机会`);
}
}
}
}
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();
for (let k = 0; k < users.length; k++) {
if (newAccount == users[k].name) {
console.log(`账号已存在,请重新设输入`);
return;
} else if (k == users.length - 1) {
console.log(`请输入您创建账号的密码`);
let newPWD = rs.question();
users.push({
name: newAccount,
pwd: newPWD,
banlace: 0,
});
console.log(`注册成功!`);
return true;
}
}
}
}
function islogin() {
let arr = account();
if (arr[0]) {
while (arr[0]) {
console.log(`请输入以下选项1、查询余额 2、存钱 3、取钱 4、转账 5、切换账户 6、修改密码 0、退出`);
let Num = rs.question() - 0;
switch (Num) {
case 0: {
console.log(`欢迎下次存钱`);
return false;
}
case 1: {
console.log(`您的余额为${users[arr[1]].banlace}元`);
break;
}
case 2: {
saveMoney(arr[1]);
break;
}
case 3: {
dropMoney(arr[1]);
break;
}
case 4: {
tranMoney(arr[1]);
break;
}
case 5: {
let changeIndex = qieHuanAccount(arr[1]);
if (changeIndex || changeIndex == 0) {
arr[1] = changeIndex;
}
break;
}
case 6: {
let ischange = changePWD(arr[1]);
if (ischange) {
account();
}
break;
}
default: {
console.log(`请输入正确的数字`);
}
}
}
}
}
function main() {
user();
islogin();
}
system();