Java简易银行系统

目录

简介

一、BankSystem: 主操作系统

二、BankAccount: 账户管理

三、SavingsAccount: 储蓄账户

四、CheckingAccount: 支票账户

五、关于操作

BankSystem.java

BankAccout.java

CheckingAccount.java

SavingAccount.java


简介

编写一个在控制台操作的银行账户系统,基本功能包括开户、账号密码登录、存取款、查询余额和转账,扩展功能包括开户时账户类型的选择(存款账户与支票账户),以及对存款账户统一发放利息和对支票账户统一进行扣税。

一、BankSystem: 主操作系统

    0--退出
    1--注册:默认注册SavingsAccount,如果确认注册支票账户,则为CheckingAccount;
    2--登录:统一登录,CheckingAccount具有SavingsAccount基础存储功能(但扣税),向转账指定ID功能(余额不足或ID非CheckingAccount不予转账);
    3--管理:实现统一发放利息和对支票账户统一进行设置扣税额和限制参数;默认密码:administrator

二、BankAccount: 账户管理

    super类;
    提款金额大于余额,不予处理。

三、SavingsAccount: 储蓄账户

    账户ID、姓名、密码等信息;
    1、2--存取款;
    3--查余额;
    4--查详情。

四、CheckingAccount: 支票账户

    账户ID、姓名、密码等信息;
    1、2--存取款(纳入扣税);
    3--查余额;
    4--查详情;
    5--转账:纳入扣税,向转账指定ID(非自身)。

五、关于操作

    用户注册(没有设置默认账户,且限制数量1000账户内);
    用户登录(注册的账户默认定义的ID和用户自定义密码);
    用户从administrator在重置各参数(操作存在提示是否退出:{ N }+ 再次输入密码:{ administrator } 可继续进行操作,{ Y } 确认退出);
    用户再次登录查看;

BankSystem.java

import java.util.Scanner;
public class BankSystem extends BankAccount{
    public static BankAccount[] accounts = new BankAccount[1000];
    public static int cnt = 0;

