(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());
}
}
面向对象最最最基础的应用,一定要理解