题目
(The Account class) Design a class named Account that contains:
A private int data field named id for the account (default 0).
A private doubl e data field named balance for the account (default 0).
A private double data field named annualInterestRate that stores the cur-rent interest rate (default 0). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when theaccount 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 getMonthyInterestRate()that returns the monthlyinterest 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.
(账户类Account)设计一个名为Account的类,它包括:
一个名为id的int类型私有数据域(默认值为0)。
一个名为 balance 的 double类型私有数据域(默认值为0)。
一个名为 annualInterestRate的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。
一个名为 dateCreated的 Date类型的私有数据域,存储账户的开户日期。
一个用于创建默认账户的无参构造方法。
一个用于创建带特定id 和初始余额的账户的构造方法。id、balance和annualInterstRate的访问器和修改器:dateCreated 的访问器
一个名为 getMonthlyInterestRate()的方法,返回月利率。
一个名为 withDraw 的方法,从账户提取特定数额。
一个名为 deposit的方法向账户存储特定数额。
画出该类的 UML图并实现这个类。
编写一个测试程序,创建一个账户ID为1122、余额为20000美元、年利率为4.5%的Account 对象。使用withdraw方法取款 2500美元,使用deposit方法存款 3000美元,然后打印余额、月利息以及这个账户的开户日期。
代码
Account.java
import java.util.Date;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
Account(){
id=0;
balance=0;
annualInterestRate=0;
dateCreated=new Date();
}
Account(int id,int balance){
this.id=id;
this.balance=balance;
annualInterestRate=0;
dateCreated=new Date();
}
int getId(){
return id;
}
void setId(int id){
this.id=id;
}
double getBalance(){
return balance;
}
void setBalance(double balance){
this.balance=balance;
}
double getAnnualInterestRate(){
return annualInterestRate;
}
void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate=annualInterestRate;
}
Date getDateCreated(){
return dateCreated;
}
double getMonthlyInterestRate(){
return annualInterestRate/(12*100);
}
double getMonthlyInterest(){
return balance*getMonthlyInterestRate();
}
void withDraw(double wd){
balance=balance-wd;
}
void deposit(double ds){
balance=balance+ds;
}
@Override
public String toString() {
return "Account ID: " + getId() + "\nBalance: " + getBalance() +
"\nDate created " + getDateCreated() + "\nCurrent Annual Rate: " + getAnnualInterestRate();
}
}
Test.java
public class Test {
public static void main(String[] args) {
Account list1=new Account(1122,20000);
list1.setAnnualInterestRate(4.5);
list1.withDraw(2500);
list1.deposit(3000);
System.out.println(list1.toString() + "\nMonthly Interest: " + list1.getMonthlyInterest());
}
}
UML图
结果