java case3:银行ATM day5

atm的1.查询\n2.存款\n3.取款\n4.转账\n5.修改密码\n6.退出\n7.注销账户功能编写

2之前都是跟着黑马编写的;;

2之后都是自己写的可能比较冗长;;

代码:

1.私有化姓名 id 密码 金额 限额

package Case;

public class Account {
    private String ID;
    private String name;
    private String password;
    private double money;
    private double quotaMoney;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public double getQuotaMoney() {
        return quotaMoney;
    }

    public void setQuotaMoney(double quotaMoney) {
        this.quotaMoney = quotaMoney;
    }
}

2.对比黑马 修改了限额的判断(他是默认限额,我是根据零钱判断限额::

package Case;

//ATMsystem

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * atm入口类
 */
public class Case3 {
    public static void main(String[] args) {
        ArrayList<Account> accounts = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("==================ATM=================");
            System.out.println("1.登录账户\n2.注册账户\n3.退出系统");
            System.out.println("请输入您的选择:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    login(accounts,sc);
                    break;
                case 2:
                    register(accounts,sc);
                    break;
                case 3:
                    System.out.println("退出成功!!!");
                    System.exit(0);
                default:
                    System.out.println("您输入有误");
            }
        }
    }

    /**
     *
     * @param accounts
     * @param sc
     */
    private static void login(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("================登入=============");
        if (accounts.size()==0){
            System.out.println("该银行未有账户,请注册");
            register(accounts,sc);
        }

        while (true) {
            System.out.println("请输入卡号:");
            String ID = sc.next();
            Account acc = getID(ID,accounts);
            if (acc != null) {
                while (true) {
                    System.out.println("请输入密码:");
                    String password = sc.next();
                    if (acc.getPassword().equals(password)){
                        System.out.println("登入成功!!!");
                        showUserCommand(sc,acc,accounts);
                        return;//干掉登入方法
                    }else {
                        System.out.println("密码错误");
                    }
                }
            }else {
                System.out.println("卡号不存在");
            }
        }


    }

    /**
     * 展示登入后的操作页
     */
    private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts) {
        while (true){
            System.out.println("===================操作页面========================");
            System.out.println("1.查询\n2.存款\n3.取款\n4.转账\n5.修改密码\n6.退出\n7.注销账户\n请选择:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    showAcc(acc);
                    break;
                case 2:
                    saveMoney(acc,sc);
                    break;
                case 3:
                    drawMoney(acc,sc);
                    break;
                case 4:
                    tansferMoney(acc,sc,accounts);

                    break;
                case 5:
                    updataPassword(acc,sc);
                    return;
                case 6:
                    System.out.println("退出成功!");
                    return;//干掉当前方法
                case 7:
                    System.out.println("您真的要销户??y");
                    String choice1 = sc.next();
                    switch (choice1){
                        case "y":
                            double money = acc.getMoney();
                            System.out.println("您已取出金额:"+money);
                            accounts.remove(acc);
                            System.out.println("销户成功!");
                            return;
                        default:
                            System.out.println("继续保留!!");
                    }
                    break;
                default:
                    System.out.println("请重新选择:");
            }
        }
    }

    /**
     * 修改密码
     * @param acc
     * @param sc
     */
    private static void updataPassword(Account acc, Scanner sc) {
        System.out.print("请您输入您的新密码:");
        String password = sc.next();
        acc.setPassword(password);

    }

    /**
     * 转账
     * @param acc
     * @param sc
     * @param accounts
     */
    private static void tansferMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("==========用户转账操作===========");
        if (accounts.size() < 2) {
            System.out.println("该银行尚未有两个及以上账户");
            return;
        }
        double money = acc.getMoney();
        if (money == 0) {
            System.out.println("余额不足!无法进行转账操作");
            return;
        }
        System.out.print("请您输入对方卡号:");
        String id = sc.next();
        Account account = getID(id,accounts);
        if (account == null) {
            System.out.println("该用户未存在");
            return;
        }
        String name = account.getName();
        String n1 = name.substring(0,1);
        String n2 = name.substring(2);
        String tname = n1 + "*" + n2;
        while (true) {
            System.out.println("请验证\t[" + tname + "]\t中*的字:");
            String n3 = sc.next();
            String name2 = n1 + n3 + n2;
            if (name2.equals(name)) {
                break;
            }
        }//验证
        while (true) {
            System.out.print("请输入您要转账的金额:");
            double tranMoney = sc.nextDouble();
            double quota = acc.getQuotaMoney();
            if (tranMoney <= money && tranMoney <=quota) {
                double money1 =acc.getMoney()-tranMoney;
                acc.setMoney(money1);
                double quota1 = quotamoney(acc);
                acc.setQuotaMoney(quota1);
                showAcc(acc);

                double money2 = account.getMoney()+tranMoney;
                account.setMoney(money2);
                double quota2 = quotamoney(account);
                account.setQuotaMoney(quota2);
                System.out.println("转账成功!!!");
                break;
            }else {
                System.out.println("余额不足&&被限额!!");
            }
        }
    }


    /**
     * 取钱
     * @param acc
     * @param sc
     */
    private static void drawMoney(Account acc, Scanner sc) {
        double money = acc.getMoney();
        if(money <= 0){
            System.out.println("余额不足!!!");
            return;
        }
        while (true) {
            System.out.print("请输入取款金额:");
            double dmoney = sc.nextDouble();
            double quatomoney = acc.getQuotaMoney();
            if (dmoney > money) {
                System.out.println("余额不足!请重新输入:");
            } else if (dmoney > quatomoney) {
                System.out.println("超过限额!请重新输入:");
            } else {
                acc.setMoney(money - dmoney);
                double quatoMoney = quotamoney(acc);
                acc.setQuotaMoney(quatoMoney);
                break;
            }
        }
        showAcc(acc);
    }

    /**
     * 存钱
     * @param acc
     * @param sc
     */
    private static void saveMoney(Account acc,Scanner sc) {
        System.out.println("请您输入存款金额");
        double money = sc.nextDouble();
        acc.setMoney(acc.getMoney()+money);
        System.out.println("恭喜您存钱成功!!!");
        double quatomoney = quotamoney(acc);
        acc.setQuotaMoney(quatomoney);
        showAcc(acc);
    }

    /**
     * 展示账户
     * @param acc
     */
    private static void showAcc(Account acc) {
        System.out.println("=================账户================");
        System.out.println("ID:\t"+acc.getID());
        System.out.println("户主:\t"+acc.getName());
        System.out.println("余额:\t"+acc.getMoney());
        System.out.println("限额:\t"+acc.getQuotaMoney());
    }

    /**
     * 用户开户功能的实现;;
     * @param accounts
     */
    private static void register(ArrayList<Account> accounts,Scanner sc) {
        System.out.println("==========系统开户操作=============");
        Account account = new Account();

        System.out.println("请输入您的姓名:");
        String name = sc.next();
        account.setName(name);

        while (true) {
            System.out.println("请输入密码:");
            String password = sc.next();
            System.out.println("请确认密码:");
            String okpasswod = sc.next();
            if (password.equals(okpasswod)){
                account.setPassword(password);
                break;
            }else {
                System.out.println("密码错误!!!");
            }
        }

        double quotaMoney = 5000;
        account.setQuotaMoney(quotaMoney);

        //生产随机卡号
        String ID = randomID(accounts);
        account.setID(ID);

        //把账户对象添加到集合
        accounts.add(account);
        System.out.println("恭喜!!!"+name+"开户成功!!您的卡号为:"+ID+",请妥善保管您的ID");
    }

    /**
     * 生成卡号
     * @return
     */
    private static String randomID(ArrayList<Account> accounts) {
        Random x = new Random();
        while (true) {
            //1.生成8位数字
            String ID = "";
            for (int i = 0; i < 8; i++) {
                ID += x.nextInt(10);
            }
            //2.判断重复
            Account acc = getID(ID,accounts);
            if (acc == null) {
                return ID;
            }
        }
    }

    /**
     *判断卡号是否存在
     * @param ID
     * @param accounts
     * @return  account | null
     */
    private static Account getID(String ID,ArrayList<Account> accounts){
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getID().equals(ID)){
                return acc;
            }
        }
        return null;
    }

    /**
     * 限额
     * @return
     */
    private static double quotamoney(Account acc) {
        double money = acc.getMoney();
        double quotaMoney;
        if(money <= 5000){
            quotaMoney = 5000.00;
        } else if (money <= 10000) {
            quotaMoney = 8000.00;
        }else{
            quotaMoney = 10000.00;
        }
        return quotaMoney;
    }
}


