源码
//9.7的账户实体类
package experiment04;
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;
}
}
11.8的新账户实体类
package experiment04;
import java.util.ArrayList;
public class NewAccount extends Account {
private String name;
private ArrayList<Transaction> transactions = new ArrayList<>();//必须新建集合实例
public NewAccount(String name, int id, double balance) {
super(id, balance);
this.name = name;
}
@Override
public double withdraw(double money) {// 取款 返回余额
setBalance(getBalance() - money);
transactions.add(new Transaction('W', money, getBalance(), "取钱操作"));
return getBalance();
}
@Override
public double deposit(double money) {// 存款 返回余额
setBalance(getBalance() + money);
// 记录添加 存钱D 交易量 新余额 描述
transactions.add(new Transaction('D', money, getBalance(), "存钱操作"));
return getBalance();
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
@Override
public String toString() {
return "NewAccount [name=" + name + ", transactions=" + transactions + ", getId()=" + getId()
+ ", getBalance()=" + getBalance() + ", getDateCreated()=" + getDateCreated() + "]";
}
}
package experiment04;
import java.util.ArrayList;
public class NewAccount extends Account {
private String name;
private ArrayList<Transaction> transactions = new ArrayList<>();//必须新建集合实例
public NewAccount(String name, int id, double balance) {
super(id, balance);
this.name = name;
}
@Override
public double withdraw(double money) {// 取款 返回余额
setBalance(getBalance() - money);
transactions.add(new Transaction('W', money, getBalance(), "取钱操作"));
return getBalance();
}
@Override
public double deposit(double money) {// 存款 返回余额
setBalance(getBalance() + money);
// 记录添加 存钱D 交易量 新余额 描述
transactions.add(new Transaction('D', money, getBalance(), "存钱操作"));
return getBalance();
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
@Override
public String toString() {
return "NewAccount [name=" + name + ", transactions=" + transactions + ", getId()=" + getId()
+ ", getBalance()=" + getBalance() + ", getDateCreated()=" + getDateCreated() + "]";
}
}
//交易记录实体类
package experiment04;
import java.util.Date;
public class Transaction {
private Date date ;
private char type;//取钱W 存钱D
private double amount;//交易量
private double banlance;//余额
private String description;
public Transaction(char type, double amount, double banlance, String description) {
//super();
date=new Date();
this.type = type;
this.amount = amount;
this.banlance = banlance;
this.description = description;
}
@Override
public String toString() {
return "Transaction [date=" + date + ", type=" + type + ", amount=" + amount + ", banlance=" + banlance
+ ", description=" + description + "]\n";
}
}
//测试类
package experiment04;
public class TestNewAccount {
public static void main(String[] args) {
NewAccount newAccount = new NewAccount("George", 1122, 1000);
newAccount.setAnnualInterestRate(0.015);// 设置年利率为1.5%
newAccount.deposit(30);// 存$30
newAccount.deposit(40);// 存$40
newAccount.deposit(50);// 存$50
newAccount.withdraw(5);// 取$5
newAccount.withdraw(4);// 取$4
newAccount.withdraw(2);// 取$2
System.out.println(newAccount.getTransactions());
}
}
运行截图