Java语言程序设计 例题9.7(Account类)

9.7 (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, and an annual interest rate of 4.5%. Use the withdraw
method to withdraw $2,500, use the deposit method to deposit $3,000, and print 
the balance, the monthly interest, and the date when this account was created.

9.7(Account类)设计一个名为Account的类,他包括:

为账号定义一个名为id的int类型私有数据域(默认值为0)标识账号。
为账号定义一个名为balance的double类型私有数据域(默认值为0)表示余额。
一个名为annualInterestRate的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。
一个名为dateCreated的Date类型的私有数据域,存储账户的开户日期。
一个用于创建默认账户的无参构造方法。
一个用于创建具有指定id和初始余额的账户的构造方法。
id、balance和annualInterestRate的访问器方法和修改器方法。
dateCreated的访问器方法。
一个名为getMonthlyInterstRate()的方法,返回月利率。
一个名为getMonthlyInterst()的方法,返回月利息。
一个名为writeDraw的方法,从账户提取指定余额。
一个名为deposit的方法向指定账户存储指定额度。
画出该类的UML图并实现这个类。
提示:方法getMonthlyInterst()用于返回月利息,而不是利率。月利息是balance*monthly-InteresRate。monthlyInterestRate是annualInterestRate/12、注意,annualInterestRate是一个百分数,比如4.5%。你需要将其除以100.
编写一个测试程序,创建一个账户ID为1122、余额为20000美元、年利率为4.5%的Account对象。使用withDraw方法取款2500美元,使用deposit方法存款3000美元,然后打印余额、月利息以及这个账户的开户日期。

代码如下:

import java.util.*;
public class Unite9Test7 
{
	public static void main(String[] args) 
	{
		
		Scanner input=new Scanner(System.in);
		System.out.println("请输入账户ID和余额: ");
		int id=input.nextInt();
		double balance = input.nextDouble();
		Account acc=new Account(id,balance); 
		acc.setAnnualInterestRate(4.5);
		System.out.println("请输入取款金额和存款金额: ");
		double deposit=input.nextDouble();
		acc.deposit(deposit);
		double withdraw=input.nextDouble();
		acc.withdraw(withdraw);
		System.out.println("余额\t\t月利息\t\t创建账户的日期");
		System.out.println(acc.getBalance()+"\t\t"+acc.getMonthlyInterest()+"\t\t"+acc.getDateCreate().toString());
		
	}

}
class Account
{
	private int id=0;
	private double balance=0;
	private static double annualInterestRate=0;
	private Date dateCreate=new Date();
	public Account() 
	{
		
	}
	public Account(int id,double balance) 
	{
		this.id = id;
		this.balance=balance;
	}
	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 getAnnualInterestRate()
	{
		return annualInterestRate;
	}
	public static void setAnnualInterestRate(double annualInterestRate)
	{
		Account.annualInterestRate = annualInterestRate;
	}
	public Date getDateCreate() 
	{
		return dateCreate;
	}
	public double getMonthlyInterestRate() 
	{
		return (this.annualInterestRate/12)/100;
	}
	public double getMonthlyInterest() 
	{
		double MonthlyInterest;
		MonthlyInterest=this.balance*this.getMonthlyInterestRate();
		return MonthlyInterest;
	}
	public void withdraw(double withdraw) 
	{
		this.balance+=withdraw;
	}
	public void deposit(double deposit) 
	{
		this.balance-=deposit;
	}
}

结果如下:


 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

差劲的厉害了

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

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

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

打赏作者

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

抵扣说明:

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

余额充值