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;
}
}
结果如下: