ATM机(函数版)

ATM机(函数版)

ATM机运用函数控制
ATM(函数版高级)

//ATM机器函数版
const re = require('readline-sync');
let arr = [
    'z1', '1', 100,
    'z2', '2', 200,
    'z3', '3', 300
];


//主程序
function atm() {
    login();
}
//登录
function login() {
    let count = 3;
    let state = true;
    let username, pwd;  //f控制业务选择的循环
    while (count) {//控制登录次数
        console.log('请输入用户名:');
        username = re.question();
        // let username = '3';
        for (let index = 0; index < arr.length; index++) {
            if (username == arr[index] && (index % 3 == 0 || index == 0)) {
                console.log('请输入密码:');
                pwd = re.question();
                // let pwd = '2';
                if (pwd == arr[index + 1]) {
                    console.log(`${username}登录成功,余额为${arr[index + 2]}`);
                    state = true;
                    option(index);
                    count -= 3;
                }
                else {
                    count--;
                    if (count == 0) {
                        console.log('多次输入错误,请联系客服');
                    } else {
                        console.log(`密码错误,你还有${count}次机会`);
                    }
                    state = false;
                }
            } else {
                index += 2;
            }
        }

    }

}


//选项
function option(index) {
    let state = true;
    while (state) {
        console.log(`${arr[index]}用户,请选择操作:1:存钱,2:取钱,3:查询余额,4:改密码,5:转账,6:切换账户,0:退出`);
        let num = re.question() - 0;
        switch (num) {
            case 1:
                saveMoney(index);  //存钱
                break;
            case 2:
                drawMoney(index);  //取钱
                break;
            case 3:
                balance(index);    //余额
                break;
            case 4:
                changepwd(index);  //改密码
                return 0;
            case 5:
                transaccount(index);  //转账
                return 0;
            case 6:
                switchaccount(index);   //切换账户
                return 0;
            case 0:
                state = false;
                console.log("欢迎下次光临");
                // index=arr.length-1;
                break;
            default:
                console.log('你输入的数字不是查询业务,请重新输入!!');
        }
    }
}
//存钱
function saveMoney(index) {
    console.log('请输入存钱金额:');
    let jin = re.question() - 0;
    arr[index + 2] += jin;
    console.log(`存钱成功`);
}
//取钱
function drawMoney(index) {
    console.log('请输入取钱金额:');
    let save = re.question() - 0;
    if (save <= arr[index + 2]) {
        arr[index + 2] -= save;
        console.log(`取钱成功`);
    } else {
        console.log(`余额不足`);
    }
}
//余额
function balance(index) {
    console.log(`余额为:${arr[index + 2]}`);
}
//改密码
function changepwd(index) {
    console.log("请输入旧密码:");
    let originalpassword = re.question();   // originalpassword 原密码的输入
    if (arr[index + 1] == originalpassword) {
        console.log("旧密码正确,请输入新密码");
        let chpwd = re.question();
        arr[index + 1] = chpwd;
        // console.log(chpwd);
        console.log("请重新登录");
        login(index);
        return 0;
    }
    else {

        console.log(`输入的旧密码错误`);
    }
}
//转账
function transaccount(index) {
    console.log("请输入你要转账的账户");
    let newaccount = re.question();  //获取转账的账户
    if (arr.indexOf(newaccount) != -1) {
        let newacc = arr.indexOf(newaccount);  //获取转账帐号的下标
        console.log(`账户存在${arr[newacc]},请输入要转账的金额:`);
        let newmoney = re.question() - 0;
        if (newmoney <= arr[index + 2]) {
            arr[index + 2] -= newmoney;
            arr[newacc + 2] += newmoney;
            console.log("转账成功");
            option(index);
            return 0;
        } else {
            console.log("余额不足");
            option(index);
            return 0;
        }
    } else {
        console.log("你输入的账户不存在");
        option(index);
        return 0;
    }
}
//切换账户
function switchaccount(acc) {
    console.log("请输入你要切换的账户");
    let newaccount = re.question();//切换的账户用户名
    // for(acc = 0;acc<arr.length;acc++){
    if (arr.indexOf(newaccount) != -1) {
        acc = arr.indexOf(newaccount);
        console.log(`输入的账户${newaccount}存在,请输入它的密码:${arr[acc]}`);
        let newpwd = re.question();
        if (newpwd == arr[acc + 1]) {
            console.log(`输入正确,请选择是否跳转1:确定,2:取消`);
            let move = re.question() - 0;
            switch (move) {
                case 1:
                    option(acc);
                    return 0;
                case 2:
                    break;
                default:
                    break;
            }
        } else {
            console.log(`错误,密码是:${arr[acc + 1]}`);
        }
    } else {
        console.log(`输入的账户不存在${arr[acc]}`);
    }
}

// }
atm();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值