<script type="text/javascript">
let cards = [
{ "cardNo": "10001", "pwd": "123456", money: 1000000000 },
{ "cardNo": "10002", "pwd": "123456", money: 1000000000 },
{ "cardNo": "10003", "pwd": "123456", money: 1000000000 }]
let loginIndex = -1; //登录的账号信息,存储取应的下标,-1表示未登录
//登录
Login();
function Login() {
//是否登录成功
let card, pwd;
for (let count = 0; count < 3; count++) {
card = prompt('请输入账号:');
pwd = prompt('请输入密码');
if (isLoginSucess(card, pwd)) {
//成功
Main();
return;
} else {
alert('账号或密码出错,请重新输入')
}
}
}
function isLoginSucess(card, pwd) {
//循环卡号和密码,看是否有相等
for (let i = 0; i < cards.length; i++) {
if (cards[i].cardNo == card && cards[i].pwd == pwd) {
loginIndex = i;
return true;
}
}
return false;
}
//主界面
function Main() {
//alert('欢迎登录个人银行')
let choose = Number(prompt(`
欢迎登录私人银行
1.查询 2.存款
3.取款 4.转账
0.退出
`))
switch (choose) {
case 1: QueryMoney(); Main(); break;
case 2: DepositMoney(); Main(); break;
case 3: WithdrawMoney(); Main(); break;
case 4: TransferAccount(); Main(); break;
case 0: Exit(); break;
default:
alert('请输入正确的选项');
Main();
}
}
//查询
function QueryMoney() {
if (loginIndex == -1) {
alert('请先登录');
Login();
return;
}
alert('您当前的余额为:' + cards[loginIndex].money);
}
//存款
function DepositMoney() {
if (loginIndex == -1) {
alert('请先登录');
Login();
return;
}
let money = Number(prompt('请输入你要存款的金额'));
cards[loginIndex].money += money;
alert('存款成功,您当前余额为:' + cards[loginIndex].money);
}
//取款
function WithdrawMoney() {
if (loginIndex == -1) {
alert('请先登录');
Login();
return;
}
let money = Number(prompt('请输入你要取款的金额'));
cards[loginIndex].money -= money;
if (cards[loginIndex].money < money) {
alert('余额不足');
return;
alert('取款成功,您当前余额为:' + cards[loginIndex].money);
}
}
//转账
function TransferAccount() {
if (loginIndex == -1) {
alert('请先登录');
Login();
return;
}
let zhuanCard = prompt('请输入需要转账的卡号');
//判断转账卡号是否正确
let zhuanIndex = -1;
for (var i = 0; i<cards.length; i++) {
if (cards[i].cardNo === zhuanCard) {
zhuanIndex = i;
break;
}
}
//提示卡号不存在
if (zhuanIndex == -1) {
alert('转账卡号有问题,请重新输入');
TransferAccount();
return;
}
//提示输入转账金额
let money = Number(prompt('请输入转账金额'));
if (cards[loginIndex].money < money) {
alert('余额不足');
return;
}
//转账用户减钱
//被转用户加钱
//事务,要么全部成功,要么全部失败(回滚) - 数据库
cards[loginIndex].money -= money;
cards[zhuanIndex].money += money;
alert('转账成功,您当前余额为:' + cards[loginIndex].money);
}
//退出
function Exit() {
loginIndex = -1;
// Login();
}
</script>
一、登陆
![](https://i-blog.csdnimg.cn/blog_migrate/7b111446b903355c6697f96a57110de3.png)
二、主界面
![](https://i-blog.csdnimg.cn/blog_migrate/4a22392a108d41d31c960e0f42517278.png)
查询、存款项
![](https://i-blog.csdnimg.cn/blog_migrate/eb66e8041e45cf1a407d3b7c03ce730f.png)
三、取款项
![](https://i-blog.csdnimg.cn/blog_migrate/90d26d147cae956f9da3aa100691116f.png)
四、转账项
![](https://i-blog.csdnimg.cn/blog_migrate/361a9bb91c9a9597a45784f847cc5ea5.png)
五、退出
![](https://i-blog.csdnimg.cn/blog_migrate/142c2099751f2410ae47e43f4dece3e4.png)