JavaScript模拟ATM业务

Posted 爱吃鱼的猫#

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript模拟ATM业务相关的知识,希望对你有一定的参考价值。

const readline = require("readline-sync");
let time = 0; //输密码次数
let x = 1; //临时变量
console.log("请输入您的密码");
let password = readline.question() - 0;
while (password != 123456) {
    console.log("您输入的密码有误,请重新输入");
    password = readline.question() - 0;
    time++;
    if (time == 2) {
        console.log("密码三次错误,卡已被锁定");
        break;
    }
}
//以上为确认密码正确
if(password == 123456){
    let balance = 2000; //默认卡内余额为两千
    while (x == 1) {
        console.log("请选择业务");
        console.log("1.存款");
        console.log("2.取款");
        console.log("3.查看");
        console.log("4.退出");
        let check = readline.question() - 0;
        while (isNaN(check) || check < 1 || check > 4) {
            console.log("请重输");
            check = readline.question() - 0;
        }
        if (check == 4) {
            console.log("感谢使用,再见");
            break;
        }
        if (check == 3) {
            console.log(`银行卡余额为${balance}元`);
            console.log("退出请按1,返回主菜单请按2");
            let get = readline.question() - 0;
            while (isNaN(get) || get < 1 || get > 2) {
                console.log("请重输");
                get = readline.question() - 0;
            }
            if (get == 1) {
                console.log("感谢使用,再见");
                x++;
            }
        }
        while (check == 2) {
            if (balance == 0) {
                console.log("请注意,卡里没有钱啦");
                console.log("退出请按1,返回主菜单请按2");
                let choice2 = readline.question()-0;
                while (isNaN(choice2) || choice2 < 1 || choice2 > 2) {
                    console.log("请重输");
                    choice2 = readline.question() - 0;
                }
                if (choice2 == 1) {
                    console.log("感谢使用,再见");
                    x++;
                    break;
                }else{
                    break;
                }
            }
            console.log("请输入取款金额:");
            let drawmoney = readline.question()-0;
            while (isNaN(drawmoney) || drawmoney < 0 || drawmoney > 20000) {
                console.log("单笔取款不得超过两万,请输入正确的数字");
                drawmoney = readline.question() - 0;
            }
            let h =balance;//临时变量
            while (drawmoney > h) {
                console.log("取款金额超过余额,请重新输入");
                drawmoney = readline.question()-0;
            }
            balance -= drawmoney;        
            console.log(`取款金额为${drawmoney}元,卡内剩余${balance}元`);
            console.log("退出请按1,继续取款请按2,返回主菜单请按3");
            let choice1 = readline.question() - 0;
            while (isNaN(choice1) || choice1 < 1 || choice1 > 3) {
                console.log("请重输");
                choice1 = readline.question() - 0;
            }
            if (choice1 == 1) {
                console.log("感谢使用,再见");
                x++;
                break;
            }else if(choice1 == 2){
                check == 2;
            }else{
                break;
            }
        }
        while (check == 1) {
            console.log("请输入存入金额:");
            let savemoney = readline.question()-0;
            while (isNaN(savemoney) || savemoney < 0 || savemoney > 50000) {
                console.log("单笔存款不得超过五万,请输入正确的数字");
                savemoney = readline.question() - 0;
            }
            balance += savemoney;
            console.log(`存入金额为${savemoney}元,卡内剩余${balance}元`);
            console.log("退出请按1,继续存款请按2,返回主菜单请按3");
            let choice = readline.question() - 0;
            while (isNaN(choice) || choice < 1 || choice > 3) {
                console.log("请重输");
                choice = readline.question() - 0;
            }
            if (choice == 1) {
                console.log("感谢使用,再见");
                x++;
                break;
            }else if(choice == 2){
                check == 1;
            }else{
                break;
            }
        }
    }
}

 

以上是关于JavaScript模拟ATM业务的主要内容,如果未能解决你的问题,请参考以下文章

一个用java模拟ATM机的代码且数据库为mysql,

C代码和python代码:模拟ATM(自动柜员机)界面

模拟ATM机银行系统

语言模拟ATM自动取款机系统

ATM

java ATM机 数据库