银行(1)0925

2练习1:创建一个简单的银行程序包

 

练习目标-Java 语言中面向对象的封装性及构造器的使用。

任务

在这个练习里,创建一个简单版本的(账户类)Account类。将这个源文件放入banking程序包中。在创建单个帐户的默认程序包中,已编写了一个测试程序TestBanking。这个测试程序初始化帐户余额,并可执行几种简单的事物处理。最后,该测试程序显示该帐户的最终余额。

 

1.  创建banking 包

2.  在banking 包下创建Account类。该类必须实现上述UML框图中的模型。

  1. 声明一个私有对象属性:balance,这个属性保留了银行帐户的当前(或即时)余额。
  2. 声明一个带有一个参数(init_balance)的公有构造器,这个参数为balance属性赋值。
  3. 声明一个公有方法getBalance,该方法用于获取经常余额。
  4. 声明一个公有方法deposit,该方法向当前余额增加金额。
  5. 声明一个公有方法withdraw从当前余额中减去金额。

3.  编译TestBanking.java文件。

4.  运行TestBanking类。可以看到下列输出结果:

  Creating an account with a 500.00 balance

Withdraw 150.00

Deposit 22.50

Withdraw 47.62

The account has a balance of 324.88

//定义一个账户
public class Account {
    // 账户余额
    private double balance;

    public Account(double init_balance) {
        balance = init_balance;
    }

    // 获取账户余额
    public double getBlance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    // 存钱
    // amt存钱的额度
    public void deposit(double amt) {
        balance += amt;
    }

    // 取钱
    // amt取钱的额度
    public void withdraw(double amt) {
        if (balance >= amt) {
            balance -= amt;
        } else {
            System.out.println("余额不足!");
        }
    }
}

/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */
package com.troubleshooting.testbank1;

import com.troubleshooting.bank1.Account;

public class TestBanking1 {

    public static void main(String[] args) {
        Account account;

        // Create an account that can has a 500.00 balance.
        System.out.println("Creating an account with a 500.00 balance.");
        // code
        account = new Account(500);
        System.out.println("Withdraw 150.00");

        // code
        account.withdraw(150);
        System.out.println("Deposit 22.50");

        // code
        account.deposit(22.50);
        System.out.println("Withdraw 47.62");
        // code
        account.withdraw(47.62);
        // Print out the final account balance
        System.out.println("The account has a balance of " + account.getBlance());
    }
}

显示结果:
Creating an account with a 500.00 balance.
Withdraw 150.00
Deposit 22.50
Withdraw 47.62
The account has a balance of 324.88

练习2

 

练习目标-使用引用类型的成员变量:在本练习中,将扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。

任务

  1. 在banking包下的创建Customer类。该类必须实现上面的UML图表中的模型。

a. 声明三个私有对象属性:firstName、lastName和account。

b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)

c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。

d. 声明setAccount 方法来对account属性赋值。

e. 声明getAccount 方法以获取account属性。

  1. 在exercise2主目录里,编译运行这个TestBanking程序。应该看到如下输出结果:

Creating the customer Jane  Smith.

Creating her account  with  a  500.00  balance.

Withdraw 150.00

Deposit 22.50

Withdraw 47.62

Customer [Smith, Jane] has a balance of 324.88

//定义一个账户
public class Account {
    // 账户余额
    private double balance;

    public Account(double init_balance) {
        balance = init_balance;
    }

    // 获取账户余额
    public double getBlance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    // 存钱
    // amt存钱的额度
    public void deposit(double amt) {
        balance += amt;
    }

    // 取钱
    // amt取钱的额度
    public void withdraw(double amt) {
        if (balance >= amt) {
            balance -= amt;
        } else {
            System.out.println("余额不足!");
        }
    }
}

package com.troubleshooting.bank2;
import com.troubleshooting.bank1.Account;
public class Customer {
    
    private String firstName;
    private String lastName;
    private Account account;
    
    public Customer(String f,String l){
        firstName = f;
        lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

}

package com.troubleshooting.testbank1;
/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */

import com.troubleshooting.bank1.Account;
import com.troubleshooting.bank2.Customer;

public class TestBanking2 {

