Java黑皮书课后题第11章:11.3(Account类的子类)在编程练习题9.7中定义了一个Account类来对一个银行账户建模。一个账户有账号、余额、年利率、开户日期等属性,以及存款和取款等方法
续标题:创建支票账户checking account和储蓄账户saving account两个子类。支票账户有一个透支限定额,但储蓄账户不能透支
- 题目(续)
- 题目中提到的编程练习题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和getterpublic 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;}@Overridepublic 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;}@Overridepublic 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) {// 创建AccountTest03_Account ta = new Test03_Account();ta.toString();// 创建Savings-AccountTest03_saving_account sa = new Test03_saving_account();sa.toString();// 创建CheckingAccountTest03_checking_account ca = new Test03_checking_account();ca.toString();}
}

总UML图

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
