java course design - bank management system

Bank Management System (java)

Environment :
idea2020
jdk1.8

Functions that can be realized:
1. Register account
2. Log in
3. Query account information
4. Deposit
5. Withdrawal
6. Transfer to another account
7. Change account password
8. Cancel account

project structure

project structure

project demo

1. Main page:

insert image description here
2. Register an account:
(1) Enter the user name, password, confirm the password, and withdraw cash.
(2) When the two password inputs are inconsistent, the system prompts to re-enter, and the account registration cannot be completed until the two inputs are consistent.
(3) The card number is a series of 8-digit numbers randomly generated by the system, and the generated card numbers will not be repeated.
(4) After completing the account registration, switch to the main menu, and then perform the login operation.
insert image description here
3. Login interface
(1) When there is no account in the system, you cannot log in (for the convenience of testing, first write an account in the code, you can delete or cancel the account and then test): (2) Login after registering an account: when logging
insert image description here
in Verify the card number, password, any input error will prompt to re-enter, until the input is correct, you can log into the operation menu.
Login without any errors:
insert image description here
Login with errors:
insert image description here
4. There are 7 functions in the operation main menu
(1) Query: display the card number, user name, balance, and cash withdrawal amount of the login account.
insert image description here
(2) Deposit: Input the deposit amount, and re-output the account information after deposit.
insert image description here

(3) Withdrawal: Enter the withdrawal amount to determine whether it exceeds the single limit? Judging whether the balance is exceeded? And give the corresponding prompt information until the money can be withdrawn, and give the account information after withdrawal after the money is withdrawn.
insert image description here

(4) Transfer to another account: The premise is that there must already be two accounts.
There are no two accounts.
insert image description here
There are two accounts: Verify that the other party’s card number is correct? Verify the person's last name/name? Is the transfer amount less than the balance?
After the verification is successful, the transfer operation is completed, and finally the current account balance is prompted.
insert image description here

(5) Modify the account password: Verify the original password. After successful verification, enter the new password twice. The new password must be the same. After the verification is successful, set a new password. After the setting is completed, it will automatically log out of the account and return to the main menu. You need to log in again to continue the operation.
insert image description here

(6) Exit: Exit the account and return to the main menu interface.
insert image description here

(7) Cancel account: delete the current account (object) from the object collection.
Unsuccessful account cancellation: y is not entered, and the account still has a balance.
insert image description here
Successful account cancellation: the account balance is 0. After confirming the account cancellation, the login card number no longer exists.
insert image description here

Full code:

