多用户ATM机(面向对象编程)

let readline = require('readline-sync');
// 清屏函数
let clear = () => process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H');
class Person {
    constructor(userID, userName, userPwd) {
        this.id = userID;
        this.name = userName;
        this.pwd = userPwd;
    }
}
class ATM {
    constructor() {
        this.arr = [];
    }
    // 注册
    signAccount(Person) {
        let isflag = true;
        for (let i of this.arr) {
            if (i.id == Person.id) {
                isflag = false;
                break;
            }
        }
        if (isflag) {
            this.arr.push({ id: Person.id, name: Person.name, pwd: Person.pwd, totalMoney: 0 });
            return true;
        } else {
            return false;
        }
    }
    // 登录
    loadAccount(userID, userPwd) {
        let isflag = '';
        for (let i of this.arr) {
            if (i.id == userID && i.pwd == userPwd) {
                isflag = i.name;
            }
        }
        return isflag;
    }
    // 查询
    searchMoneyFunc(userID) {
        for (let i of this.arr) {
            if (i.id == userID) {
                return i.totalMoney;
            }
        }
    }
    // 取钱
    outMoneyFunc(userID, outMoney) {
        if (outMoney < 0) {
            return `非法金额!`;
        }
        for (let i of this.arr) {
            if (i.id == userID) {
                if (i.totalMoney < outMoney) {
                    return `余额不足!`;
                } else {
                    i.totalMoney -= outMoney;
                    return `钱已取出,当前余额为:${i.totalMoney}`;
                }
            }
        }
    }
    // 存钱
    saveMoneyFunc(userID, saveMoney) {
        if (saveMoney < 0) {
            return `非法金额`;
        }
        for (let i of this.arr) {
            if (i.id == userID) {
                i.totalMoney += saveMoney;
                return `钱已存入,当前余额为:${i.totalMoney}`;
            }
        }
    }
    // 转账
    transferMoneyFunc(userID, transferID, transferMoney) {
        if (transferMoney < 0) {
            return `非法金额`;
        }
        let userMoney = this.searchMoneyFunc(userID);
        if (userMoney < transferMoney) {
            return `余额不足!`;
        }
        let isflag = false;
        for (let i of this.arr) {
            if (i.id == transferID) {
                isflag = true;
                i.totalMoney += transferMoney;
                break;
            }
        }
        if (isflag) {
            for (let i of this.arr) {
                if (i.id == userID) {
                    i.totalMoney -= transferMoney;
                    return `转账成功,当前余额为${i.totalMoney}`;
                }
            }
        } else {
            return `没有此用户!`;
        }
    }
}
let atm = new ATM();
let action = true;
while (action) {
    clear();
    console.log('请输入您要进行的操作:1.用户登录  2.用户注册  0.退出');
    let choose = ~~readline.question('');
    switch (choose) {
        // 用户登录
        case 1: {
            console.log('请输入您的账户:');
            let account = readline.question('');
            console.log('请输入您的密码:');
            let password = ~~readline.question('');
            let login = atm.loadAccount(account, password); // 登录
            if (login != '') {
                console.log(`欢迎您!${login}`);
                let act = true;
                while (act) {
                    clear();
                    console.log('请输入您要操作的程序:1、查询 2、取款 3、存款 4、转账 0、退出');
                    let chooseNum = ~~readline.question('');
                    switch (chooseNum) {
                        // 查询
                        case 1: {
                            let totalMoney = atm.searchMoneyFunc(account);
                            console.log(`当前余额为:${totalMoney}`);
                            console.log('按回车键继续~~');
                            readline.question('');
                            break;
                        }
                        // 取款
                        case 2: {
                            console.log('请输入您的取款金额:');
                            let outMoney = ~~readline.question('');
                            console.log(atm.outMoneyFunc(account, outMoney));
                            console.log('按回车键继续~~');
                            readline.question('');
                            break;
                        }
                        // 存款
                        case 3: {
                            console.log('请输入您的存款金额:');
                            let saveMoney = ~~readline.question('');
                            console.log(atm.saveMoneyFunc(account, saveMoney));
                            console.log('按回车键继续~~');
                            readline.question('');
                            break;
                        }
                        // 转账
                        case 4: {
                            console.log('请输入您要转入的账户ID:');
                            let transferID = readline.question('');
                            console.log('请输入您的转款金额:')
                            let transferMoney = ~~readline.question('');
                            console.log(atm.transferMoneyFunc(account, transferID, transferMoney));
                            console.log('按回车键继续~~');
                            readline.question('');
                            break;
                        }
                        // 退出
                        case 0: {
                            act = false;
                            break;
                        }
                        // 输入有误 
                        default:
                            console.log('您的操作不符合规范');
                            console.log('按回车键继续~~');
                            readline.question('');
                            break;
                    }
                }
            } else {
                console.log(`账户或密码错误!按回车键继续~~`);
                readline.question('');
            }
            break;
        }
        // 用户注册
        case 2: {
            console.log('请输入您的姓名:');
            let userName = readline.question('');
            console.log('请输入您的ID:');
            let userID = readline.question('');
            console.log('请输入您的密码:');
            let userPwd = readline.question('');
            let person = new Person(userID, userName, userPwd);
            let account = atm.signAccount(person);
            if (account) {
                console.log('账户创建成功!');
                console.log('按回车键继续~~');
                readline.question('');
                // console.log(atm.arr);
            } else {
                console.log('该账户已被人抢先注册啦,账户创建失败了呢!');
                console.log('按回车键继续~~');
                readline.question('');
            }
            break;
        }
        // 退出
        case 0: {
            console.log('系统已退出,感谢使用!');
            action = false;
            break;
        }
        // 输入错误
        default: {
            console.log('输入有误!');
            console.log('按回车键继续~~');
            readline.question('');
            break;
        }
    }
}

 在多用户ATM机这个案例中,功能比较复杂的算是转账了。用面向对象编程思想来写,思路是比较清晰的。本案例用了ES6的class类来实现。

posted @ 2018-10-14 20:35  smuwgeg  阅读(421)  评论(0编辑  收藏  举报