  public static void main(String[] args) {
    Customer customer;
    Account account;

    // Create an account that can has a 500.00 balance.
    account = new Account(500.00);
    System.out.println("Creating the customer Jane Smith."); 
    //code
    customer = new Customer("Jane","Smith");
    System.out.println("Creating her account with a 500.00 balance.");
    //code
    //account.setBalance(500);
    customer.setAccount(account);
    System.out.println("Withdraw 150.00");
   
    //code
    //account.withdraw(150);
    customer.getAccount().withdraw(150);
    System.out.println("Deposit 22.50");
      //code
    //account.deposit(22.50);
    customer.getAccount().deposit(22.50);
    System.out.println("Withdraw 47.62");
       //code
    //account.withdraw(47.62);
    customer.getAccount().withdraw(47.62);
    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
         + ", " + customer.getFirstName()
         + "] has a balance of " + customer.getAccount().getBlance());
  }

private static void customer(String string, String string2) {
    // TODO Auto-generated method stub
    
}
}

练习3:修改withdraw 方法

练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功。

 

任务

1.  修改Account类

  1. 修改deposit 方法返回true(意味所有存款是成功的)。
  2. 修改withdraw方法来检查提款数目是否大于余额。如果amt小于balance,则从余额中扣除提款数目并返回true,否则余额不变返回false。

2.  在exercise2主目录编译并运行TestBanking程序,将看到下列输出;

Creating the customer Jane Smith.

Creating her account with a 500.00 balance.

Withdraw 150.00: true

Deposit 22.50: true

Withdraw 47.62: true

Withdraw 400.00: false

Customer [Smith, Jane] has a balance of 324.88

//定义一个账户
public class Account {
    // 账户余额
    private double balance;

    public Account(double init_balance) {
        balance = init_balance;
    }

    // 获取账户余额
    public double getBlance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    // 存钱
    // amt存钱的额度
    public boolean deposit(double amt) {
        balance += amt;
        return true;
    }

    // 取钱
    // amt取钱的额度
    public boolean withdraw(double amt) {
        if (balance >= amt) {
            balance -= amt;
            return true;
        } else {
            System.out.println("余额不足!");
            return false;
        }
    }
}

package com.troubleshooting.bank3;

public class Customer {

    private String firstName;
    private String lastName;
    private Account account;

    public Customer(String f, String l) {
        firstName = f;
        lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

}

package com.troubleshooting.testbank1;
/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */

import com.troubleshooting.bank3.Customer;
import com.troubleshooting.bank3.Account;

public class TestBanking3 {

  public static void main(String[] args) {
    Customer customer;
    Account account;

    // Create an account that can has a 500.00 balance.
    account = new Account(500.00);
    System.out.println("Creating the customer Jane Smith.");
    //code 
    customer = new Customer("Jane", "Smith");
    System.out.println("Creating her account with a 500.00 balance.");
    
    //code
    customer.setAccount(account);
    account = customer.getAccount();
    // Perform some account transactions
    System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
    System.out.println("Deposit 22.50: " + account.deposit(22.50));
    System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
    System.out.println("Withdraw 400.00: " + account.withdraw(400.00));

    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
         + ", " + customer.getFirstName()
         + "] has a balance of " + account.getBlance());
  }
}

显示结果:

Creating the customer Jane Smith.

Creating her account with a 500.00 balance.

Withdraw 150.00: true

Deposit 22.50: true

Withdraw 47.62: true

余额不足!

Withdraw 400.00: false

Customer [Smith, Jane] has a balance of 324.88

练习4:用数组表示多重性

 

练习目标-在类中使用数组作为模拟集合操作: 在本练习中,将用数组实现银行与客户间的多重关系。

 

任务

对银行来说,可添加Bank类。 Bank 对象跟踪自身与其客户间的关系。用Customer对象的数组实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。

  1. 创建 Bank 类
  1. 为Bank类增加两个属性:customers(Customer对象的数组)和numberOfCustomers(整数,跟踪下一个customers数组索引)
  1. 添加公有构造器,以合适的最大尺寸(至少大于5)初始化customers数组。
  1. 添加addCustomer方法。该方法必须依照参数(姓,名)构造一个新的Customer对象然后把它放到customer数组中。还必须把numberofCustomers属性的值加1。
  1. 添加getNumOfCustomers 访问方法,它返回numberofCustomers属性值。
  1. 添加getCustomer方法。它返回与给出的index参数相关的客户。
  1. 编译并运行TestBanking程序。可以看到下列输出结果:

