0
点赞
收藏
分享

微信扫一扫

JavaSE项目——ATM系统

最后的执着 2022-04-27 阅读 45

目录

1.JavaBean搭建 

2.ATMSystem.java

3.工具类框架搭建

3.1 注册

3.2 登陆

3.3 功能界面

3.4 修改限额

3.5 注销

3.6 修改密码

3.7 转账

3.8 取款

3.9 存款

3.10 查询

3.11 密码保护功能

3.12 获取随机账户id

3.13 根据id获取账户


1.JavaBean搭建 

package com.cn.bamboo;

import java.util.Objects;

public class Account {
    private String cardId;
    private String userName;
    private String passwords;
    private Double money;
    private Double quota;

    public Account() {
    }

    public Account(String cardId, String userName, String passwords, Double money, Double quota) {
        this.cardId = cardId;
        this.userName = userName;
        this.passwords = passwords;
        this.money = money;
        this.quota = quota;
    }

    public Account(String cardId, String userName, String passwords, Double quota) {
        this.cardId = cardId;
        this.userName = userName;
        this.passwords = passwords;
        this.quota = quota;
    }

    {
        this.money = 0.0;
    }

    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 getPasswords() {
        return passwords;
    }

    public void setPasswords(String passwords) {
        this.passwords = passwords;
    }

    public Double getMoney() {
        return money;
    }

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

    public Double getQuota() {
        return quota;
    }

    public void setQuota(Double quota) {
        this.quota = quota;
    }

    @Override
    public String toString() {
        return "cardId = " + cardId + ' ' +
                ", userName = " + userName + ' ' +
                ", passwords = " + passwords + ' ' +
                ", money = " + money + '$' +
                ", quota = " + quota + '$';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Account account = (Account) o;
        return Objects.equals(cardId, account.cardId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(cardId);
    }
}

2.ATMSystem.java

 

package com.cn.bamboo;

import java.util.*;

public class AMTSystem {


    public static void main(String[] args) {
        Set<Account> accountsData = new HashSet<>();
       /* accountsData.add(new Account("10101","bamboo","123456",13156413.0,50000.0));
        accountsData.add(new Account("10102","dragon","123456",0.0,50000.0));*/
        Scanner sc = new Scanner(System.in);
        System.out.println("===========================Welcome to the BambooBank ATM system===========================");
        while (true) {
            try {
                System.out.println("1.Login account");
                System.out.println("2.Register account");
                System.out.println("3.Exit");
                System.out.print("Please input 1|2|3 choose operate:");
                int command = sc.nextInt();
                switch (command) {
                    case 3:
                        System.out.println("Welcome to next time.");
                        return;
                    case 2:
                        SysUtils.register(accountsData, sc);
                        break;
                    case 1:
                        SysUtils.login(accountsData, sc);
                        break;
                    default:
                        System.out.println("Without this option.");
                        break;
                }
            }catch (InputMismatchException e){
                sc.nextLine();
                System.out.println("Input error!");
            }
        }
    }



}

3.工具类框架搭建

import java.util.*;

public class SysUtils {
    private SysUtils() {
    }
}

3.1 注册

public static void register(Set<Account> accountSet, Scanner sc) {
        while (true) {
            System.out.print("Please enter your real name:");
            String name = sc.next();
            System.out.print("Enter your password:");
            String pwd = sc.next();
            System.out.print("Please enter your password again:");
            String pwd1 = sc.next();
            if (pwd.equals(pwd1)) {
                String cardID = getRandomCardID(accountSet);
                while (true) {
                    System.out.print("Please set the transfer limit:");
                    try {
                        Double quota = sc.nextDouble();
                        Account account = new Account(cardID, name, pwd, quota);
                        boolean b = accountSet.add(account);
                        if (b) {
                            System.out.println("Creating a successful!");
                            System.out.println(account.getCardId());
                            return;
                        }
                    } catch (InputMismatchException e) {
                        sc.nextLine();
                        System.out.println("It can only be numbers!");
                    }
                }
            } else {
                System.out.println("The passwords are inconsistent!");
            }
        }
    }

3.2 登陆

public static void login(Set<Account> accountSet, Scanner sc) {
        if (accountSet.size() > 0) {
            while (true) {
                try {
                    System.out.print("Please enter your account ID:");
                    Long cardID = sc.nextLong();
                    sc.nextLine();
                    Account account = getAccountById(accountSet, cardID.toString());
                    if (account != null) {
                        System.out.print("Please enter password:");
                        String passwords = sc.nextLine();
                        if (account.getPasswords().equals(passwords)) {
                            System.out.println("land successfully!");
                            showCommand(account, accountSet, sc);
                            return;
                        }
                    } else {
                        System.out.println("The empty card!");
                    }
                } catch (InputMismatchException e) {
                    sc.nextLine();
                    System.out.println("The input does not match!");
                }
            }
        } else {
            System.out.println("ATM System is empty.");
        }
    }

3.3 功能界面