运行结果:

E:\soft\Java\jdk-17.0.1\bin\java.exe "-javaagent:E:\soft\IntelliJIDEA 2022.1.3\lib\idea_rt.jar=55930:E:\soft\IntelliJIDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\code\out\production\untitled Case.Case3
==================ATM=================
1.登录账户
2.注册账户
3.退出系统
请输入您的选择:
2
==========系统开户操作=============
请输入您的姓名:
许x文
请输入密码:
123
请确认密码:
123
恭喜!!!许x文开户成功!!您的卡号为:19471741,请妥善保管您的ID
==================ATM=================
1.登录账户
2.注册账户
3.退出系统
请输入您的选择:
2
==========系统开户操作=============
请输入您的姓名:
张x希
请输入密码:
123
请确认密码:
123
恭喜!!!张x希开户成功!!您的卡号为:32661867,请妥善保管您的ID
==================ATM=================
1.登录账户
2.注册账户
3.退出系统
请输入您的选择:
1
================登入=============
请输入卡号:
19471741
请输入密码:
123
登入成功!!!
===================操作页面========================
1.查询
2.存款
3.取款
4.转账
5.修改密码
6.退出
7.注销账户
请选择:
2
请您输入存款金额
12000
恭喜您存钱成功!!!
=================账户================
ID:	19471741
户主:	许x文
余额:	12000.0
限额:	10000.0
===================操作页面========================
1.查询
2.存款
3.取款
4.转账
5.修改密码
6.退出
7.注销账户
请选择:
3
请输入取款金额:3000
=================账户================
ID:	19471741
户主:	许x文
余额:	9000.0
限额:	8000.0
===================操作页面========================
1.查询
2.存款
3.取款
4.转账
5.修改密码
6.退出
7.注销账户
请选择:
4
==========用户转账操作===========
请您输入对方卡号:32661867
请验证	[张*希]	中*的字:
x
请输入您要转账的金额:6000
=================账户================
ID:	19471741
户主:	许x文
余额:	3000.0
限额:	5000.0
转账成功!!!
===================操作页面========================
1.查询
2.存款
3.取款
4.转账
5.修改密码
6.退出
7.注销账户
请选择:
5
请您输入您的新密码:456
==================ATM=================
1.登录账户
2.注册账户
3.退出系统
请输入您的选择:
1
================登入=============
请输入卡号:
32661867
请输入密码:
123
登入成功!!!
===================操作页面========================
1.查询
2.存款
3.取款
4.转账
5.修改密码
6.退出
7.注销账户
请选择:
1
=================账户================
ID:	32661867
户主:	张x希
余额:	6000.0
限额:	8000.0
===================操作页面========================
1.查询
2.存款
3.取款
4.转账
5.修改密码
6.退出
7.注销账户
请选择:
7
您真的要销户??y
y
您已取出金额:6000.0
销户成功!
==================ATM=================
1.登录账户
2.注册账户
3.退出系统
请输入您的选择:
3
退出成功!!!

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值