实验题目 8:
将替换这样的数组代码:这些数组代码用于实现银行和客户间,以及客户与他们
的帐户间的关系的多样性。
实验目的:
使用集合
实验说明:
修改 Bank 类
修改 Bank 类,利用 ArrayList 实现多重的客户关系,不要忘记倒入必须的
java.util类
1. 将 Customer 属性的声明修改为List 类型,不再使用
numberOfCustomers 属性。
2. 修改 Bank 构造器,将 customers 属性的声明修改为List 类型,不再使用
numberOfcustomers 属性
3. 修改 addCustomer 方法,使用 add 方法
4. 修改 getCustomer 方法,使用 get 方法
5. 修改 getNumofCustomer 方法,使用 size 方法
修改 Customer 类 6. 修改 Customer 类,使用 ArrayList 实现多重的账户关系。修改方法同
上。
编译运行 TestBanking 程序
这里,不必修改 CustomerReport 代码,因为并没有改变 Bank 和
Customer 类的接口。编译运行TestBanking
应看到下列输出结果:
" CUSTOMERS REPORT "
“=================”
Customer:Simms,Jane
Savings Account:current balance is
尚硅谷 Java 基础实战—Bank 项目
$500.00 Checking Account:current
balance is $200.00
Customer:Bryant,Owen
Checking Accout:current balance is $200
Customer:Soley,Tim U7
Savings Account:current balance is $1,500.00
Checking Account:current balance is $200.00
Customer:Soley,Tim
Checking Account:current balance is $200.00
Savings Account :current balance is $150.00
Account.java
package banking;
public class Account {
protected double balance ;
public Account(){
}
public Account(double init_balance){
balance = init_balance ;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public boolean deposit(double amt){
if (amt > 0){
balance += amt ;
return true ;
}else {
System.out.println("请输入正确的存款数");
return false ;
}
}
public void withdraw(double amt) throws OverdraftException{
if (balance >= amt){
balance -= amt ;
} else {
throw new OverdraftException("资金不足", amt - balance) ;
}
}
}
Bank.java
package banking;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Bank {
private List<Customer> customers ;
//private int numberOfCustomer ;
private Bank(){
customers = new ArrayList<Customer>() ;
}
private static Bank bank = new Bank() ;
public static Bank getBank(){
return bank ;
}
public void addCustomer(String f, String l){
Customer cust= new Customer(f,l) ;
customers.add(cust) ;
}
public int getNumOfCustomers(){
return customers.size() ;
}
public Customer getCustomer(int index){
return customers.get(index) ;
}
public Iterator<Customer> getCustomers(){
return customers.iterator() ;
}
}
CheckingAccount.java
package banking;
public class CheckingAccount extends Account{
private Double overdraftProtection ;
public CheckingAccount(double balance){
super(balance);
}
public CheckingAccount(double balance, double protect){
super(balance);
overdraftProtection = protect ;
}
public double getOverdraftProtection() {
return overdraftProtection;
}
public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
}
public void withdraw(double amt) throws OverdraftException{
if (balance >= amt){
balance -= amt ;
} else{
if(overdraftProtection == null){
throw new OverdraftException("no overdraft exception", amt - balance) ;
}else if (overdraftProtection <= amt - balance){
throw new OverdraftException("Insuffient funds for overdraft exception", amt - balance - overdraftProtection) ;
}else {
overdraftProtection