    private static void showCommand(Account account, Set<Account> accountSet, Scanner sc) {
        System.out.println(account.getUserName() + "VIP " + "welcome you login system,your card id:" + account.getCardId());
        System.out.println("=================Welcome you login BambooBank operation page is displayed=================");
        while (true) {
            try {
                System.out.println("1.query");
                System.out.println("2.deposit");
                System.out.println("3.withdrawal");
                System.out.println("4.transfer accounts");
                System.out.println("5.change password");
                System.out.println("6.Change limit");
                System.out.println("7.unregister current account");
                System.out.println("8.exit");
                System.out.print("You can continue select functions to operate:");
                int command = sc.nextInt();
                switch (command) {
                    case 1:
                        queryAccount(account);
                        break;
                    case 2:
                        depositAccount(account, sc);
                        break;
                    case 3:
                        withdrawalAccount(account, sc);
                        break;
                    case 4:
                        transferAccount(account, accountSet, sc);
                        break;
                    case 5:
                        changePassword(account, sc);
                        break;
                    case 6:
                        changeQuota(account, sc);
                        break;
                    case 7:
                        unregisterAccount(account, accountSet, sc);
                        break;
                    case 8:
                        System.out.println("Welcome to use next time!");
                        return;
                    default:
                        System.out.println("Choose error.");
                        break;
                }
            } catch (InputMismatchException e) {
                sc.nextLine();
                System.out.println("Error in format!");
            }
        }
    }

3.4 修改限额

    private static void changeQuota(Account account, Scanner sc) {
        try {
            System.out.print("Please enter a new quota: ");
            Double quota = sc.nextDouble();
            if (quota > 0) {
                account.setQuota(quota);
                System.out.println("The modification succeeded.");
                System.out.println("The current quota is : " + account.getQuota() + '$');
                System.out.println("\n\n\n");
            } else {
                System.out.println("The amount cannot be less than or equal to 0 !");
            }
        } catch (InputMismatchException e) {
            System.out.println("Not an amount unit!");
        }
    }

3.5 注销

    private static void unregisterAccount(Account account, Set<Account> accountSet, Scanner sc) {
        System.out.println("Confirm logout " + account.getCardId() + '?');
        System.out.print("Please enter yes or no :");
        String choose = sc.next();
        switch (choose) {
            case "yes":
                accountSet.remove(account);
                System.out.println("Successfully delete!");
                System.exit(0);
                break;
            case "no":
                break;
            default:
                System.out.println("Input error!");
                break;
        }
    }

3.6 修改密码

    private static void changePassword(Account account, Scanner sc) {
        System.out.print("Please enter your current password: ");
        String passwords = sc.next();
        if (passwords.equals(account.getPasswords())) {
            System.out.print("Please enter the new password: ");
            String newPasswords = sc.next();
            System.out.print("Enter the new password again: ");
            String seNewPasswords = sc.next();
            if (newPasswords.equals(seNewPasswords)) {
                account.setPasswords(newPasswords);
                System.out.println("Password changed successfully!");
                System.out.println("\n\n\n");
            } else {
                System.out.println("The passwords are inconsistent!");
            }
        } else {
            System.out.println("Wrong password!");
        }
    }

3.7 转账

    private static void transferAccount(Account account, Set<Account> accountSet, Scanner sc) {
        while (true) {
            try {
                System.out.print("Please enter the transfer ID:");
                String transferID = sc.next();
                Account transferAccount = getAccountById(accountSet, transferID);
                if (transferAccount != null && !transferAccount.equals(account)) {
                    while (true) {
                        System.out.print("Please verify the name of the remitter:");
                        String name = sc.next();
                        if (name.equals(transferAccount.getUserName())) {
                            System.out.println("Your account balance is: " + String.format("%.2f", account.getMoney()) + '$');
                            System.out.println("Please enter the transfer amount:");
                            Double transferMoney = sc.nextDouble();
                            if (transferMoney > account.getMoney()) {
                                System.out.println("Not sufficient funds.");
                            } else {
                                if (transferMoney > account.getQuota()) {
                                    System.out.println("Beyond the limit money!");
                                } else {
                                    account.setMoney(account.getMoney() - transferMoney);
                                    transferAccount.setMoney(transferAccount.getMoney() + transferMoney);
                                    System.out.println("Transfer success!");
                                    System.out.println("\n\n\n");
                                    return;
                                }
                            }
                        } else {
                            sc.nextLine();
                            System.out.println("Check failure!");
                        }
                    }
                } else {
                    System.out.println("The system does not have this account.");
                }

            } catch (InputMismatchException e) {
                sc.nextLine();
                System.out.println("Format error.");
            }
        }
    }

3.8 取款

