美文网首页
ATM简易小程序

ATM简易小程序

作者: _Raye | 来源:发表于2017-04-04 11:34 被阅读0次

    Account账户类:

    package org.mobiletrain;
    
    /**
     * 银行账户
     * @author apple
     *
     */
    public class Account {
    
        private String id;        //账号
        private double balance;  //余额
        private String password;//密码
        
        /**
         * 构造器
         * @param id
         */
        public Account(String id,String password,double balance){
            this.id = id;
            this.password = password;
            this.balance = balance;
        }
        
        /**
         * 验证身份是否有效
         * @param password 密码
         * @return 密码匹配返回true,否则返回false
         */
        public boolean isValid(String password){
            return this.password.equals(password);
        }
        
        /**
         * 存钱
         * @param money 存款金额
         */
        public void deposit(double money){
            balance += money;
        }
        
        /**
         * 取钱
         * @param money 取款金额
         * @return 取款成功返回true,否则返回false
         */
        public boolean withdraw(double money){//取钱
            if (money <= balance) {
                balance -= money;
                return true;
            }
            return false;
        }
        
        /**
         * 获取账户ID
         * @return 账户ID
         */
        public String getId() {
            return id;
        }
        
        /**
         * 获取账户余额
         * @return 账户余额
         */
        public double getBalance() {
            return balance;
        }
        
        /**
         * 修改密码
         * @param password 密码
         */
        public void setPassword(String password) {
            this.password = password;
        }
        
    }
    

    ATM类:

    package org.mobiletrain;
    
    /**
     * ATM机
     * @author apple
     *
     */
    public class ATM {
    
        private Account currentAccount = null;
        //用一个数组模拟银行的数据库系统 预先保存若干个银行账户
        private Account[] accountsArray= {
                new Account("11223344","123123",1200),
                new Account("22334455","123456",3000),
                new Account("33445566","666666",5000)
        };
        
        /**
         * 读卡
         * @param account 银行账户
         * @return 读卡成功返回true,否则返回false
         */
        public boolean readCard(String id){
            for (Account account : accountsArray) {
                if (account.getId().equals(id)) {
                    currentAccount = account;
                    return true;
                }
            }
            return false;
        }
        
        /**
         * 验证身份
         * @param password 密码
         * @return 验证通过返回true,否则返回false
         */
        public boolean verify(String password){
            if (currentAccount != null) {
                return currentAccount.isValid(password);
            }
            return false;
        }
        
        /**
         * 查询余额
         * @return 当前余额
         */
        public double showBalance(){
            return currentAccount.getBalance();
        }
        
        /**
         * 存钱
         * @param money
         */
        public void deposit(int money){
            currentAccount.deposit(money);
        }
        
        /**
         * 取钱
         * @param money
         * @return 
         */
        public boolean withdraw(int money){
            return currentAccount.withdraw(money);
        }
        
        /**
         * 修改密码
         * @param newPassword 新密码
         */
        public void changePassword(String newPassword){
            currentAccount.setPassword(newPassword);
        }
        
        /**
         * 退卡
         */
        public void quitCard(){
            currentAccount = null;
        }
        
        /**
         * 转账
         * @param otherId 转入账号
         * @param money 转账金额
         * @return 转账成功返回true,否则返回false
         */
        public boolean transfer(String otherId,double money){
            for (Account account : accountsArray) {
                if (account.getId().equals(otherId)) {
                    Account otherAccount = account;
                    if (currentAccount.getBalance() >= money) {
                        currentAccount.withdraw(money);
                        otherAccount.deposit(money);
                        return true;
                    }
                    else {
                        return false;
                    }
                }
            }
            return false;
        }
    }
    

    ATM测试类:

    package org.mobiletrain.test;
    
    import java.util.Scanner;
    import org.mobiletrain.ATM;
    
    public class ATMtest {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);             
            ATM atm = new ATM();
            boolean isRunning = true;
            while(isRunning){
                System.out.println("欢迎使用XX银行ATM机");
                System.out.print("请插入银行卡:");
                String id = input.next();
                atm.readCard(id);
                if (atm.readCard(id)) {
                    int counter = 0;
                    boolean isCorrect = false;
                    do{
                    counter += 1;
                    System.out.print("请输入密码:");
                    String password = input.next();
                    isCorrect = atm.verify(password);
                    }while(counter < 3 && !isCorrect);
                    if (isCorrect) {
                        boolean flag = true;
                        while(flag){
                            System.out.println("1.查询余额");
                            System.out.println("2.存款");
                            System.out.println("3.取款");
                            System.out.println("4.转账");
                            System.out.println("5.修改密码");
                            System.out.println("6.退卡");
                            int choice;
                            do {
                                System.out.print("请选择:");
                                choice = input.nextInt();
                            } while (choice < 1 || choice > 6);
                            switch (choice) {
                            case 1: 
                                System.out.println("账户余额:" + atm.showBalance());
                                break;
                            case 2:{//一堆花括号就构成了一个作用域(scope)
                                    //在某个作用域中定义的变量只在该作用域中生效
                                System.out.print("请放入钞票:");
                                int money = input.nextInt();
                                atm.deposit(money);
                                System.out.println("存款成功!");
                                break;
                            }
                            case 3:{
                                System.out.print("请输入或选择取款金额:");
                                int money = input.nextInt();
                                if (atm.withdraw(money)) {
                                    System.out.println("请取走你的钞票");
                                }
                                else {
                                    System.out.println("余额不足!");
                                }
                                break;
                            }
                            case 4:{
                                System.out.print("请输入转入账号:");
                                String otherId = input.next();
                                System.out.print("请输入转账金额:");
                                double money = input.nextDouble();
                                if (atm.transfer(otherId, money)) {
                                    System.out.println("转账成功");
                                }
                                else {
                                    System.out.println("转账失败,请检查转入账号和账户余额是否正确!");
                                }
                                break;
                            }
                            case 5:
                                System.out.print("请输入新密码:");
                                String newPwd = input.next();
                                System.out.print("请再输入一次:");
                                String rePwd = input.next();
                                if (newPwd.equals(rePwd)) {
                                    atm.changePassword(newPwd);
                                    System.out.println("密码已修改");
                                }
                                else {
                                    System.out.println("两次密码输入不一致.");
                                }
                                break;
                            case 6:
                                System.out.println("请取走你的卡片");
                                flag = false;
                                break;
                            }
                        }
                    }
                    else {
                        System.out.println("输入密码错误次数超过3次,你的卡被回收");
                    }
                }
            }
            input.close();
        }
    }
    

    相关文章

      网友评论

          本文标题:ATM简易小程序

          本文链接:https://www.haomeiwen.com/subject/mxvaottx.html