1. Define the account class Account: used to store the basic information of the account

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) {
    
    
        CardId = cardId;
        UserName = userName;
        Password = password;
        this.quotaMoney = quotaMoney;
    }

    public Account(String cardId, String userName, String password, double money, double quotaMoney) {
    
    
        CardId = cardId;
        UserName = userName;
        Password = password;
        this.money = money;
        this.quotaMoney = quotaMoney;
    }

    public String getCardId() {
    
    
        return CardId;
    }

    public void setCardId(String cardId) {
    
    
        CardId = cardId;
    }

    public String getUserName() {
    
    
        return UserName;
    }

    public void setUserName(String userName) {
    
    
        UserName = userName;
    }

    public String getPassword() {
    
    
        return Password;
    }

    public void setPassword(String password) {
    
    
        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. Define the self-service bank ATMSystem class: used to complete various functions of the bank

import javafx.scene.transform.Scale;
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<>();

        //为了测试方便,写好的一个账户信息(以下三行可以删除)
        Scanner sc=new Scanner(System.in);
        Account account=new Account("12345678","123","123",0,100);
        accounts.add(account);

        //主菜单
        while(true){
    
    
            System.out.println("-----------欢迎来到ATM系统----------------");
            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("输入命令不存在");
            }
        }

    }
    /**
     * 注册
     * @param accounts
     * @param sc
     */
    private static void register(ArrayList<Account> accounts, Scanner sc) {
    
    
        System.out.println("===============欢迎进入开户界面=====================");
        System.out.print("请输入用户名:");
        String userName = sc.next();
        String password = null;
        String password1 = null;
        while (true) {
    
    
            System.out.print("请输入密码:");
            password = sc.next();
            System.out.print("请确认密码:");
            password1 = sc.next();
            if(password.equals(password1)){
    
    
                break;
            }else{
    
    
                System.out.println("两次密码不一致,请重新输入");
            }
        }
        System.out.print("请设置当日取现额度:");
        Double quotaMoney = sc.nextDouble();
        //随机8位不重复账号(独立方法)
        String cardId=getRandomId(accounts);

        Account account=new Account(cardId,userName,password,quotaMoney);
        accounts.add(account);
        System.out.println("恭喜你"+userName+"先生/女士,开户成功,您的卡号为:"+cardId);
    }
    /**
     * 生成随机8位不重复账号
     * @param accounts
     * @return
     */
    private static String getRandomId(ArrayList <Account> accounts) {
    
    
        Random r=new Random();
        while (true) {
    
    
            String cardId="";
            for (int i = 0; i <8 ; i++) {
    
    
                cardId+=r.nextInt(10);//0-9
                }
            Account acc = getAccountByCardId(accounts, cardId);
            if(acc==null){
    
    //卡号没有重复
                return cardId;
            }else{
    
    
                return null;
            }
        }
    }
    /**
     * 根据卡号判断是否存在账户
     * @param accounts
     * @param cardId
     * @return
     */
    private static Account getAccountByCardId(ArrayList <Account> accounts,String cardId){
    
    
        for (int i = 0; i < accounts.size(); i++) {
    
    
            Account a=accounts.get(i);
            if(a.getCardId().equals(cardId)){
    
    
                return a;
            }
        }
        return null;//查无此账号!

    }
    /**
     * 用户登录
     * @param accounts
     * @param sc
     */
    private static void login(ArrayList <Account> accounts,Scanner sc) {
    
    //登录
        System.out.println("===============欢迎进入登录界面=====================");
        if(accounts.size()==0){
    
    
            System.out.println("该系统中无任何账户,请先开户");
            return;//结束这个方法
        }
        while (true) {
    
    
            System.out.print("请输入卡号:");
            String cardId= sc.next();
            //根据卡号找账户对象存在?
            Account acc=getAccountByCardId(accounts,cardId);
            if(acc!=null){
    
    //卡号存在
                while (true) {
    
    
                    System.out.print("请输入密码:");
                    String password= sc.next();
                    if (acc.getPassword().equals(password)) {
    
    
                        System.out.println("恭喜你!"+acc.getUserName()+"登录成功!"+"卡号:"+acc.getCardId());
                        //查询,转账,取款功能
                        showCommand(accounts,acc,sc);
                        return;//干掉登录方法
                        //break;
                    } else {
    
    
                        System.out.println("密码错误!请重新输入");
                    }
                }
               // break;
            }else{
    
    
                System.out.println("该账户卡号不存在!");
            }
        }
    }
    /**
     * 登陆后的操作界面
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void showCommand(ArrayList accounts,Account acc,Scanner sc) {
    
    //操作界面
        while (true) {
    
    
            System.out.println("===============登陆成功欢迎进入用户操作界面=====================");
            System.out.print("1.查询");
            System.out.print("2.存款");
            System.out.print("3.取款");
            System.out.print("4.转账");
            System.out.print("5.修改密码");
            System.out.print("6.退出");
            System.out.print("7.注销当前账户");
            System.out.print("请选择你的操作:");
            int command1=sc.nextInt();
            switch(command1){
    
    
                case 1:
                    query(acc);//展示
                    break;
                case 2:
                    depositMoney(acc,sc);//存钱
                    break;
                case 3:
                    drawMoney(acc,sc);//取钱
                    break;
                case 4:
                    zhuanzhang(accounts,acc,sc);//转账
                    break;
                case 5:
                    updatePassword(acc,sc);//修改密码
                    return;//重新登录
                case 6:
                    System.out.println("退出成功,欢迎下次光临");
                    return;//干掉当前show方法
                case 7:
                    if(deleteAccount(accounts,acc,sc)){
    
    ;//销户
                        return;
                    }else{
    
    
                        break;
                    }

                default:
                    System.out.println("输入命令不存在");

            }
        }

    }
    /**
     * 展示账户信息
     * @param acc
     */
    private static void query(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());
    }
    /**
     * 存款
     * @param acc
     */
    private static void depositMoney(Account acc,Scanner sc) {
    
    
        System.out.println("======================存钱=========================");
        System.out.print("请输入存款金额:");
        acc.setMoney(sc.nextDouble()+acc.getMoney());
        System.out.println("存款成功!");
        query(acc);
    }
    /**
     * 取钱
     * @param acc
     * @param sc
     */
    private static void drawMoney(Account acc, Scanner sc) {
    
    
        System.out.println("======================取钱=========================");
        if (acc.getMoney() < 100) {
    
    
            System.out.println("账户余额小于100元,不能取出");
            return;
        } else {
    
    
            while (true) {
    
    
                System.out.print("请输入要取款的金额:");
                double money = sc.nextDouble();
                if (money > acc.getQuotaMoney()) {
    
    //超过单次超过限额
                    System.out.println("超过单次限额,每次最多取款 " + acc.getQuotaMoney() + "元");
                } else {
    
    
                    if (money > acc.getMoney()) {
    
    //超过余额
                        System.out.println("账户余额不足,账户余额为"+acc.getMoney()+"元");
                    } else {
    
    
                        System.out.println("取款"+money+"成功");
                        acc.setMoney(acc.getMoney() - money);
                        query(acc);
                        return;//干掉这个方法
                    }
                }
            }
        }
    }
    /**
     * 转账:必须有>=两个账户
     * @param
     * @param acc
     * @param sc
     */
    private static void zhuanzhang(ArrayList accounts, Account acc, Scanner sc) {
    
    
        System.out.println("======================转账=========================");
        //1.自己有钱?
        if(accounts.size()<2){
    
    
            System.out.println("当前系统账户不足2个,无法转账");
            return;
        }
        //有两个账户?
        if(acc.getMoney()==0){
    
    
            System.out.println("自己都没钱,就别转了");
            return;
        }
        while (true) {
    
    
            System.out.print("请输入对方卡号:");
            String cardId = sc.next();
            if(acc.getCardId().equals(cardId)){
    
    
                System.out.print("无法向自己账户转钱!");
                continue;//结束当前执行,死循环进入下一次
            }
            Account a=getAccountByCardId(accounts,cardId);//这个卡号存在?
            if(a==null){
    
    
                System.out.print("不存在该账户,请重新输入:");
            }else{
    
    //存在
                String userName = a.getUserName();
                String after = userName.substring(1);
                while(true){
    
    
                    System.out.print("您当前将为*"+after+"转账!请输入姓氏确认:");
                    String before=sc.next();
                    if(userName.startsWith(before)){
    
    
                    //if(userName.substring(0,1).equals(before)){
    
    
                        System.out.println("账户验证成功");
                        while(true){
    
    
                            System.out.print("请输入转账金额:");
                            Double money=sc.nextDouble();
                            if(money>acc.getMoney()){
    
    
                                System.out.print("余额不足,您当前余额为"+acc.getMoney()+"。  ");
                            }else {
    
    
                                acc.setMoney(acc.getMoney() - money);
                                a.setMoney(a.getMoney() + money);
                                System.out.println("转账成功!您的账户余额为"+acc.getMoney());
                                return;
                            }
                        }
                    }else{
    
    
                        System.out.println("验证失败!请重新输入");
                    }
                }
            }

        }
    }
    /**
     * 改密码
     * @param acc
     * @param sc
     */
    private static void updatePassword(Account acc, Scanner sc) {
    
    
        System.out.println("====================修改密码=========================");
        while (true) {
    
    
            System.out.print("请输入原始密码:");
            String oldPassword = sc.next();
            if(acc.getPassword().equals(oldPassword)) {
    
    
                while (true) {
    
    
                    System.out.print("请输入新密码");
                    String newPassword = sc.next();
                    System.out.print("请确认新密码");
                    String newPassword1 = sc.next();

                    if (newPassword.equals(newPassword1)) {
    
    
                        acc.setPassword(newPassword);
                        System.out.println("密码修改成功!请重新登录");
                        return;
                    } else {
    
    
                        System.out.println("两次输入不一致!");
                    }
                }
            }else{
    
    
                System.out.print("输入的原密码输入错误,");
            }
        }
    }
    /**
     * 注销(删除)账户
     * @param accounts
     * @param acc
     * @param sc
     */
    private static boolean deleteAccount(ArrayList accounts, Account acc, Scanner sc) {
    
    
        System.out.println("====================注销账户=========================");
        System.out.print("您真的要销户吗?y/n");
        String next = sc.next();
        switch(next){
    
    
            case "y":
                if(acc.getMoney()>0){
    
    
                    System.out.println("您的账户还有"+acc.getMoney()+"元,不予许销户");
                }else{
    
    
                    System.out.println("账户销户完成!");
                    accounts.remove(acc);
                    return true;
                }
                break;
            default:
                System.out.println("账户继续保留!");
        }
        return false;
    }
}

There are still areas that can be improved, such as: the transfer limit is not judged, it is all the same.
If you don’t understand, you can contact qq: 2559347978 for communication

Guess you like

Origin blog.csdn.net/qq_46026355/article/details/125729286