ATM取款机

一、 需要ATM机类,该类完成主菜单的相关操作,在取钱和转账时,需判断余额情况

要求:实现数据持久化存储(方式不限)

创建ATM机类

    private static List<String> transferInfo = new ArrayList<>(); //转账信息
    private static List<Atm> list = new ArrayList<>(); //把读取到的信息放到ATM集合里边
    private static Atm atm = null; //本人
    private String account;  //账号
    private String password; //密码
    private double balance;  //余额
    private String bankName; //银行卡名字
    private String name;     //持卡人姓名

创建菜单 二级菜单

public void main(){
        int a = 0;
        System.out.println("***************");
        System.out.println("***欢迎使用ATM***");
        System.out.println("***************");
        System.out.println("请输入卡号");
        String user = new Scanner(System.in).next();
        System.out.println("请输入密码");
        String pwd = new Scanner(System.in).next();
        for (int i = 0; i < this.list.size(); i++) {
            if (this.list.get(i).getAccount().equals(user) && this.list.get(i).getPassword().equals(pwd)){
                a = 1;
                System.out.println("-->>密码正确");
                atm = list.get(i);
                secondMain();
                break;
            }
        }
        if (a == 0){
            System.out.println("密码错误");
            main();
            return;
        }
    }

    private void secondMain(){
        int a = 0;
        while(a != 6){
            System.out.println("*********************");
            System.out.println("**请输入你想要操作的类型**");
            System.out.println("** 1.余额查询 2.取款 **");
            System.out.println("** 3.存款    4.转账 **");
            System.out.println("** 5.打印    6.退卡 **");
            System.out.println("*** 7.查询转账信息  ***");
            System.out.println("***************");
            a = new Scanner(System.in).nextInt();
            switch (a){
                case 1: yue();break;
                case 2: getMoney();break;
                case 3: saveMoney();break;
                case 4: transfer();break;
                case 5: print();break;
                case 6: exit();break;
                case 7: askTransferInfo();break;
            }
        }
    }

创建各种功能 取款(test)、存款(test)、、、、、

 private void yue(){
        System.out.println("你的余额是:" + atm.getBalance());
    }

    private void getMoney(){
        FileWriter out = null;
        BufferedWriter bw = null;
        try {
            System.out.println("请输入你要取款金额");
            double money = new Scanner(System.in).nextDouble();
            atm.setBalance(atm.getBalance() - money);
            out = new FileWriter("account.txt");
            bw = new BufferedWriter(out);

            if (list.indexOf(atm) > -1){
                list.set(list.indexOf(atm), atm);
                System.out.println("取钱成功!!你的余额是" + atm.getBalance());

                for (int i = 0; i < list.size(); i++){
                    bw.write(list.get(i).getAccount() + "," +list.get(i).getPassword() + "," + list.get(i).getBalance() + "," + list.get(i).getBankName() + "," + list.get(i).getName());
                    bw.newLine();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bw != null)
                    bw.close();
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void saveMoney(){
        FileWriter out = null;
        BufferedWriter bw = null;
        try {
            System.out.println("请输入你要存款金额");
            double money = new Scanner(System.in).nextDouble();
            atm.setBalance(atm.getBalance() + money);
            out = new FileWriter("account.txt");
            bw = new BufferedWriter(out);

            if (list.indexOf(atm) > -1){
                list.set(list.indexOf(atm), atm);
                System.out.println("存款成功!!你的余额是" + atm.getBalance());

                for (int i = 0; i < list.size(); i++){
                    bw.write(list.get(i).getAccount() + "," +list.get(i).getPassword() + "," + list.get(i).getBalance() + "," + list.get(i).getBankName() + "," + list.get(i).getName());
                    bw.newLine();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bw != null)
                    bw.close();
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void transfer(){
        int a = 0;
        FileWriter out = null;
        BufferedWriter bw = null;
        System.out.println("请输入你要转账的卡号");
        String user = new Scanner(System.in).next();
        for (int i = 0; i < list.size();i++){
            Atm atm1 = list.get(i);
            if (atm1.getAccount().equals(user) && !atm1.getAccount().equals(atm.getAccount())){
                a = 1;
                System.out.println("请输入你要转账的金额");
                double money = new Scanner(System.in).nextDouble();
                if (atm.getBalance() - money < 0){
                    System.out.println("余额不足");
                }else {
                    atm.setBalance(atm.getBalance() - money);
                    atm1.setBalance(atm1.getBalance() + money);
                    System.out.println("转账成功,你的余额是" + atm.getBalance());
                    String data = atm1.getName() + money + "     " + atm1.getBankName();
                    transferInfo.add(data);
                    try {
                        out = new FileWriter("account.txt");
                        bw = new BufferedWriter(out);
                        for (int j = 0; j < list.size(); j++){
                            bw.write(list.get(j).getAccount() + "," +list.get(j).getPassword() + "," + list.get(j).getBalance() + "," + list.get(j).getBankName() + "," + list.get(j).getName());
                            bw.newLine();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            if (bw != null)
                                bw.close();
                            if (out != null)
                                out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                }

            }else{
                a = 0;
            }
        }
        if (a == 0){
            System.out.println("输入错误");
            transfer();
        }

    }

    private void print(){
        System.out.println("文件保存路径 m.txt");
        FileWriter out = null;
        BufferedWriter bw = null;
        try {
            out = new FileWriter("m.txt");
            bw = new BufferedWriter(out);
            for (Atm atm :
                    list) {
                bw.write(atm.getAccount() + "," +atm.getPassword() + "," + atm.getBalance() + "," + atm.getBankName() + "," + atm.getName());
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                if (bw != null)
                    bw.close();
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void exit(){
        System.out.println("请保管好你的卡片");
        System.exit(0);
    }

    private void askTransferInfo(){
        for (String s :
                transferInfo) {
            System.out.println(s);
        }
    }

运行截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值