Customer [1] is Simms,Jane

Customer [2] is Bryant,Owen

   Customer [3] is Soley,Tim

   Customer [4] is Soley,Maria

//定义一个账户
public class Account {
    // 账户余额
    private double balance;

    public Account(double init_balance) {
        balance = init_balance;
    }

    // 获取账户余额
    public double getBlance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    // 存钱
    // amt存钱的额度
    public boolean deposit(double amt) {
        balance += amt;
        return true;
    }

    // 取钱
    // amt取钱的额度
    public boolean withdraw(double amt) {
        if (balance >= amt) {
            balance -= amt;
            return true;
        } else {
            System.out.println("余额不足!");
            return false;
        }
    }
}
package com.troubleshooting.bank4;

//创建 Bank 类
public class Bank {
    // 为 Bank 类 增 加 两 个 属 性 : customers(Customer对象的数组 ) 和
    // numberOfCustomers(整数,跟踪下一个 customers 数组索引)
    // 用于存放客户
    private Customer[] customers;
    // 记录存放客户的数目
    private int numberOfCustomers;

    // 添加公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
    public Bank() {
        customers = new Customer[10];
    }

    // 添加一个Customer到数组中
    // 添加 addCustomer 方法。该方法必须依照参数(姓,名)构造一个新的
    // Customer
    // 对象然后把它放到 customer 数组中。还必须把 numberofCustomers
    // 属性的值加 1。
    public void addCustomer(String f, String l) {
        Customer cust = new Customer(f, l);
        // customers[0]=cust;
        // customers[1]=cust;
        // customers[2]=cust;
        // customers[3]=cust;
        // customers[4]=cust;
        // ................
        customers[numberOfCustomers] = cust;
        numberOfCustomers++;
    }

    // 添加 getNumOfCustomers 访问方法,它返回 numberofCustomers 属性值。
    public int getNumOfCustomer() {
        return numberOfCustomers;
    }

    // 添加 getCustomer方法。它返回与给出的index参数相关的客户。
    public Customer getCustomer(int index) {
        return customers[index];
    }
}

package com.troubleshooting.bank4;

public class Customer {

    private String firstName;
    private String lastName;
    private Account account;

    public Customer(String f, String l) {
        firstName = f;
        lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

}

package com.troubleshooting.testbank1;
/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */

import com.troubleshooting.bank4.*;

public class TestBanking4 {

  public static void main(String[] args) {
    Bank bank = new Bank();

    // Add Customer Jane, Simms
    //code
    bank.addCustomer("Jane", "Simms");
    //Add Customer Owen, Bryant
    //code
    bank.addCustomer("JOwen", "Bryant");
    // Add Customer Tim, Soley
    //code
    bank.addCustomer("Tim", "Soley");
    // Add Customer Maria, Soley
    //code
    bank.addCustomer("Maria", "Soley");
    //遍历
    for ( int i = 0; i < bank.getNumOfCustomer(); i++ ) {
      Customer customer = bank.getCustomer(i);

      System.out.println("Customer [" + (i+1) + "] is "
             + customer.getLastName()
             + ", " + customer.getFirstName());
    }
  }
}
显示结果:
Customer [1] is Simms, Jane
Customer [2] is Bryant, JOwen
Customer [3] is Soley, Tim
Customer [4] is Soley, Maria

练习4:用List表示多重性

 

练习目标-在类中使用List作为模拟集合操作: 在本练习中,将用List实现银行与客户间的多重关系。

 

任务

对银行来说,可添加Bank类。 Bank 对象跟踪自身与其客户间的关系。用Customer对象的List实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。

