ATM机对象版

const readline = require('readline-sync');
//账户数据
let accArr = [
    { name: "z3", pwd: "123", balance: 1000 },
    { name: "l4", pwd: "234", balance: 1500 },
    { name: "qzw", pwd: "777", balance: 2000 }
]


//主程序

function atm() {
    //状态值
    let state = true;
    //次数
    let count = 3;
    while (state) {
        console.log('请选择:1.登录 2.注册 0.退出系统');
        let choice = readline.question() - 0;
        switch (choice) {
            case 0:
                //退出系统
                state = false;
                console.log('退出成功!欢迎下次光临!');
                break;
            case 1:
                //登录
                //接收登录的返回值(即用户选择退出,退出主程序)
                state = login(count, state);
                break;
            case 2:
                //注册
                reg();
                break;
            //输入其他就输入错误!
            default:
                console.log('输入错误!!');
        }
    }
}

//判断账号是否存在
function searchname(inputname) {

    for (let index = 0; index < accArr.length; index++) {
        if (inputname == accArr[index].name) {
            //如果输入的账户名等于账户数据就返回下标index
            return index;
        }
    }
    账号不存在则返回false
    return false;
}

//注册
function reg() {
    console.log('请输入注册的账号名');
    let newname = readline.question();
    //判断注册账号是否存在
    if (searchname(newname) === false) {
        console.log('请设置密码');
        let newpwd = readline.question();
        console.log('请确定密码');
        let compwd = readline.question();
        //判断2次密码是否一致
        if (newpwd == compwd) {
            console.log(`注册成功!当前用户名为${newname}`);
            //添加到存储账号的数组
            accArr.push({ name: newname, pwd: newpwd, balance: 0 });
        } else {
            console.log('两次密码不一致!请重新输入');
        }
    } else {
        console.log(`注册失败!当前${newname}已被注册!`);
    }

}

//登录
function login(count, state) {
    //输入账号不限次数
    while (state) {
        console.log('请输入账号');
        let inputname = readline.question();
        //获取账号的下标
        let index = searchname(inputname);
        //判断账号是否存在(index为0表示第一个账号的下标)
        if (index || index === 0) {
            //密码3次机会
            while (count) {
                console.log('请输入密码');
                let inputpwd = readline.question();
                //判断密码是否等于账户数据
                if (inputpwd == accArr[index].pwd) {
                    console.log(`登陆成功!欢迎${inputname}光临中国银行`);
                    //功能选项的调用
                    //index成功登录账号的下标
                    //state状态值
                    return option(state, index);
                } else {
                    count--;
                    if (count == 0) {
                        console.log(`3次机会用完,账号已冻结!!!`);
                        //三次机会用完就跳出循环
                        state = false;
                    } else {
                        console.log(`密码错误,还有${count}次机会`);
                    }
                }
            }
        } else {
            console.log(`你输入的${inputname}不存在`);
        }
    }
}

//功能选项
function option(state, index) {
    while (state) {
        console.log('请选择业务:1.查询余额 2.存钱 3.取钱 4.修改密码 5.切换用户 6.转账 0.退出系统');
        let option = readline.question() - 0;
        switch (option) {
            case 0:
                console.log('退出成功!');
                //退出成功结束循环
                state = false;
                break;

            case 1:
                //查询余额
                serchmoney(index);
                break;

            case 2:
                //存钱
                savemoney(index);
                break;

            case 3:
                //取钱
                drawmoney(index);
                break;

            case 4:
                //修改密码
                if (changepwd(index)) {
                    return true;
                }
                break;

            case 5:
                //切换用户
                let changeindex = changename(index);
                if (changeindex || changeindex == 0) {
                    //把切换成功的下标复制给的index
                    //即修改当前下标
                    index = changeindex;
                }
                break;

            case 6:
                //转账
                transmoney(index);
                break;
        }
    }
}

//查询余额
function serchmoney(index) {
    console.log(`当前余额为${accArr[index].balance}元`);
}

//存钱
function savemoney(index) {
    console.log('请输入你存钱的金额');
    let savemoney = readline.question() - 0;
    accArr[index].balance += savemoney;
    console.log(`存钱成功!当前金额为${accArr[index].balance}`);
}

//取钱
function drawmoney(index) {
    console.log('请输入你要取钱的金额');
    let drawmoney = readline.question() - 0;
    //判断取钱金额是否小于账户数据金额
    if (drawmoney <= accArr[index].balance) {
        accArr[index].balance -= drawmoney;
        console.log(`取钱成功!当前金额为${accArr[index].balance}`);
        //超出账户数据金额就取钱失败
    } else {
        console.log('取钱失败!金额不足!请重新选择');
    }
}

//修改密码
function changepwd(index) {
    console.log('请输入原密码');
    let oldpwd = readline.question();
    //判断原密码是否等于账户数据密码
    if (oldpwd == accArr[index].pwd) {
        console.log('密码正确!请输入新密码');
        let newpwd = readline.question();
        console.log('请确认密码');
        let compwd = readline.question();
        //判断两次密码是否一致
        if (newpwd == compwd) {
            //把确定密码赋值给新密码
            accArr[index].pwd = newpwd;
            console.log('修改成功!');
              //修改成功后退出到主程序,返回true
            return true;
        } else {
            console.log('两次密码不一致!请重新输入!');
        }
    } else {
        console.log('原密码不正确!请重新输入');
        //修改不成功后,返回false
        return false;
    }

}

//切换用户
function changename(index) {
    console.log('请输入你要切换的用户');
    let changename = readline.question();
    //切换用户的下标    判断用户存不存在
    let changeindex = searchname(changename);
    if (changeindex !== false) {
        //判断切换用户的下标是否等于数据用户下标
        if (changeindex != index) {
            console.log('请输入密码');
            let changepwd = readline.question();
            if (changepwd == accArr[changeindex].pwd) {
                console.log(`密码正确!切换成功!当前账户为${changename}`);
                //切换成功,返回切换当前用户的下标
                return changeindex;
            } else {
                console.log('密码错误!请重新输入');
                return false;
            }
        } else {
            console.log(`切换失败!您已经在${changename}下了!`);
        }
    }
    else {
        console.log('用户不存在!');
    }

}

//转账
function transmoney(index) {
    console.log('请输入你要转账的账户');
    let transname = readline.question();
    let transindex = searchname(transname);
    //判断转账的账号是否存在
    if (transindex !== false) {
        //判断是否给自己转账 
        if (transindex != index) {
            console.log('请输入你要转账的金额');
            let transmoney = readline.question() - 0;
            //判断转账金额是否小于账户数据金额
            if (transmoney <= accArr[index].balance) {
                //减去转账的金额等于账户数据的金额
                accArr[index].balance -= transmoney;
                //其他账户的金额就增加
                accArr[transindex].balance += transmoney;
                console.log('转账成功!');
            } else {
                console.log('转账失败!余额不足!请重新输入');
            }
        } else {
            console.log('无法给自己转账!请重新输入');
        }
    } else {
        console.log(`转账失败!${transname}不存在`);
    }
}
atm();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值