设计一个名为Account的类(Java上机作业)

Java上机作业

在这里插入图片描述

翻译:

(Account类)设计一个名为Account的类,其中包含:

一个名为私有int数据字段id账户(默认值0)。

一个名为balancefort的私有双数据字段。

一个名为annualInteresrent interest rate的双数据字段。(默认值为o)

一个名为dateCreated的Date数据字段,存储创建帐户时的日期。
假设所有帐户的利率相同。

创建默认帐户的无参数构造函数。

一个构造函数,它使用指定的id和初始余额创建一个帐户,用于id、balance和annualInterestRate的setter访问器方法。

一个名为getMonthlyInterestRate()的方法,返回月利息。
月利率=年利率÷12,日利率═年利率÷360=月利率÷30,比方说年利率是7.05%,换算成月利率就是7.05%/12=5.875%。

从帐户中提取指定金额的名为withdraw()的方法。

从帐户中存取指定金额的名为deposit()的方法

一个存款的方法,它将一定数额的存款存入帐户。

为类绘制UML图,实现类,编写一个测试程序,创建一个帐号对象,帐号ID为1122, 初始余额为 20,000$,年利率为4.5%。使用 withdraw()提取$2,500,使用 deposit()存储$3,000,打印余额,每月利息,以及该帐户创建的日期。

代码段


import java.util.Date;

public class Account { //Account类

	public static void main(String[] args) {
		
		Account account = new Account(1122, 20000);//初始化id和初始存款
		account.setAnnualInterestRate(4.5);//输入年利率
		account.withDraw(2500);//提取
		account.deposit(3000);//存储
		//打印余额,每月利息,以及该帐户创建的日期。
		System.out.println("Balance: "+account.getBalance()+"\n"
				+"Monthly Interest Rate: "+account.getMonthlyInterestRate()+"\n"
				+"Date Created: "+account.getDateCreated());
	}


	
	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;
	}
	
}


运行结果(编译器Eclipse):

在这里插入图片描述

  • 22
    点赞
  • 135
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值