(The Account class) Design a class named Account that contains:(java面向对象思想的基础运用)

本文介绍了如何设计一个名为`Account`的类,包含账户ID、余额、年利率、创建日期等属性及相应的方法。类的实现包括无参构造器、带参构造器、访问器和修改器,以及计算月利率和月利息的方法。此外,还提供了一个测试程序,创建了一个账户对象并进行了存款、取款操作,最后输出了账户余额、月利息和创建日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 (The Account class) Design a class named Account that contains:

■ A private int data field named id for the account (default 0).

■ A private double data field named balance for the account (default 0).

■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.

■ A private Date data field named dateCreated that stores the date when the account was created.

■ A no-arg constructor that creates a default account.

■ A constructor that creates an account with the specified id and initial balance.

■ The accessor and mutator methods for id, balance, and annualInterestRate.

■ The accessor method for dateCreated.

■ A method named getMonthlyInterestRate() that returns the monthly interest rate.

■ A method named getMonthlyInterest() that returns the monthly interest.

■ A method named withdraw that withdraws a specified amount from the account.

■ A method named deposit that deposits a specified amount to the account.

Draw the UML diagram for the class and then implement the class.

(Hint: The method getMonthlyInterest() is to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12. Note that annualInterestRate is a percentage, e.g., like 4.5%. You need to divide it by 100.)

Write a test program that creates an Account object with an account ID of 1122, a balance of 20,000,andanannualinterestrateof4.52,500, use the deposit method to deposit $3,000, and print the balance, the monthly interest, and the date when this account was created.

直接上UML图和代码

 代码

先是账户实体类

package homework03;

import java.util.Date;

public class Account {
	private int id=0;  //账户id
	private double balance=0;  //账户余额
	private double annualInterestRate=0; //利率
	private Date dateCreated;     //账户创建日期
	public Account() {
		super();
	}
	public Account(int id, double balance) {
		super();
		this.id = id;
		this.balance = balance;
		this.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;
	}
	
	//返回月利率  月利率=年利率/12
	public  double getMonthlyInterestRate(){
		 
		
		 return  this.annualInterestRate/12;
	 }
	
	//返回月利息   月利息=存款*月利率
	public  double getMonthlyInterest(){
		
		return  this.balance*getMonthlyInterestRate();
	}
	
	//取款  返回余额
	public  double withdraw (double money){
		this.balance-=money;
		return  this.balance;
	}
	//存款  返回余额
	public  double deposit  (double money){
			this.balance+=money;
		return  this.balance;
	}
	
}

再是测试类

package homework03;

import java.text.DecimalFormat;

public class TestAccount {

	public static void main(String[] args) {
		//初始化一个账户对象 id是1122   余额为$20,000
		Account account = new Account(1122,20000);
		//设置账户年利率
		account.setAnnualInterestRate(4.52500/100);
        //取款2500
        account.withdraw(2500);
		//存款$3000
		account.deposit(3000);
		//格式化输出 题目中小数为5位
		DecimalFormat df = new DecimalFormat("0.00000");
		//打印 余额 月利息和创建账户日期
		System.out.println("账户余额: $"+df.format(account.getBalance())
		+"\n月利息: $"+df.format(account.getMonthlyInterest())
		+"\n账户创建日期: "+account.getDateCreated().toLocaleString());
	}

}

面向对象最最最基础的应用,一定要理解

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WaitIKnowYou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值