24.ATM项目

wuchangjian2021-11-03 18:09:53编程学习

//ATMSystem.java

package package1;

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


public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> accounts = new ArrayList<>();
        showMain(accounts);

    }

    public static void showMain(ArrayList<Account> accounts) {
        System.out.println("-------------------------欢迎进入首页---------------------------");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你想做的操作");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.print("您可以输入命令了:");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    login(accounts, sc);
                    break;
                case 2:
                    register(accounts, sc);
                    break;
                default:
                    System.out.println("您当前输入的操作不支持");
            }
        }
    }


    private static void login(ArrayList<Account> accounts, Scanner sc) {

        if (accounts.size() == 0) {
            System.out.println("您当前系统中无任何账户,请先注册");
            return;
        }


        while (true) {
            System.out.println("请您输入登陆的卡号");
            String cardId = sc.next();

            Account acc = getAccountByCardId(cardId, accounts);

            if (acc != null) {
                while (true) {
                    System.out.println("请您输入登陆的密码");
                    String password = sc.next();

                    if (acc.getPassWord().equals(password)) {
                        System.out.println("恭喜您" + acc.getUserName() + "先生/女士成功进入系统,您的卡号是:" + acc.getCardId());
                        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.查询账户");
            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(acc);
                    break;
                case 2:
                    //存款
                    depositMoney(acc, sc);
                    break;
                case 3:
                    //取款
                    drawMoney(acc, sc);
                    break;
                case 4:
                    //转账2
                    transferMoney(accounts, acc, sc);
                    break;
                case 5:
                    //修改密码
                    updataPassWord(acc, sc);
                    return;
                case 6:
                    //退出
                    System.out.println("欢迎下次光临");
                    return;
                case 7:
                    //注销账户
                    accounts.remove(acc);
                    System.out.println("销户成功");
                    return;
                default:
                    System.out.println("您的命令输入有误~~~");
            }
        }

    }

    private static void updataPassWord(Account acc, Scanner sc) {
        System.out.println("===================修改密码====================");


        while (true) {
            System.out.println("请您输入正确的密码");
            String okpassword = sc.next();
            if (acc.getPassWord().equals(okpassword)) {
                while (true) {
                    System.out.println("请您输入新密码");
                    String newPassWord = sc.next();

                    System.out.println("请您输入确认密码");
                    String oknewPassWord = sc.next();

                    if (newPassWord.equals(oknewPassWord)) {
                        acc.setPassWord(newPassWord);
                        return;
                    } else {
                        System.out.println("您两次输入的密码不一致");
                    }
                }
            } else {
                System.out.println("当前输入的密码不正确");
            }
        }
    }

    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc) {
        if (accounts.size() < 2) {
            System.out.println("对不起,系统中无其他账户");
            return;
        }

        if (acc.getMoney() == 0) {
            System.out.println("对不起,您自己都没钱,就别转了~~");
            return;
        }

        while (true) {
            System.out.println("请您输入对方的卡号");
            String cardId = sc.next();
            Account account = getAccountByCardId(cardId, accounts);
            if (account != null) {
                if (account.getCardId().equals(acc.getCardId())) {
                    System.out.println("您不可以为自己转账");
                } else {
                    String name = "*" + account.getUserName().substring(1);
                    System.out.println("请确认【" + name + "】的姓氏");
                    String preName = sc.next();

                    if (account.getUserName().startsWith(preName)) {
                        System.out.println("请您输入转账的金额");
                        double money = sc.nextDouble();
                        if (money >= acc.getMoney()) {
                            System.out.println("对不起,您要转账的金额太多,您最多可以转账" + acc.getMoney());
                        } else {
                            acc.setMoney(acc.getMoney() - money);
                            account.setMoney(account.getMoney() + money);
                            System.out.println("恭喜您,转账成功了,已经为" + account.getUserName() + "转账" + money);
                            showAccount(acc);
                            return;
                        }
                    } else {
                        System.out.println("对不起,您认证的信息有误");
                    }
                }
            } else {
                System.out.println("对不起,您输入的转账卡号有问题");
            }
        }
    }

    private static void drawMoney(Account acc, Scanner sc) {
        System.out.println("=====================取款操作=====================");
        if (acc.getMoney() >= 100) {
            while (true) {
                System.out.println("请您输入取款金额");
                double money = sc.nextDouble();
                if (money > acc.getQuotaMoney()) {
                    System.out.println("您当次取款金额超过每次限额,不要取那么多,每次最多可以取" + acc.getQuotaMoney());
                } else {
                    if (acc.getMoney() >= money) {
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您取款" + money + "成功了,当前账户余额为" + acc.getMoney());
                        return;
                    } else {
                        System.out.println("余额不足啊!");
                    }
                }
            }
        } else {
            System.out.println("您的金额没超过100元,别取了");
        }
    }

    private static void depositMoney(Account acc, Scanner sc) {
        System.out.println("===================存钱操作====================");
        System.out.println("请输入存款金额");
        double money = sc.nextDouble();

        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成");
        showAccount(acc);
    }

    private static void showAccount(Account acc) {
        System.out.println("============当前账户详情===========");
        System.out.println("卡号" + acc.getCardId());
        System.out.println("姓名" + acc.getUserName());
        System.out.println("余额" + acc.getMoney());
        System.out.println("当次限额" + acc.getQuotaMoney());
    }


    private static void register(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("========================用户开户功能=========================");
        System.out.println("请输入开户名称:");
        String name = sc.next();

        String password;
        while (true) {
            System.out.println("请输入开户密码");
            password = sc.next();

            System.out.println("请确认开户密码");
            String okPassword = sc.next();

            if (okPassword.equals(password)) {
                break;
            } else {
                System.out.println("两次密码必须一致");
            }
        }
        System.out.println("请您输入当次限额");
        double quotaMoney = sc.nextDouble();
        String cardId = createCardId(accounts);
        Account account = new Account(cardId, name, password, quotaMoney);

        accounts.add(account);
        System.out.println("恭喜您,开户成功,您的卡号是:" + account.getCardId());
    }


    public static String createCardId(ArrayList<Account> accounts) {
        Random r = new Random();
        String cardId = "";
        while (true) {
            for (int i = 0; i < 8; i++) {
                cardId += r.nextInt(10);
            }

            Account acc = getAccountByCardId(cardId, accounts);

            if (acc == null) {
                return cardId;
            }
        }
    }

    public static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {

        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId)) {
                return acc;
            }
        }

        return null;
    }
}




//Account.java

package package1;

public class Account {
    private String  cardId;
    private String  userName;
    private String  passWord;
    private double  money;
    private double  quotaMoney;

    public Account() {
    }

    public Account(String cardId, String userName, String passWord, double quotaMoney) {
        this.cardId = cardId;
        this.userName = userName;
        this.passWord = passWord;
        this.quotaMoney = quotaMoney;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    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;
    }
}

相关文章

SingleCellExperiment类使用

SingleCellExperience类用于表示单细胞测序数据。它继承自RangedS...

“21天好习惯“第一期-14

Problem Description 输入三个字符后,按各字符的ASC...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。