设计一个名为Account的类

它包括:

  • 为账号定义一个名为id的int类型私有数据域(默认值为0)标识账号
  • 为账号定义一个名为balance的double类型私有数据域(默认值为0)表示余额
  • 一个名为annualInterestRate的double类型私有数据域储存当前利润(默认值为0)。假设所有的账户都有相同的利率
  • 一个名为dateCreated的Date类型的私有数据域,储存账户的开户日期
  • 一个用于创建默认账户的无参构造方法
  • 一个用于创建具有指定id和初始余额的账户的构造方法
  • id、balance和annualInterstRate的访问器方法和修改器方法
  • dateCreated的访问器方法
  • 一个名为getMonthlyInterestRate()的方法,返回月利率
  • 一个名为getMonthlyInterest()的方法,返回月利息
  • 一个名为withDraw的方法,从账户提取指定额度
  • 一个名为deposit的方法向账户储存指定额度

画出该类的UML图并实现这个类

Account
-id:int=0
-balance:double=0
-annualInterestRate:double=0
-dateCreated:Date
+Account()
+Account(id:int,balance:double)
+getId():int
+setId(id:int):void
+getBalance():double
+setBalance(balance:double):void
+getannualInterstRate():double
+getdateCreated():date
+getMonthlyInterestRate():double
+getMonthlyInterest():double
+withDraw():void
+deposit():void
import java.util.Date;

public class Account {
	private int id = 0;
	private double balance = 0;
	private double annualInterestRate = 0;
	private Date dateCreated;
	public Account() {
		dateCreated = new Date();
	}
	public Account(int id, double balance) {
		this.id = id;
		this.balance = balance;
		dateCreated = new Date();
	}
	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 double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}
	public Date getDateCreated() {
		return dateCreated;
	}
	public double getMonthlyInterestRate() {
		double monthlyInterestRate = annualInterestRate / 12;
		return balance * monthlyInterestRate / 100;
	}
	public void withDraw(double money) {
		balance -= money;
	}
	public void deposit(double money) {
		balance += money;
	}//下面为测试程序
	public static void main(String[] args) {
		Account account = new Account(1122, 20000);
		account.setAnnualInterestRate(4.5);
		account.withDraw(2500);
		account.deposit(3000);
		System.out.println("余额: "+account.getBalance()+"\n"+"月利率: "+account.getMonthlyInterestRate()+"\n"+"创建日期: "+account.getDateCreated());
	}

}

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值