    public BankSystem(){}
    //主界面
    public void mainWindow()
    {
        System.out.println();
        System.out.print("______________________________\n");
        System.out.println("\t\tBank System");
        System.out.println("0-Exit\t1-Register\t2-LogIn\t3-Administrator");
        System.out.print("______________________________\n");
        System.out.print("choose: ");
    }
    //注册
    public void register()
    {
        Scanner s = new Scanner(System.in);
        boolean fullFlag = false;//判满标签
        String id = String.valueOf(cnt);
        //分配ID
        if(cnt >= 1000)
        {
            System.out.println("WARNING: FULL");
            fullFlag = true;
        }
        else if(cnt < 100 && cnt >= 10)
        {
            id = '0' + id;
        }
        else if(cnt < 10 && cnt >= 0)
        {
            id = "00" + id;
        }
        System.out.println("\n_____________________________\n\t\tBank Register");
        if (fullFlag == true)
        {
            System.out.println("WARING: FULL");
        }
        else
        {
            System.out.println("ID: " + id);
            System.out.print("Name: ");
            String name = s.nextLine();
            System.out.print("Password: ");
            String pw = s.nextLine();
            while (true)//确认密码
            {
                System.out.print("Confirm Password: ");
                String cpw = s.nextLine();
                if(cpw.equals(pw))
                {
                    break;
                }
                else
                {
                    System.out.println("WARNING: DOSE NOT EQUAL");
                }
            }
            while (true)
            {
                System.out.print("Whether this account register as a Checking Account? (Y/N): ");//支票
                String type = s.nextLine();
                if (type.equals("Y"))
                {
                    accounts[cnt++] = new CheckingAccount(id, name, pw, 0);
                    break;
                } else if (type.equals("N"))
                {
                    accounts[cnt++] = new SavingsAccount(id, name, pw, 0);
                    break;
                }
            }
            System.out.println(accounts[cnt-1].getName() +" registers successfully");
        }
        System.out.println("_____________________________");
    }
    //转账
    public void transfer(int num)
    {
        boolean exitFlag = false;
        int begin;
        int end;
        String type;
        Scanner s = new Scanner(System.in);
        while (true)
        {
            String tid;
            int illegal = 0;
            while (true)
            {
                System.out.print("Transfer to ID: ");
                tid = s.nextLine();
                if (tid.length() == 3 && Integer.valueOf(tid) < cnt && !tid.equals(accounts[num].getID()))//转账ID限制
                {
                    System.out.print("Your password: ");
                    if ((s.nextLine()).equals(accounts[num].getPassword()))
                    {
                        break;
                    }
                }
                else
                {
                    System.out.println("WARING: ILLEGAL");
                    if (illegal == 3)//多次失败操作视为非法
                    {
                        exitFlag = true;
                        break;
                    }
                    illegal++;
                }
            }
            if (illegal != 3)
            {
                System.out.print("Transfer amount: ");
                double money = s.nextDouble();
                if ((accounts[num].getID()).compareTo(tid) < 0)
                {
                    begin = num + 1;
                    end = cnt ;
                }
                else
                {
                    begin = 0;
                    end = num;
                }
                while (begin < end)
                {
                    if ((accounts[begin].getID()).equals(tid)  && (accounts[begin] instanceof CheckingAccount))//找CheckingAccount ID
                    {
                        ((CheckingAccount)accounts[num]).withdraw(money);
                        if (accounts[num].getWithdrawAmount() > 0)
                        {
                            ((CheckingAccount)accounts[begin]).deposit(money);
                            System.out.println("Successfully transfer amount: " + accounts[num].getWithdrawAmount() + " to ID: "+ accounts[begin].getID());
                        }
                        else
                        {
                            System.out.println("Your balance is not enough.");
                        }
                        exitFlag = true;
                        break;
                    }
                    begin++;
                }
                if (exitFlag == false)
                {
                    System.out.println("Fail to transfer amount: " + money + " to " + accounts[begin].getID());
                    while (true) {
                        type = s.nextLine();
                        if (type.equals("Y")) {
                            exitFlag = true;
                            break;
                        } else if (type.equals("N")) {
                            break;
                        } else {
                            System.out.print("Whether exit? (Y/N): ");
                        }
                    }
                }
            }

            if (exitFlag == true)
            {
                break;
            }
        }
    }
    //登录
    public void logIn()
    {
        if (cnt == 0)
        {
            System.out.println("WARING: NONE DATA");
        }
        else
        {
            Scanner s = new Scanner(System.in);
            boolean flag = false;
            boolean exitFlag = false;
            while (true)
            {
                System.out.print("ID: ");
                String id = s.nextLine();
                System.out.print("Password: ");
                String pw = s.nextLine();
                int i = 1;
                for (i = 0; i < cnt; i++)
                {
                    if (id.equals(accounts[i].getID()) && pw.equals(accounts[i].getPassword()))
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag == false)
                {
                    System.out.println("WARNING: ID or password is wrong.");
                }
                else
                {
                    while (true)
                    {
                        System.out.println("_____________________________\n\t\tBank Account");
                        System.out.println("Welcome " + accounts[i].getName() + "\tAccount type: " + accounts[i].getAccountType());
                        System.out.print("0-Exit\t1-Deposit\t2-Withdraw\t3-Balance\n4-Info\t");//可转账
                        if (accounts[i] instanceof CheckingAccount)
                        {
                            System.out.println("5-Transfer");
                        }
                        else if (accounts[i] instanceof SavingsAccount)
                        {
                            System.out.println();
                        }
                        System.out.print("______________________________\nchoose: ");
                        int op = s.nextInt();
                        switch (op)
                        {
                            case 1:
                                System.out.print("Deposit amount: ");
                                accounts[i].deposit(s.nextDouble());
                                System.out.println("Successfully deposit.");
                                break;
                            case 2:
                                System.out.print("Withdraw amount: ");
                                accounts[i].withdraw(s.nextDouble());
                                System.out.println("Withdraw: " + accounts[i].getWithdrawAmount());
                                break;
                            case 3:
                                System.out.println("Balance: " + accounts[i].getBalance());
                                break;
                            case 4:
                                System.out.println(accounts[i].print());
                                break;
                            case 5:
                                if (accounts[i] instanceof SavingsAccount)
                                {
                                    System.out.println("WARING: ILLEGAL");
                                }
                                else if (accounts[i] instanceof CheckingAccount)
                                {
                                    transfer(i);
                                }
                                break;
                            case 0:
                                exitFlag = true;
                                break;
                            default:
                                System.out.println("WARING: ILLEGAL");
                                break;
                        }
                        if (exitFlag == true)
                        {
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }
    //管理
    public void admin()
    {
        Scanner s = new Scanner(System.in);
        boolean exitFlag = false;
        String flag;
        System.out.print("Whether exit? (Y/N): ");
        while (true)
        {
            flag = s.nextLine();
            if (flag.equals("Y"))
            {
                break;
            }
            else if (flag.equals("N"))
            {
                System.out.print("PASSWORD: ");
                if (s.nextLine().equals("administrator"))//默认密码
                {
                    System.out.print("1-Adjust interest rate\t2-Deduct\t3-Exit\nchoose: ");
                    int op = s.nextInt();
                    switch (op)
                    {
                        case 1:
                            double rate = 0;
                            for (BankAccount account : accounts)
                            {
                                if (account instanceof SavingsAccount)
                                {
                                    rate = ((SavingsAccount)account).getInterestRate();
                                    break;
                                }
                            }
                            System.out.print("Interest rate: " + rate +"\nAdd: ");
                            double add = s.nextDouble();
                            for (BankAccount account : accounts)
                            {
                                if (account instanceof SavingsAccount)
                                {
                                    ((SavingsAccount)account).addInterest(add);
                                    ((SavingsAccount)account).addInterest();
                                    rate = ((SavingsAccount)account).getInterestRate();
                                }
                            }
                            System.out.println("Successfully. The interest rate is " + rate);
                            break;
                        case 2:
                            System.out.print("Deduction amount: ");
                            double fees = s.nextDouble();
                            System.out.print("Deduction times: ");
                            int times = s.nextInt();
                            for (BankAccount account : accounts)
                            {
                                if (account instanceof CheckingAccount)
                                {
                                    ((CheckingAccount)account).setTax(fees);
                                    ((CheckingAccount)account).deductFees(times);
                                }
                            }
                            System.out.println("Successfully. The deduction fees is " + fees);
                            break;
                        case 3:
                            exitFlag =true;
                            break;
                        default:
                            System.out.println("WARING: ILLEGAL");
                            break;
                    }
                    if (exitFlag == true)
                    {
                        break;
                    }
                }
                else
                {
                    System.out.print("WARING: WRONG! Whether exit? (Y/N):");
                    if (flag.equals("Y"))
                    {
                        break;
                    }
                }
            }
            else
            {
                System.out.print("Whether exit? (Y/N): ");//拒绝多次操作
            }
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        BankSystem bs = new BankSystem();
        while (true)
        {
            bs.mainWindow();
            int op = s.nextInt();
            boolean exitFlag = false;//退出标签
            switch (op)
            {
                case 1:
                    bs.register();
                    break;
                case 2:
                    bs.logIn();
                    break;
                case 3:
                    bs.admin();
                    break;
                case 0:
                    exitFlag = true;
                    break;
                default:
                    System.out.println("WARNING: ILLEGAL");
            }
            if (exitFlag == true)
            {
                break;
            }
        }
    }
}

BankAccout.java

public class BankAccount {
    private String id, name, password;
    private double balance, withdrawAmount;
    private static String accountType;
    public BankAccount()
    {
    }
    public BankAccount(String id, String name, String password, double balance)
    {
        this.id = id;
        this.name = name;
        this.password = password;
        this.balance = balance;
    }
    public void deposit(double amount)
    {
        balance += amount;
    }
    public void  withdraw(double amount)
    {
        if (balance >= amount)
        {
            balance -= amount;
            this.withdrawAmount = amount;
        }
        else
        {
            this.withdrawAmount = 0;
        }
    }
    public double getBalance()
    {
        return balance;
    }
    public String getID()
    {
        return id;
    }
    public String getName()
    {
        return name;
    }
    public String getPassword()
    {
        return password;
    }
    public double getWithdrawAmount()
    {
        return withdrawAmount;
    }
    public String getAccountType()
    {
        return accountType;
    }
    public String print()
    {
        return "ID: " + getID() + " ; Name: " + getName();
    }
}

CheckingAccount.java

public class CheckingAccount extends BankAccount {
    private int transactionCount;
    private double tax;
    private static String accountType = "Checking";
    public CheckingAccount()
    {
    }
    public CheckingAccount(String id, String name, String pw, double balance)
    {
        super(id, name, pw, balance);
    }
    public void deposit(double amount)
    {
        if (amount > 0)
        {
            super.deposit(amount);
            transactionCount++;
        }
    }
    public void withdraw(double amount)
    {
        if (super.getBalance() >= amount && amount > 0)
        {
            super.withdraw(amount);
            transactionCount++;
        }
        else
        {
            super.withdraw(amount);
        }
    }
    public void setTax(double fees)
    {
        this.tax = fees;
    }
    public void deductFees(int times)
    {
        if(transactionCount > times)
        {
            double fees = (transactionCount - times) * tax;
            super.withdraw(fees);
        }
        transactionCount = 0;
    }
    public int getTransactionCount()
    {
        return transactionCount;
    }
    public double getTax()
    {
        return tax;
    }
    public String getAccountType()
    {
        return accountType;
    }
    public String print()
    {
        return "ID: " + getID() + " ; Name: " + getName() +
                ";\n\tBalance: " + getBalance() + " ; TransactionCount: " + getTransactionCount() + " ; Tax: " + getTax();
    }

}

SavingAccount.java

public class SavingsAccount extends BankAccount {
    private double interestRate;
    private static String accountType = "Saving";
    public SavingsAccount(String id, String name, String pw, double balance)
    {
        super(id, name, pw, balance);
    }
    public void addInterest(double add)
    {
        interestRate += add;
    }
    protected double countInterest()
    {
        return interestRate * getBalance();
    }
    public double getInterestRate()
    {
        return interestRate;
    }
    public void addInterest()
    {
        deposit( countInterest() );
    }
    public String getAccountType()
    {
        return accountType;
    }
    public String print()
    {
        return "ID: " + getID() + " ; Name: " + getName() +
                ";\n\tBalance: " + getBalance() + " ; InterestRate: "+ getInterestRate();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值