    private static void withdrawalAccount(Account account, Scanner sc) {
        while (true) {
            try {
                System.out.println("Your current account amount is: " + account.getMoney() + '$');
                System.out.println("Please enter the amount you want to withdraw money: ");
                Double withdrawalMoney = sc.nextDouble();
                if (withdrawalMoney == 0) {
                    System.out.println("No withdrawal this time.");
                    return;
                } else if (withdrawalMoney > 0) {
                    if (withdrawalMoney < account.getMoney()) {
                        account.setMoney(account.getMoney() - withdrawalMoney);
                        System.out.println("The withdrawal is successful.\nThe current balance is: " + String.format("%.2f", account.getMoney()) + '$');
                        System.out.println("\n\n\n");
                        return;
                    } else {
                        System.out.println("The account balance is insufficient, withdraw all.");
                        Double money = account.getMoney();
                        account.setMoney(0.0);
                        System.out.println("The withdrawal money is: " + String.format("%.2f", money) + '$');
                        System.out.println("The account balance is: " + String.format("%.2f", account.getMoney()) + '$');
                        System.out.println("\n\n\n");
                        return;
                    }
                } else {
                    System.out.println("Format error, please re-enter.");
                }
            } catch (InputMismatchException e) {
                sc.nextLine();
                System.out.println("Just numbers!");
            }
        }
    }

3.9 存款

    private static void depositAccount(Account account, Scanner sc) {
        while (true) {
            try {
                System.out.print("Please enter the deposit money:");
                Double money = sc.nextDouble();
                if (money <= 0) {
                    System.out.println("Amount input error!");
                } else {
                    account.setMoney(account.getMoney() + money);
                    System.out.println("Deposit successfully!");
                    System.out.println("The current account balance is: " + String.format("%.2f", account.getMoney()) + '$');
                    System.out.println("\n\n\n");
                    return;
                }
            } catch (InputMismatchException e) {
                sc.nextLine();
                System.out.println("Just numbers!");
            }
        }
    }

3.10 查询

    private static void queryAccount(Account account) {
        if (account != null) {
            String pwd = passwordPrivacyProtection(account);
            System.out.println("\n\n\n");
            System.out.println("Account card ID: " + account.getCardId());
            System.out.println("Account username: " + account.getUserName());
            System.out.println("Account password: " + pwd);
            System.out.println("Account money: " + String.format("%.2f", account.getMoney()) + '$');
            System.out.println("Account quota money: " + String.format("%.2f", account.getQuota()) + '$');
            System.out.println("\n\n\n");
        } else {
            System.out.println("Account is null.");
        }
    }

3.11 密码保护功能

    private static String passwordPrivacyProtection(Account account) {
        String pwd = "";
        StringBuffer sb = new StringBuffer(pwd);
        int len = account.getPasswords().length();
        for (int i = 0; i < len; i++) {
            sb.append('*');
        }
        pwd = sb.toString();
        return pwd;
    }

3.12 获取随机账户id

    private static String getRandomCardID(Set<Account> accountSet) {
        Random r = new Random();
        String datas = "0123456789";
        while (true) {
            String cardID = "6217008888";
            StringBuffer sb = new StringBuffer(cardID);
            for (int i = 0; i < 6; i++) {
                int ran = r.nextInt(10);
                char ch = datas.charAt(ran);
                sb.append(ch);
            }
            Account account = getAccountById(accountSet, sb.toString());
            if (account == null) {
                cardID = sb.toString();
                return cardID;
            }
        }

    }

3.13 根据id获取账户

    private static Account getAccountById(Set<Account> accountSet, String cardID) {
        Iterator<Account> i = accountSet.iterator();
        while (i.hasNext()) {
            Account account = i.next();
            if (account.getCardId().equals(cardID)) {
                return account;
            }
        }
        return null;
    }
举报

相关推荐

0 条评论