欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Java编写银行账户类(面向对象思考实验)

程序员文章站 2022-04-01 20:50:21
银行账户类(Accoount)可以为账户存储交易。取款和存款都会产生交易信息。需要记录账户每次交易(Transaction)的信息,包括取钱还是存钱,交易的金额,交易时间和交易之后的余额。系统可以通过账户查询所有的交易次数(最多10次)。问题一:为了实现此功能需要设计几个类?答:两个类,Account类和Transaction类。问题二:请写出你的设计的类的代码(先画UML图在展示代码)。UML图:Account类:Transaction类:注意: 两者之间为单项关联代码如下:Acco...

银行账户类(Accoount)可以为账户存储交易。取款和存款都会产生交易信息。需要记录账户每次交易(Transaction)的信息,包括取钱还是存钱,交易的金额,交易时间和交易之后的余额。系统可以通过账户查询所有的交易次数(最多10次)。
问题一:为了实现此功能需要设计几个类?
答:两个类,Account类和Transaction类。
问题二:请写出你的设计的类的代码(先画UML图在展示代码)。
UML图:
Account类:
使用Java编写银行账户类(面向对象思考实验)

Transaction类:
使用Java编写银行账户类(面向对象思考实验)
注意: 两者之间为单项关联

代码如下:
Account类中:

public class Account {
    //名字
    private String name;
    //用户id
    private int id = 0;
    //账户余额
    private double balance = 0;
    //账户年利率
    private static double annuallnterestRate = 0;
    //账户创建的日期
    private Date dateCreated;
    //用于存储账户所有的交易信息
    private Transaction[] transactions = new Transaction[size];
    //用于记录账户交易信息存储的位置
    private int top = 0;
    //能够记录账户最大的交易信息数量
    private static int size = 10;

    //无参创`在这里插入代码片`建账户
    public Account() {
        dateCreated = new Date();
    }

    //带特定id和初始余额开通账户
    public Account(double annuallnterestRate, int id, double balance,String name) {
        this.annuallnterestRate = annuallnterestRate;
        this.id = id;
        this.balance = balance;
        this.name = name;
        dateCreated = new Date();
    }

    //get和set方法



    public int getId() {
        return id;
    }

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

    public double getBalance() {
        return balance;
    }

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

    public static double getAnnuallnterestRate() {
        return annuallnterestRate;
    }

    public static void setAnnuallnterestRate(double annuallnterestRate) {
        Account.annuallnterestRate = annuallnterestRate;
    }

    //获取用户创建日期
    public Date getDataCreated() {
        return dateCreated;
    }

    //获取月利润
    public double getMonthlyInterest() {
        return (balance * annuallnterestRate / 12) / 100;
    }

    //取钱
    public void withDraw(double amount) {
        balance = balance - amount;
        if(top<size){
            transactions[top++] = new Transaction('W',amount,balance);
        }else{
            //如果超过了数组存储的最大容量,则删除第一个账单,后面的账单依次往前移,新帐单存在最后一个位置
            for(int i = 0;i<size-1;i++){
                transactions[i] = transactions[i+1];
                transactions[size-1] = new Transaction('W',amount,balance);
            }
        }
    }

    //存钱
    public void deposit(double amount) {
        balance = balance + amount;
        if(top<size){
            transactions[top++] = new Transaction('D',amount,balance);
        }else{
            //如果超过了数组存储的最大容量,则删除第一个账单,后面的账单依次往前移,新帐单存在最后一个位置
            for(int i = 0;i<size-1;i++){
                transactions[i] = transactions[i+1];
                transactions[size-1] = new Transaction('D',amount,balance);
            }
        }
    }

    //输出用户信息及最近的交易记录
    public void output() {
        System.out.println("Name:" + name);
        System.out.println("Annual interest rate : " + getAnnuallnterestRate());
        System.out.println("Balance:" + balance);
        System.out.printf("%-40s%-15s%-15s%-15s\n", "Date", "Type", "Amount", "Balance");
        for(Transaction transaction:transactions){
            if(transaction!=null){
                transaction.print();
            }
        }
    }

}

Transaction类中:

public class Transaction {
    private Date tradTime;
    private char tradType;
    private double tradAmount;
    private double balance;

    Transaction(char tradType, double tradAmount, double balance) {
        tradTime = new Date();
        this.tradType = tradType;
        this.tradAmount = tradAmount;
        this.balance = balance;
    }

    public Date getTradTime() {
        return tradTime;
    }

    public char getTradType() {
        return tradType;
    }


    public double getTradAmount() {
        return tradAmount;
    }


    public double getBalance() {
        return balance;
    }

    public String toString() {
        return getTradTime() + "\t" + getTradType() + "\t\t" + getTradAmount() + "\t" + balance;
    }

    public void print(){
        System.out.printf("%-40s%-15s%-15s%-15s\n", getTradTime(),
                getTradType(), getTradAmount(), getBalance());
    }
}

Test(测试)类

public class Test {
    public static void main(String[] args) {
        Account a1 = new Account(1.5,1122,1000,"George");
        a1.setAnnuallnterestRate(5.5);
        a1.deposit(30);
        a1.deposit(40);
        a1.deposit(50);
        a1.withDraw(5);
        a1.withDraw(4);
        a1.withDraw(2);
        a1.output();
    }
}

运行截图如下:
使用Java编写银行账户类(面向对象思考实验)
(未对代码进行精简,部分位置会显得冗余了,请谅解)

本文地址:https://blog.csdn.net/qq_51868285/article/details/110212598

相关标签: java实验 java