  1. 创建 Bank 类
  1. 为Bank类增加两个属性:customers(Customer对象的List)和numberOfCustomers(整数, 当前Customer对象的数量)
  1. 添加公有构造器,初始化customersList
  1. 添加addCustomer方法。该方法必须依照参数(姓,名)构造一个新的Customer对象然后把它放到customerList中。
  1. 添加getNumOfCustomers 访问方法,它返回numberofCustomers属性值。
  1. 添加getCustomer方法。它返回与给出的index参数相关的客户。
  1. 编译并运行TestBanking程序。可以看到下列输出结果:

Customer 1 is Simms,Jane

Customer 2 is Bryant,Owen

   Customer 3 is Soley,Tim

   Customer 4 is Soley,Maria

当前客户数量 = 4

import java.util.ArrayList;
import java.util.List;

public class Bank{
    private List<Customer> customers;             //customers集合
    private int numberOfCustomers;                //记录客户数量
    public Bank(){                                //构造方法,初始化customers
        customers=new ArrayList<Customer>();
    }
    public void addCustomer(String f,String l){        //方法:往customers里面添加Customer对象
        Customer p=new Customer(f,l);
        customers.add(p);
    }
    public int getNumOfCustomers(){                   //获取客户数量,即获取customers的长度
        numberOfCustomers=customers.size();
        return numberOfCustomers;
    }
    public Customer getCustomer(int index){            //根据索引取值return customers.get(index); 
  }
 }
public class TestBanking {
    public static void main(String[] args) {
        Bank cus=new Bank(){};
        cus.addCustomer("Simms", "Jane");
        cus.addCustomer("Bryant", "Owen");
        cus.addCustomer("Soley","Tim");
        cus.addCustomer("Soley","Maria");
        for(int i=0;i<cus.getNumOfCustomers();i++){
            System.out.println("Customer "+(i+1)+" is"+" "+cus.getCustomer(i));
        }    
        System.out.println("当前客户数量="+cus.getNumOfCustomers());
        System.out.println("第二个客户是:"+cus.getCustomer(2));
    }
}

Customer类:

public class Customer {
    //声明私有属性
    private String firstName;
    private String lastName;
    private Account account;
    //声明构造器
    Customer(){};
    Customer(String f,String l){
        this.firstName=f;
        this.lastName=l;
    }
    //声明存取器getter/setter方法
    public String getFirstName() {                    //firstName
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {                    //lastName
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    //存取器getter/setter方法——account
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account acct) {
        this.account=acct;
    }
    @Override
    public String toString() {
        return firstName + "," + lastName;
    }
}

 

练习5

 

练习目标:继承、多态、方法的重写。 在本练习中,将在银行项目中创建Account的两个子类:SavingAccount 和 CheckingAccount

 

 

创建 Account类的两个子类:SavingAccount(存款账户) 和 CheckingAccount(透支账户)子类

  1. 修改Account类;将balance属性的访问方式改为protected
  2. 创建 SavingAccount 类,该类继承Account类
  3. 该类必须包含一个类型为double的interestRate(利率)属性
  4. 该类必须包括带有两个参数(balance和interest_rate)的共有构造器。该构造器必须通过调用super(balance)将balance参数传递给父类构造器。

实现CheckingAccount类

1.  CheckingAccount类必须扩展Account类

2.  该类必须包含一个类型为double的overdraftProtection(透支额度)属性。

3.  该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调用super(balance)将balance参数传递给父类构造器。

4.  给类必须包括另一个带有两个参数(balance 和 protect)的共有构造器。该构造器必须通过调用super(balance)并设置overdragtProtection属性,将balance参数传递给父类构造器。

5.  CheckingAccount类必须覆盖withdraw方法。此方法必须执行下列检查。如果当前余额足够弥补取款amount,则正常进行。如果不够弥补但是存在透支保护,则尝试用overdraftProtection得值来弥补该差值(balance-amount).如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。

6.  在主exercise1目录中,编译并执行TestBanking程序。输出应为:

Creating the customer Jane Smith.

Creating her Savings Account with a 500.00 balance and 3% interest.

Creating the customer Owen Bryant.

Creating his Checking Account with a 500.00 balance and no overdraft protection.

Creating the customer Tim Soley.

Creating his Checking Account with a 500.00 balance and 500.00 in overdraft prot

ection.

Creating the customer Maria Soley.

Maria shares her Checking Account with her husband Tim.

Retrieving the customer Jane Smith with her savings account.

Withdraw 150.00: true

Deposit 22.50: true

Withdraw 47.62: true

Withdraw 400.00: false

Customer [Simms, Jane] has a balance of 324.88

Retrieving the customer Owen Bryant with his checking account with no overdraft

protection.

Withdraw 150.00: true

Deposit 22.50: true

Withdraw 47.62: true

Withdraw 400.00: false

Customer [Bryant, Owen] has a balance of 324.88

Retrieving the customer Tim Soley with his checking account that has overdraft p

rotection.

Withdraw 150.00: true

Deposit 22.50: true

Withdraw 47.62: true

Withdraw 400.00: true

Customer [Soley, Tim] has a balance of 0.0

Retrieving the customer Maria Soley with her joint checking account with husband

