JAVA exercises --- bank account management system

Hi everyone, this is my first blog. I was learning Java some time ago, and I will share a comprehensive exercise with you, so let's not talk nonsense and code! ! !

Bank Account Management System
Project name : Bank Account Management System ( BAM for short)
Project description : This is a bank account management system , users can operate their own bank accounts .
Project implementation method : This is a synchronous exercise . With the deepening of the Java course , this project will become more complete . The task of the students is to complete each advanced project requirement with the deepening of knowledge points.
1.

 

Account class code

public class Account {
    private long id;
    private static long pid; //用来分配账号的id
    private String password;
    private String name;
    private String personId;
    private double balance;

    public Account() {
    }

    public Account(long id, String password, String name, String personId, double balance) {
        this.id = id;
        this.password = password;
        this.name = name;
        this.personId = personId;
        this.balance = balance;
    }

    //成员方法
    public double deposit(double money) {
        balance+=money;
        return balance;
    }
    //取款
    public boolean withdraw(double money) {
        if (balance>money) {
            balance-=money;
            return true;
        }
        else {
            System.out.println("取款数目过大");
            return false;
        }
    }


    //练习2:封装

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(String personId) {
        this.personId = personId;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

2. Credit accounts and savings accounts Inherited accounts  

The method with parameters of the parent class will not be automatically inherited, and super (parameter) can be used in the construction method of the subclass to call the construction method of the parent class

package entity;

/*
  信用账户
 */
public class CreditAccount extends Account {
    public CreditAccount(long id,String name,String personId,String password,double balance) {
        super(id, password, name, personId, balance);
    }
    //取款,对父类方法的重写
    //信用账户可以透支
    @Override
    public boolean withdraw(double money) {
        if (money>super.getBalance()+10000) {
            //取钱透支超过10000元
            return false;
        }else {
            //将余额中的钱减少
            super.setBalance(super.getBalance()-money);
            return true;
        }
    }
}
package entity;

/*
 储蓄账户
 */
public class SavingAccount extends Account {
    public SavingAccount(long id,String name,String personId,String password,double balance) {
        super(id, password, name, personId, balance);
    }

    @Override
    public boolean withdraw(double money) {
        if (money>super.getBalance()){
            return false;
        }else {
            super.setBalance(super.getBalance()-money);
            return true;
        }
    }
}

 Bank class code

package 银行账户管理系统.biz;
import com.sun.source.tree.IfTree;
import entity.Account;
import entity.CreditAccount;
import entity.SavingAccount;

/*
 银行类
 1.当前所有账户对象的集合,存放在数组中
 2.当前账户数量
 方法:
 1.用户开户,需要的参数: id,密码,密码确认,姓名,身份证号码,账户类型,返回新创建的Account对象的账号
 2.用户登录,参数:id,密码 返回登陆账户的账号
 3.用户存款,参数:id,存款数额,返回void
 4.用户取款,参数:id,取款数额,返回void
 5.查询余额,参数:id,返回该账户的余额double
 用户会通过调用Bank对象以上的方法来操作自己的账户
 */
public class Bank {
    //当前所有的账户对象的集合,存放在数组中,对数组声明并初始化
    private Account accounts[] = new Account[20];//账户集合
    private int number; //账户数目

    public Bank() {
        accounts = new Account[20];
        number = 0;
    }

    private Account selectAccountById(long id) {
        Account acu=null;
        for (int i=1;i<accounts.length;i++){
            if (accounts[i].getId()==id) {
                //根据id找到用户 如果从数组中找到相应的卡号和密码,则返回整个对象
                acu=accounts[i];
                break;
            }
        }
        return acu;
    }

    //开户方法
    public Account openAccount(long id, String pass1, String pass2, String name, String personID, int type) {
        //创建一个新帐户
        Account acc = null;
        //判断两次密码是否一致
        if (pass1.equals(pass2)) {
            //如果返回为真,则说明密码一致
            //账户类型是一个整数,为0的时候表示储蓄账户,为1的时候表示信用账户
            if (type == 1) {
                acc = new CreditAccount(id, name, pass1, personID, 1);
                //刚开卡,可以定义balance=1
            }else{acc = new SavingAccount(id, name, pass1, personID, 1);}
                return acc;
            } else {//如果两次密码不一致,则开卡不成功,返回一个null。
                return null;
            }
        }

        //登录
        public Account login(long id,String password) {
        //遍历数组
            Account acu=selectAccountById(id);
             if (acu==null) {
                 return null;
             }else {
                 if (acu.getPassword().equals(password)){
                     //判断密码是否正确 对了就返回对象
                     return acu;
                 }else {return null;}
             }
        }

        //存款
       public void saveMoney(long id,double money) {
        //直接调用账户类里方法
           Account ac=selectAccountById(id);
           ac.deposit(money);
       }

       //取款
       public void outputMoney(long id,double money) {
        Account ac=selectAccountById(id);
        ac.withdraw(money);
       }

       //查询余额
      public double selectMoney(long id){
        Account ac=selectAccountById(id);
        return ac.getBalance();
      }
    }

 Test page code writing ideas

  1. Write the main page of the output statement 

  2. After the scanner implements keyboard input data, the switch calls the method in the Bank in a loop. Of course, the method written in the main page can also be written in the Bank class, and the method can be called in the test page.

That's all for today's update! Do not do well, please give more advice!

Guess you like

Origin blog.csdn.net/wuyomhchang/article/details/123577099