JAVA实验——简单的Account类及其调用测试

问题:编写一个测试程序,创建一个账户id为1122、余额为20000元、年利率为4.5%的Account对象,使用withdraw方法取款2500元,使用deposit方法存款3000元,然后显示余额、月利息以及这个账户的开户日期。

Account类

package Experience_1;

import java.util.Date;

public class Account {
    private int id;
    private double balance;
    private double annuallnterestRate;
    private Date dateCreated;

    //无参构造
    public Account() {
    }

    //有参构造
    public Account(int id, double balance) {
        this.id = id;
        this.balance = balance;
    }

    //id访问器及修改器
    public int getId() {
        return id;
    }

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

    //balance访问器及修改器
    public double getBalance() {
        return balance;
    }

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

    //annuallnterstRate访问器及修改器
    public double getAnnuallnterestRate() {
        return annuallnterestRate;
    }

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

    //dateCreated的访问器及修改器
    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(){
        this.dateCreated = new Date();
    }

    //返回月利息
    public double getMonthlyInterest() {
        return balance * annuallnterestRate / 100 / 12;
    }

    //取款
    public void withDraw(double money) {
        balance = balance - money;
    }

    //存款
    public void deposit(double money){
        balance = balance + money;
    }

}

AccountDemo测试类

package Experience_1;

public class AccountDemo {
    public static void main(String[] args) {
        Account xxl = new Account();
        xxl.setId(1122);
        xxl.setDateCreated();
        xxl.setBalance(20000);
        xxl.setAnnuallnterestRate(4.5);
        xxl.withDraw(2500);
        xxl.deposit(3000);
        System.out.println("您的余额为:" + xxl.getBalance() + "元");
        System.out.println("您的月利息为:" + xxl.getMonthlyInterest() +"元");
        System.out.println("开户日期为:" + xxl.getDateCreated());
    }
}

执行结果为:

您的余额为:20500.0元
您的月利息为:76.875元
开户日期为:Sun May 23 15:53:40 CST 2021
 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值