 Tim.

Deposit 150.00: true

Withdraw 750.00: false

Customer [Soley, Maria] has a balance of 150.0

练习 6.

 

练习目标-单例模式

 

修改Bank类来实现单例设计模式

1.  修改Bank类,创建名为getBanking的公有静态方法,它返回一个Bank类的实例。

2.  单个的实例应是静态属性,且为私有。同样,Bank构造器也应该是私有的

创建CustomerReport类

  1. 在前面的银行项目练习中,“客户报告”嵌入在TestBanking应用程序的main方法中。在这个练习中,改代码被放在,banking.reports包的CustomerReport类中。您的任务是修改这个类,使其使用单一银行对象。

2. 查找标注为注释块/***   ***/的代码行.修改该行以检索单例银行对象。

编译并运行TestBanking应用程序

看到下列输入结果:

                          CUSTOMER REPORT

                       =======================

Customer:simms,jane

      Savings Account:current balance is $500.00

      Checking Account:current balance is $200.00

Customer:Bryant,owen

      Checking Account:current balance is $200.00

Customer: Soley,Tim

      Savings Account:current balance is $1,500.00

      Checking Account:current balance is $200.00

Customer:Soley ,Maria

      Checking Account:current balance is $200.00

      Savings Account:current balance is $150.00

练习7

练习目的—自定义异常:在本练习中,将建立一个OverdraftException异常,它由Account类的withdraw方法抛出。

 

创建OverdraftException类

1.  在banking.domain包中建立一个共有类OverdraftException. 这个类扩展Exception类。

2.  添加一个double类型的私有属性deficit.(亏损)增加一个共有访问方法getDeficit

3.  添加一个有两个参数的共有构造器。deficit参数初始化deficit属性

修改Account类

4.  重写withdraw方法使它不返回值(即void).声明方法抛出overdraftException异常

5.  修改代码抛出新异常,指明“资金不足”以及不足数额(当前余额扣除请求的数额)

修改CheckingAccount类

6.  重写withdraw方法使它不返回值(即void).声明方法抛出overdraftException异常

7.  修改代码使其在需要时抛出异常。两种情况要处理:第一是存在没有透支保护的赤字,对这个异常使用“no overdraft protection”信息。第二是overdraftProtection数额足以弥补赤字:对这个异常可使用”Insufficient”fundsfor overdragt protection”信息

编译并运行TestBanking程序

Customer [simms,Jane]has a checking balance of 200.0 with a 500.0 overdragt protection

Checking Acct[Jane Simms]: withdraw 150.00

Checking Acct[Jane Simms]: deposit 22.50

Checking Acct[Jane Simms]: withdraw 147.62

Checking Acct[Jane Simms]: withdraw 470.00

Exception: Insufficient”fundsfor overdragt protection

Deifcit:470.0

Customer [Simms,Jane]has a checking balance of 0.0

Customer [Bryant,Owen]has a checking balance of 200.0

Checking Acct[Bryant,Owen]: withdraw 100.00

Checking Acct[Bryant,Owen]: deposit25.00

Checking Acct[Bryant,Owen]: withdraw 175.00

Exception: no overdraft protection  Deficit:50.0

Customer [Bryant,Owen]has a checking balance of 125.0

第9

练习目的:在本练习中,将替换这样的数组代码:这些数组代码用于实现银行和客户间,以及客户与他们的帐户间的关系的多样性。

修改Bank类

修改Bank类,利用ArrayList实现多重的客户关系,不要忘记导入必须的java.util类

1.  将Customer属性的声明修改为list类型,不再使用numberOfCustoerms属性。

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 $500.00

Checking Account:current balance is $200.00

Customer:Bryant,Owen

Checking Accout:current balance is $200.00

Customer:Soley,Tim

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

可选:修改CustomerReport类

修改CustomerReport类,使用Iterator实现对客户的迭代

1.  在Bank类中,添加一个名为getCustomers的方法,该方法返回一个客户列表上的iterator

2.  在Customer类中,添加一个名为个体Accounts的方法,该方法返回一个帐户列表上的iterator

3.  修改CustomerReport 类,使用一对嵌套的while循环(而不是使用嵌套的for循环),在客户的iterator与帐户的iterator上进行迭代

4.  重新编译运行TestBanking程序,应看到与上面一样的输出结果

