- 题目(续)
- 题目中提到的编程练习题9.7:以下代码直接利用即可
- Test03_checking_account:支票账户
- Test03_saving_account
- Test03:测试程序(创建三个对象并调用toString方法)
- 总UML图
画出这些类的UML图并实现这些类。编写一个测试程序,创建Account、SavingsAccount和CheckingAccount的对象,然后调用它们的toString()方法
题目中提到的编程练习题9.7:以下代码直接利用即可省流助手:四个私有数据域 + 无参有参构造方法 + id balance annualInterestRate三个数据域的setter和getter方法 + dateCreated的访问器方法 + getMonthlyInterestRate方法 + getMonthlyInterest方法 + withDraw方法 + deposit方法
import java.util.Date;
public class Test2_Account {
// 四个私有数据域
private int id = 0;
private double balance = 0.0;
private double annualInterestRate = 0.0;
private Date dateCreated;
// 无参构造方法
public Test2_Account(){
}
// 有参构造方法
public Test2_Account(int id, double balance){
this.id = id;
this.balance = balance;
}
// id balance annualInterestRate的setter和getter
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;
}
// dateCreated的访问器方法
public Date getDateCreated(){
return dateCreated;
}
// getMonthlyInterestRate方法
public double getMonthlyInterestRate(){
return annualInterestRate / 1200;
}
// getMonthlyInterest方法
public double getMonthlyInterest(){
return annualInterestRate * balance / 1200;
}
// withDraw方法
public void withDraw(double num){
if (num <= balance) balance -= num;
}
// deposit方法
public void deposit(double num){
balance += num;
}
@Override
public String toString() {
return "Test03_Account{" +
"id=" + id +
", balance=" + balance +
", annualInterestRate=" + annualInterestRate +
", dateCreated=" + dateCreated +
'}';
}
}
本类UML图:
Test03_checking_account:支票账户public class Test03_checking_account extends Test03_Account{
public double overDraftLimit = 0;
public Test03_checking_account(){
}
public Test03_checking_account(double overDraftLimit){
this.overDraftLimit = overDraftLimit;
}
@Override
public String toString() {
return "Test03_checking_account{" +
"overDraftLimit=" + overDraftLimit +
"} " + super.toString();
}
}
Test03_saving_account
public class Test03_saving_account extends Test03_Account{
private double minBalance = 0.0;
}
Test03:测试程序(创建三个对象并调用toString方法)
public class Test03 {
public static void main(String[] args) {
// 创建Account
Test03_Account ta = new Test03_Account();
ta.toString();
// 创建Savings-Account
Test03_saving_account sa = new Test03_saving_account();
sa.toString();
// 创建CheckingAccount
Test03_checking_account ca = new Test03_checking_account();
ca.toString();
}
}
总UML图



