Java小练习,ATM机系统模拟(ArrayList)

@Data
public class AcountUser {
    private String cardId;
    private String username;
    private String password;
    private double money;//余额
    private double quotemoney;//限制额度

    public AcountUser() {
    }

    public AcountUser(String cardId, String username, String password, double money, double quotemoney) {
        this.cardId = cardId;
        this.username = username;
        this.password = password;
        this.money = money;
        this.quotemoney = quotemoney;
    }
}

---------------------------------------------------------------------------------------------------------------------------

public class ATMOs {
    public static void main(String[] args) {
        ArrayList<AcountUser> auList = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        Random random = new Random();

        while (true) {
            System.out.println("-------------欢迎进入ATM系统-------------");
            System.out.println("1.登录账户");
            System.out.println("2.注册账户");
            System.out.println("请输入1/2选择你对应的操作");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    System.out.println("登录账户");
                    getLogin(auList, sc);
                    break;
                case 2:
                    System.out.println("注册账户");
                    getRegister(auList, sc, random);
                    break;
                default:
                    System.out.println("您的选择不在功能范围,请重新输入");
                    break;
            }
        }
    }

    private static void loginSuccess(Scanner sc, ArrayList<AcountUser> auList, AcountUser acountUser) {
        System.out.println("-------------欢迎进入银行用户操作界面-------------");
        while (true) {
            System.out.println("1.查询:");
            System.out.println("2.存款:");
            System.out.println("3.取款:");
            System.out.println("4.转账:");
            System.out.println("5.修改密码:");
            System.out.println("6.退出");
            System.out.println("7.注销当前账户");
            System.out.println("您可继续进行选择功能进行操作了:");
            int num1 = sc.nextInt();
            switch (num1) {
                case 1:
                    System.out.println("1.查询:");
                    findAcount(acountUser);
                    break;
                case 2:
                    System.out.println("2.存款:");
                    deposit(acountUser, sc);
                    break;
                case 3:
                    System.out.println("3.取款:");
                    drawMoney(acountUser, sc);
                    break;
                case 4:
                    System.out.println("4.转账:");
                    transferAccounts(auList, acountUser, sc);
                    break;
                case 5:
                    System.out.println("5.修改密码:");
                    updatePassword(sc,acountUser);
                    return;
                case 6:
                    System.out.println("6.退出:");
                    return;
                case 7:
                    System.out.println("7.注销当前账户:");
                    auList.remove(acountUser);
                    System.out.println("当前用户已注销");
                    return;
                default:
                    System.out.println("您输入的选项不在工作区,请重新输入");
                    break;
            }
        }
    }

    //修改密码
    private static void updatePassword(Scanner sc, AcountUser acountUser) {
        System.out.println("-------------欢迎您进入修改账户界面--------------");
        while (true) {
            System.out.println("请输入当前账户密码");
            String password = sc.next();
            if(!acountUser.getPassword().equals(password)){
                System.out.println("密码错误,请重新输入");
            }else{
                while (true) {
                    System.out.println("请您输入新密码");
                    String newpassword = sc.next();
                    System.out.println("请您确认密码");
                    String newpassword2 = sc.next();
                    if(!newpassword.equals(newpassword2)){
                        System.out.println("两次密码不一致,请重新输入");
                    }else{
                        acountUser.setPassword(newpassword);
                        System.out.println("密码修改成功");
                        return;
                    }
                }
            }
        }
    }

    //转账功能
    private static void transferAccounts(ArrayList<AcountUser> auList, AcountUser acountUser, Scanner sc) {
        System.out.println("-------------欢迎您进入银行用户转账界面--------------");
        if (auList.size() < 2) {
            System.out.println("当前用户不足2个,不能转账");
            return;
        }
        if (acountUser.getMoney() <= 0) {
            System.out.println("余额不足,不能转账");
            return;
        }
        while (true) {
            System.out.println("请输入您需要转账的账户卡号");
            String cardId = sc.next();
            AcountUser user2 = isCardIDExist(auList, cardId);
            if (user2 == null) {
                System.out.println("此账号不存在,请重新输入");
            } else if (user2.getCardId().equals(acountUser.getCardId())) {
                System.out.println("不能给自己转账,请重新输入");
            } else {
                System.out.println("您要给*" + user2.getUsername().substring(1) + "转账");
                while (true) {
                    System.out.println("请输入姓氏确认账户");
                    String firstname = sc.next();
                    if (user2.getUsername().startsWith(firstname)) {
                        System.out.println("正确");
                        break;
                    } else {
                        System.out.println("错误,请重新输入");
                    }
                }
                while (true) {
                    System.out.println("请输入您的转账金额:");
                    double money = sc.nextDouble();
                    if (money > acountUser.getMoney()) {
                        System.out.println("您输入的转账金额超出余额,请重新输入");
                    } else if (money > acountUser.getQuotemoney()) {
                        System.out.println("您输入的转账金额超出限度,请重新输入");
                    } else {
                        acountUser.setMoney(acountUser.getMoney() - money);
                        user2.setMoney(user2.getMoney() + money);
                        System.out.println("转账成功,您当前的余额是:" + acountUser.getMoney());
                        return;
                    }
                }
            }
        }
    }

    //取款功能
    private static void drawMoney(AcountUser acountUser, Scanner sc) {
        System.out.println("-------------欢迎您进入银行用户取款界面--------------");
        if (acountUser.getMoney() < 100) {
            System.out.println("您当前的余额不足,无法取款");
            return;
        }
        System.out.println("请输入您的取款金额:");
        double money = sc.nextDouble();
        if (money > acountUser.getMoney()) {
            System.out.println("您输入的金额超出余额【您当前的余额为" + acountUser.getMoney() + "】,请重新输入您的取款金额:");
        } else {
            acountUser.setMoney(acountUser.getMoney() - money);
            System.out.println("取款成功,您当前的余额是:" + acountUser.getMoney());
        }
    }

    //存款功能
    private static void deposit(AcountUser acountUser, Scanner sc) {
        System.out.println("-------------欢迎您进入银行用户存款界面--------------");
        System.out.println("您当前的余额:" + acountUser.getMoney());
        System.out.println("请输入您的存款金额:");
        double money = sc.nextDouble();
        acountUser.setMoney(acountUser.getMoney() + money);
        System.out.println("存款成功,当前余额:" + acountUser.getMoney());
    }

    //1.查询功能
    private static void findAcount(AcountUser acountUser) {
        System.out.println("-------------欢迎您进入银行用户详情界面--------------");
        System.out.println("===============================================\n" +
                "为您的账户信息如下:\n" +
                "账户:" + acountUser.getCardId() + "\n" +
                "姓名:" + acountUser.getUsername() + "\n" +
                "余额:" + acountUser.getMoney() + "\n" +
                "当前取现额度:" + acountUser.getQuotemoney() + "\n===============================================");
    }

    //注册操作
    private static void getRegister(ArrayList<AcountUser> auList, Scanner sc, Random random) {
        String username = null;
        String password = null;
        while (true) {
            System.out.println("-------------欢迎进入到开户操作-------------");
            System.out.println("请输入您的账户名称username");
            username = sc.next();
            System.out.println("请输入您的账户密码password");
            password = sc.next();
            System.out.println("请确认您的账户密码password");
            String password2 = sc.next();
            if (password.equals(password2)) {
//                System.out.println("两次输入密码一致");
                break;
            } else {
                System.out.println("两次密码输入不一致,请重新输入");
//                getRegister(auList,sc,random);
            }
        }
        String cardId = createCardId(random, auList);
        System.out.println("请您设置当次取现额度quotemoney");
        double quotemoney = sc.nextDouble();
        AcountUser acountUser = new AcountUser(cardId, username, password, 0, quotemoney);
        auList.add(acountUser);
        System.out.println("恭喜您," + username + "先生/女士,您开户完成,您的卡号是:" + cardId);
    }

    //随机生成账户号
    private static String createCardId(Random random, ArrayList<AcountUser> auList) {
        String cardId = "";
        for (int i = 0; i < 8; i++) {
            int randomNum = random.nextInt(10);
            cardId += randomNum;
        }
        AcountUser acountUser = isCardIDExist(auList, cardId);
        if (acountUser == null) {
            System.out.println("卡号生成成功");
        } else {
            createCardId(random, auList);
        }
        return cardId;
    }

    //判断账户号在系统中是否已经存在
    private static AcountUser isCardIDExist(ArrayList<AcountUser> auList, String cardId) {
        for (int i = 0; i < auList.size(); i++) {
            AcountUser acountUser = auList.get(i);
            if (acountUser.getCardId().equals(cardId)) {
//                System.out.println();
                return acountUser;
            }
        }
        return null;
    }

    //登录操作
    private static void getLogin(ArrayList<AcountUser> auList, Scanner sc) {
        if (auList.isEmpty()) {
            System.out.println("错误,体统中没有任何用户");
            return;
        }
        System.out.println("-------------欢迎进入到登录操作-------------");
        System.out.println("请输入您的银行卡号");
        String cardId = sc.next();
        AcountUser acountUser = isCardIDExist(auList, cardId);
        if (acountUser == null) {
            System.out.println("此卡号不存在");
            getLogin(auList, sc);
        } else {
            while (true) {
                System.out.println("请输入您的银行卡密码");
                String password = sc.next();
//                System.out.println(acountUser.getPassword());
                if (acountUser.getPassword().equals(password)) {
                    System.out.println("登录成功");
                    loginSuccess(sc, auList, acountUser);
                    return;
                } else {
                    System.out.println("您的密码有误,请重新输入");
                }
            }
        }
    }
}

