ATM系统 Java入门项目实战

20天学会JAVA黑马教程

2022/03/26 20:54 打卡 

加油 !!!

package cfqp;

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

public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> array = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("---------------欢迎来到ATM系统---------------");
            System.out.println("请选择操作");
            System.out.println("1:开户");
            System.out.println("2:登录");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    createAccount(array, sc);
                    break;
                case 2:
                    loginAccount(array, sc);
                    break;
                default:
                    System.out.println("您输入的操作有误");
            }
        }

    }

    //    private static void loginAccount(ArrayList<Account> accounts, Scanner sc) {
//        while (true) {
//            System.out.println("----------------系统登录页面------------------");
//            if(accounts.size()==0){
//                System.out.println("当前无任何账户,请先创建!");
//                return;
//            }
//            System.out.println("请输入您的卡号:");
//            String cardId = sc.next();
//            boolean flag = false;
//            for (int i = 0; i < accounts.size(); i++) {
//                Account account = accounts.get(i);
//                if (account.getCardId().equals(cardId)) {
//                    flag = true;
//                    while (true) {
//                        System.out.println("请输入您的密码:");
//                        String password = sc.next();
//                        if (password.equals(account.getPassword())) {
//                            System.out.println(account.getName() + "贵宾,欢迎您进入系统,您的卡号:" + account.getCardId());
//                            break;
//                        } else {
//                            System.out.println("您的密码有误,请确认!");
//                        }
//                    }
//                }
//                if (flag) {
//                    break;
//                }
//
//            }
//            if (!flag) {
//                System.out.println("不存在该卡号!");
//            } else {
//                break;
//            }
//        }
//    }
    private static void loginAccount(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("----------------系统登录页面------------------");
        if (accounts.size() == 0) {
            System.out.println("当前无任何账户,请先创建!");
            return;
        }

        while (true) {
            System.out.println("请输入您的卡号:");
            String cardId = sc.next();
            Account account = getAccountById(cardId, accounts);
            if (account == null) {
                System.out.println("不存在该卡号!");
            } else {
                while (true) {
                    System.out.println("请输入密码:");
                    String password = sc.next();
                    if (password.equals(account.getPassword())) {
                        System.out.println(account.getName() + "贵宾,欢迎您进入系统,您的卡号:" + account.getCardId());
                        showUserCommand(sc, account, accounts);
                        return;

                    } else {
                        System.out.println("您的密码有误,请确认!");
                    }
                }

            }
        }
    }

    public static void showUserCommand(Scanner sc, Account account, ArrayList<Account> accounts) {
        while (true) {
            System.out.println("-------------------欢迎您进入银行用户操作界面----------------");
            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 command = sc.nextInt();
            switch (command) {
                case 1:
                    showAccount(account);
                    break;
                case 2:
                    deposit(account, sc);
                    break;
                case 3:
                    Withdrawal(account, sc);
                    break;
                case 4:
                    Transfer(account, sc, accounts);
                    break;
                case 5:
                    changePassword(account, sc);
                    return;
                case 6:
                    return;
                case 7:
                    if(destroyAccount(sc, accounts, account)){
                        return;
                    }
                    else{
                        break;
                    }
                default:
                    System.out.println("您输入的操作有误");
            }
        }
    }

    /**
     * 销户
     *
     * @param accounts 账户集合
     * @param account  当前登录成功的账户对象
     */
    private static boolean destroyAccount(Scanner sc, ArrayList<Account> accounts, Account account) {
        System.out.println("您真的要销户?y/n");
        String s = sc.next();
        switch (s) {
            case "y":
                if (account.getMoney() > 0) {
                    System.out.println("余额不为0,不能销户");
                } else {
                    accounts.remove(account);
                    System.out.println("销户完成,回到登录页面");
                    return true;
                }
                break;
            default:
                System.out.println("当前账户继续保留~");
        }
        return false;
    }




    /**
     * 修改密码
     *
     * @param account 当前登录成功的账户对象
     * @param sc      扫描器
     */
    private static void changePassword(Account account, Scanner sc) {
        System.out.println("----------------欢迎来到密码修改界面---------------");
        while (true) {
            System.out.println("请您输入当前账户的密码:");
            String password = sc.next();
            if (password.equals(account.getPassword())) {
                while (true) {
                    System.out.println("请您输入新的密码:");
                    String passwordNew = sc.next();
                    System.out.println("请您确认新密码:");
                    String passwordAck = sc.next();
                    if (passwordAck.equals(passwordNew)) {
                        System.out.println("密码修改成功,请您重新登录!");
                        account.setPassword(passwordAck);
                        return;
                    } else {
                        System.out.println("两次密码不一致");
                    }
                }
            } else {
                System.out.println("密码错误");
            }
        }
    }

    private static void Transfer(Account account, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("-----------------欢迎进入转账界面-----------------");
        if (accounts.size() < 2) {
            System.out.println("当前系统,账户不足两个,不能转账!!!");
            return;
        }
        if (account.getMoney() == 0) {
            System.out.println("您自己都没钱,就别转了!");
            return;
        }
        while (true) {
            System.out.println("请您输入对方卡号:");
            String id = sc.next();
            if (id.equals(account.getCardId())) {
                System.out.println("账户不能是自己的!");
                continue;
            }
            Account account1 = getAccountById(id, accounts);
            if (account1 == null) {
                System.out.println("对方卡号不存在");
            } else {

                String name = account1.getName();
                String s1 = '*' + name.substring(1);
                System.out.println("请输入[" + s1 + "]的姓氏");
                String s = sc.next();
                if (name.startsWith(s)) {
                    while (true) {
                        System.out.println("请输入转账金额:");
                        double money = sc.nextDouble();
                        if (money < account.getMoney()) {
                            account.setMoney(account.getMoney() - money);
                            account1.setMoney(account1.getMoney() + money);
                            System.out.println("转账成功,您当前余额为" + account.getMoney());
                            return;
                        } else {
                            System.out.println("余额不足,您最多可以转" + account.getMoney());
                        }
                    }
                } else {
                    System.out.println("姓氏输入有误");
                }
            }

        }

    }


    private static void Withdrawal(Account account, Scanner sc) {
        System.out.println("---------------欢迎您进入取款界面------------------");
        if (account.getMoney() < 100) {
            System.out.println("账户余额不足100,快去存钱吧!");
        } else {
            while (true) {
                System.out.println("请您输入取款金额");
                double money = sc.nextDouble();
                if (money > account.getQuotaMoney()) {
                    System.out.println("您当前取款超过了当次限额");
                } else {
                    if (money > account.getMoney()) {
                        System.out.println("您的账户余额不足");
                    } else {
                        account.setMoney(account.getMoney() - money);
                        System.out.println("取款成功");
                        break;
                    }
                }
            }
        }
    }


    private static void deposit(Account account, Scanner sc) {
        System.out.println("请您输入存款的金额");
        account.setMoney(sc.nextDouble() + account.getMoney());
        System.out.println("您已存款成功!");
        showAccount(account);
    }

    public static void showAccount(Account account) {
        System.out.println("---------------您的账户信息如下:------------------");
        System.out.println("卡号:" + account.getCardId());
        System.out.println("姓名:" + account.getName());
        System.out.println("余额:" + account.getMoney());
        System.out.println("当此取现额度:" + account.getQuotaMoney());
    }

    private static void createAccount(ArrayList<Account> array, Scanner sc) {
        System.out.println("-----------------系统开户操作-------------------");
        Account account = new Account();
        System.out.println("请输入账号");
        account.setName(sc.next());

        while (true) {
            System.out.println("请输入密码:");
            String password = sc.next();
            System.out.println("请输入确认密码:");
            String passwordAck = sc.next();
            if (passwordAck.equals(password)) {
                account.setPassword(password);
                break;
            } else {
                System.out.println("两次密码不正确!");
            }
        }
        System.out.println("请设置当前取现额度:");
        double quotaMoney = sc.nextDouble();
        account.setQuotaMoney(quotaMoney);

        account.setCardId(getRandomCardId(array));
        array.add(account);
        System.out.println("尊敬的" + account.getName() + "贵宾,您的账户已经开卡成功,您的卡号是:" + account.getCardId());
    }

    /**
     * @param array
     * @return s
     */
//    private static String getRandomCardId(ArrayList<Account> array) {
//        String s;
//        Random r = new Random();
//        while (true) {
//            boolean flag = true;
//            s= "";
//            for (int i = 0; i < 8; i++) {
//                int no = r.nextInt(10);
//                s += String.valueOf(no);
//            }
//            for (int i = 0; i < array.size(); i++) {
//                if (array.get(i).getCardId().equals(s)) {
//                    flag = false;
//                    break;
//                }
//            }
//            if (flag) {
//                break;
//            }
//        }
//        return s;
//    }
    private static String getRandomCardId(ArrayList<Account> array) {
        String s = "";
        Random r = new Random();
        while (true) {
            for (int i = 0; i < 8; i++) {
                int no = r.nextInt(10);
                s += no;
            }
            Account account = getAccountById(s, array);
            if (account == null) {
                return s;
            }
        }

    }

    private static Account getAccountById(String id, ArrayList<Account> array) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getCardId().equals(id)) {
                return array.get(i);
            }
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值