  1. //定义一个账户
  2. public class Account {
  3.     // 账户余额
  4.     private double balance;
  5.     public Account(double init_balance) {
  6.         balance = init_balance;
  7.     }
  8.     // 获取账户余额
  9.     public double getBlance() {
  10.         return balance;
  11.     }
  12.     public void setBalance(double balance) {
  13.         this.balance = balance;
  14.     }
  15.     // 存钱
  16.     // amt存钱的额度
  17.     public boolean deposit(double amt) {
  18.         balance += amt;
  19.         return true;
  20.     }
  21.     // 取钱
  22.     // amt取钱的额度
  23.     public boolean withdraw(double amt) {
  24.         if (balance >= amt) {
  25.             balance -= amt;
  26.             return true;
  27.         } else {
  28.             System.out.println("余额不足!");
  29.             return false;
  30.         }
  31.     }
  32. }
  33. package com.troubleshooting.bank3;
  34. public class Customer {
  35.     private String firstName;
  36.     private String lastName;
  37.     private Account account;
  38.     public Customer(String f, String l) {
  39.         firstName = f;
  40.         lastName = l;
  41.     }
  42.     public String getFirstName() {
  43.         return firstName;
  44.     }
  45.     public void setFirstName(String firstName) {
  46.         this.firstName = firstName;
  47.     }
  48.     public String getLastName() {
  49.         return lastName;
  50.     }
  51.     public void setLastName(String lastName) {
  52.         this.lastName = lastName;
  53.     }
  54.     public Account getAccount() {
  55.         return account;
  56.     }
  57.     public void setAccount(Account account) {
  58.         this.account = account;
  59.     }
  60. }
  61. package com.troubleshooting.testbank1;
  62. /*
  63.  * This class creates the program to test the banking classes.
  64.  * It creates a new Bank, sets the Customer (with an initial balance),
  65.  * and performs a series of transactions with the Account object.
  66.  */
  67. import com.troubleshooting.bank3.Customer;
  68. import com.troubleshooting.bank3.Account;
  69. public class TestBanking3 {
  70.   public static void main(String[] args) {
  71.     Customer customer;
  72.     Account account;
  73.     // Create an account that can has a 500.00 balance.
  74.     account = new Account(500.00);
  75.     System.out.println("Creating the customer Jane Smith.");
  76.     //code 
  77.     customer = new Customer("Jane", "Smith");
  78.     System.out.println("Creating her account with a 500.00 balance.");
  79.     
  80.     //code
  81.     customer.setAccount(account);
  82.     account = customer.getAccount();
  83.     // Perform some account transactions
  84.     System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
  85.     System.out.println("Deposit 22.50: " + account.deposit(22.50));
  86.     System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
  87.     System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
  88.     // Print out the final account balance
  89.     System.out.println("Customer [" + customer.getLastName()
  90.          + ", " + customer.getFirstName()
  91.          + "] has a balance of " + account.getBlance());
  92.   }
  93. }
原文地址:https://www.cnblogs.com/hanruyue/p/5907098.html