---------------------------------------------------------------------------------------------------------------

运行

-------------欢迎进入ATM系统-------------
1.登录账户
2.注册账户
请输入1/2选择你对应的操作
2
注册账户
-------------欢迎进入到开户操作-------------
请输入您的账户名称username
李四
请输入您的账户密码password
123
请确认您的账户密码password
1233
两次密码输入不一致,请重新输入
-------------欢迎进入到开户操作-------------
请输入您的账户名称username
李四
请输入您的账户密码password
123
请确认您的账户密码password
123
卡号生成成功
请您设置当次取现额度quotemoney
100
恭喜您,李四先生/女士,您开户完成,您的卡号是:27736068
-------------欢迎进入ATM系统-------------
1.登录账户
2.注册账户
请输入1/2选择你对应的操作
2
注册账户
-------------欢迎进入到开户操作-------------
请输入您的账户名称username
张三
请输入您的账户密码password
1234
请确认您的账户密码password
1234
卡号生成成功
请您设置当次取现额度quotemoney
1000
恭喜您,张三先生/女士,您开户完成,您的卡号是:68366356
-------------欢迎进入ATM系统-------------
1.登录账户
2.注册账户
请输入1/2选择你对应的操作
1
登录账户
-------------欢迎进入到登录操作-------------
请输入您的银行卡号
683663561
此卡号不存在
-------------欢迎进入到登录操作-------------
请输入您的银行卡号
68366356
请输入您的银行卡密码
123
您的密码有误,请重新输入
请输入您的银行卡密码
1234
登录成功
-------------欢迎进入银行用户操作界面-------------
1.查询:
2.存款:
3.取款:
4.转账:
5.修改密码:
6.退出
7.注销当前账户
您可继续进行选择功能进行操作了:
5
5.修改密码:
-------------欢迎您进入修改账户界面--------------
请输入当前账户密码
123
密码错误,请重新输入
请输入当前账户密码
1234
请您输入新密码
123
请您确认密码
1234
两次密码不一致,请重新输入
请您输入新密码
123
请您确认密码
123
密码修改成功
-------------欢迎进入ATM系统-------------
1.登录账户
2.注册账户
请输入1/2选择你对应的操作
1
登录账户
-------------欢迎进入到登录操作-------------
请输入您的银行卡号
68366356
请输入您的银行卡密码
123
登录成功
-------------欢迎进入银行用户操作界面-------------
1.查询:
2.存款:
3.取款:
4.转账:
5.修改密码:
6.退出
7.注销当前账户
您可继续进行选择功能进行操作了:
7
7.注销当前账户:
当前用户已注销
-------------欢迎进入ATM系统-------------
1.登录账户
2.注册账户
请输入1/2选择你对应的操作
1
登录账户
-------------欢迎进入到登录操作-------------
请输入您的银行卡号
68366356
此卡号不存在
-------------欢迎进入到登录操作-------------
请输入您的银行卡号

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值