实验一

题目

​ 创建一个简单的银行程序包。

目的

​ Java 语言中面向对象的封装性及构造器的创建和使用。

说明

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

提示

  1. 创建 banking 包

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

  • 声明一个私有对象属性:balance,这个属性保留了银行帐户的当前(或即时)余额。

  • 声明一个带有一个参数(init_balance)的公有构造器,这个参数为 balance 属性赋值。

  • 声明一个公有方法 getBalance,该方法用于获取账户余额。

  • 声明一个公有方法 deposit,该方法向当前余额增加金额。

  • 声明一个公有方法 withdraw,从当前余额中减去金额。

  1. 打开TestBanking.java文件,按提示完成编写,并编译 TestBanking.java 文件。
  2. 运行 TestBanking 类。可以看到下列输出结果:
1
2
3
4
5
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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package Banking;

public class Account {
//账户余额
private double balance;

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

public double getBalance() {
return balance;
}

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

//取钱
public void withdraw(double amt) {
if (balance >= amt){
balance -= amt;
}else {
System.out.println("余额不足!");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package TestBanking;

import Banking.Account;

public class TestBanking01 {

public static void main(String[] args) {

Account account;

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

// code
account.withdraw(150.00);
System.out.println("Withdraw 150.00");

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

// code
account.withdraw(47.62);
System.out.println("Withdraw 47.62");

// Print out the final account balance
System.out.println("The account has a balance of " + account.getBalance());
}
}

实验二

题目

​ 扩展银行项目,添加一个 Customer 类。Customer 类将包含一个 Account 对象。

目的

​ 使用引用类型的成员变量。

提示

  1. 在banking包下的创建Customer类。该类必须实现下面的UML图表中的模型。
  • 声明三个私有对象属性:firstName、lastName 和 account。
  • 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l)
  • 声明两个公有存取器来访问该对象属性,方法 getFirstName 和 getLastName返 回相应的属性。
  • 声明 setAccount 方法来对 account 属性赋值。
  • 声明 getAccount 方法以获取 account 属性。
  1. 在 exercise2 主目录里,编译运行这个 TestBanking 程序。应该看到如下输出结果:
1
2
3
4
5
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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package Banking;

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 String getLastName() {
return lastName;
}

public Account getAccount() {
return account;
}

public void setAccount(Account acct) {
account = acct;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package TestBanking;
/*
* 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 Banking.Account;
import Banking.Customer;

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

account = new Account(500.00);
System.out.println("Creating the customer Jane Smith.");
customer = new Customer("Jane","Smith");

System.out.println("Creating her account with a 500.00 balance.");
customer.setAccount(account);

System.out.println("Withdraw 150.00");
customer.getAccount().withdraw(150.00);

System.out.println("Deposit 22.50");
customer.getAccount().deposit(22.50);

System.out.println("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 " + account.getBalance());
}
}

实验三

题目

​ 修改 withdraw 方法以返回一个布尔值,指示交易是否成功。

目的

​ 使用有返回值的方法。

提示

  1. 修改 Account 类

    • 修改 deposit 方法返回 true(意味所有存款是成功的)。
    • 修改 withdraw 方法来检查提款数目是否大于余额。如果amt小于balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
  2. 在 exercise3 主目录编译并运行 TestBanking 程序,将看到下列输出:

    1
    2
    3
    4
    5
    6
    7
    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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Banking;

public class Account {
//账户余额
private double balance;

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

public double getBalance() {
return balance;
}

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

//取钱
public boolean withdraw(double amt) {
if (balance >= amt){
balance -= amt;
return true;
}else {
return false;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package TestBanking;
/*
* 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 Banking.Account;
import Banking.Customer;

public class TestBanking03 {

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.");
customer = new Customer("Jane","Smith");

System.out.println("Creating her account with a 500.00 balance.");
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.getBalance());
}
}

实验四

题目

​ 将用数组实现银行与客户间的多重关系。

目的

​ 在类中使用数组作为模拟集合操作。

提示

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

  • 创建 Bank 类
  • 为 Bank 类增加两个属性 : customers(Customer对象的数组)和 numberOfCustomers(整数,跟踪下一个 customers 数组索引)

  • 添加公有构造器,以合适的最大尺寸(至少大于5)初始化 customers 数组。

  • 添加 addCustomer 方法。该方法必须依照参数(姓,名)构造一个新的 Customer对象然后把它放到 customer 数组中。还必须把 numberofCustomers 属性的值加 1。

  • 添加 getNumOfCustomers 访问方法,它返回 numberofCustomers 属性值。

  • 添加 getCustomer 方法。它返回与给出的index参数相关的客户。

  • 编译并运行 TestBanking 程序。可以看到下列输出结果:

    1
    2
    3
    4
    Customer [1] is Simms,Jane
    Customer [2] is Bryant,Owen
    Customer [3] is Soley,Tim
    Customer [4] is Soley,Maria

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package Banking;

public class Bank {
//用于存放客户
private Customer[] customers;
//记录客户的个数
private int numberOfCustomers;

public Bank() {
customers = new Customer[5];
}
//添加一个客户到数组中
public void addCustomer(String f,String l){
Customer c = new Customer(f, l);
customers[numberOfCustomers] = c;
numberOfCustomers ++;
}
//获取客户的个数
public int getNumOfCustomers(){
return numberOfCustomers;
}
//获取指定索引的客户
public Customer getCustomer(int index){
return customers[index];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package TestBanking;
/*
* 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 Banking.*;

public class TestBanking04 {

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

//Add Customer Jane, Simms
bank.addCustomer("Jane","Simms");
//Add Customer Owen, Bryant
bank.addCustomer("Owen","Bryant");
//Add Customer Tim, Soley
bank.addCustomer("Tim","Soley");
//Add Customer Maria, Soley
bank.addCustomer("Maria","Soley");

for ( int i = 0; i < bank.getNumOfCustomers(); i++ ) {
Customer customer = bank.getCustomer(i);
System.out.println("Customer [" + (i+1) + "] is "
+ customer.getLastName()
+ "," + customer.getFirstName());
}
}
}

实验五

题目

​ 在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount。

目的

​ 继承、多态、方法的重写。

提示

  • 创建 Account 类的两个子类:SavingAccount 和 CheckingAccount 子类

    • 修改 Account 类:将 balance 属性的访问方式改为 protected
    • 创建 SavingAccount 类,该类继承 Account 类
    • 该类必须包含一个类型为 double 的 interestRate 属性
    • 该类必须包括带有两个参数(balance 和 interest_rate)的公有构造器。该构造器必须通过调用 super(balance)将 balance 参数传递给父类构造器
  • 实现 CheckingAccount 类

    • CheckingAccount 类必须扩展 Account 类
    • 该类必须包含一个类型为 double 的 overdraftProtection 属性
    • 该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调用 super(balance) 将 balance 参数传递给父类构造器
    • 给类必须包括另一个带有两个参数(balance 和 protect)的公有构造器。该构造器必须通过调用 super(balance) 并设置 overdragtProtection 属性, 将 balance 参数传递给父类构造器。
    • CheckingAccount 类必须覆盖 withdraw 方法。此方法必须执行下列检查。如果当前余额足够弥补取款 amount,则正常进行。如果不够弥补但是存在透支保护,则尝试用 overdraftProtection 得值来弥补该差值 (balance-amount)。如果弥补该透支所需要的金额大于当前的保护级别,则整个交易失败,但余额未受影响。
  • 编译并执行 TestBanking 程序。输出应为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 protection.
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 protection.
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

Process finished with exit code 0

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Banking;

//储蓄账户
public class SavingAccount extends Account{
private double interestRate;

public SavingAccount(double balance, double interest_rate) {
super(balance);
this.interestRate = interest_rate;
}

public double getInterestRate() {
return interestRate;
}

public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Banking;

//信用卡账户
public class CheckingAccount extends Account{
private double overdraftProtection;

public CheckingAccount(double balance){
super(balance);
}

public CheckingAccount(double balance, double protect){
super(balance);
this.overdraftProtection = protect;
}

public double getOverdraftProtection() {
return overdraftProtection;
}

public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
}

//取钱
public boolean withdraw(double amt) {
if (balance >= amt){
balance -= amt;
}else if (overdraftProtection >= (amt - balance)){
overdraftProtection -= (amt - balance);
balance = 0;
}else
return false;
return true;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package TestBanking;

import Banking.*;

/*
* 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.
*/
public class TestBanking05 {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account;

// Create bank customers and their accounts
System.out.println("Creating the customer Jane Smith.");
bank.addCustomer("Jane", "Simms");

System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
account = new SavingAccount(500.00, 0.03);
customer = bank.getCustomer(0);
customer.setAccount(account);

System.out.println("Creating the customer Owen Bryant.");
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
account = new CheckingAccount(500.00, 0);
customer.setAccount(account);

System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
account = new CheckingAccount(500.00, 500.00);
customer.setAccount(account);

System.out.println("Creating the customer Maria Soley.");
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
System.out.println("Maria shares her Checking Account with her husband Tim.");
customer.setAccount(bank.getCustomer(2).getAccount());

System.out.println();

// Demonstrate behavior of various account types

// Test a standard Savings Account
System.out.println("Retrieving the customer Jane Smith with her savings account.");
customer = bank.getCustomer(0);
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.getBalance());

System.out.println();

// Test a Checking Account w/o overdraft protection
System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
customer = bank.getCustomer(1);
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.getBalance());

System.out.println();

// Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
customer = bank.getCustomer(2);
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.getBalance());

System.out.println();

// Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
customer = bank.getCustomer(3);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Deposit 150.00: " + account.deposit(150.00));
System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());

}
}

实验5_1

题目

​ 创建客户账户。

目的

​ instanceof 运算符的应用。

提示

​ 修改 Customer 类

  • 修改 Customer 类来处理具有多种类型的联合账户。(例如,用数组表示多重性一节所作的,该类必须包括以下的公有方法: addAccount(Account)getAccount(int)getNumOfAccounts()。 每个 Customer 可以有多个 Account。(声明至少有 5 个)
  • 完成 TestBanking 程序,该程序创建一个客户和账户的集合,并生成这些客户及其账户余额的报告。在 TestBanking.Java 文件中,你会发现注释块以//来开头和结尾。这些注释只是必须提供的代码的位置。
  • 使用 instanceof 操作符测试拥有的账户类型,并且将 account_type 设置为适当的值,例如:“SavingsAccount”或“CheckingAccount”。
  • 编译并运行该程序,将看到下列结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
                                      CUSTOMERS 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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Customer {
private String firstName;
private String lastName;
private Account[] accounts;
private int numOfAccounts; // 记录Account的个数

public Customer(String f, String l){
firstName = f;
lastName = l;
accounts = new Account[5];
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

// 添加一个账户到Account[]里
public void addAccount(Account acct){
accounts[numOfAccounts] = acct;
numOfAccounts ++;
}

// 返回账户的个数
public int getNumOfAccounts(){
return numOfAccounts;
}

// 返回指定索引处的账户
public Account getAccount(int index){
return accounts[index];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Banking5_1.*;
import java.text.NumberFormat;

public class TestBanking05_1 {
public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer;

// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingAccount(150.00, 0.05));

// Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) {
customer = bank.getCustomer(cust_idx);

System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());

for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++ ) {
Account account = customer.getAccount(acct_idx);
String account_type = "";

// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
if (account instanceof SavingAccount){
account_type = "SavingAccount";
}
if (account instanceof CheckingAccount){
account_type = "CheckingAccount";
}

// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
System.out.println(account_type + ":current balance is " + currency_format.format(account.getBalance()));
}
}
}
}

实验5_2

题目

​ 实现更为复杂的透支保护机制。

目的

​ 继承、多态、方法的重写。

提示

  • 修改 SavingAccount 类

    • 仿照练习 1“Account 类的两个子类”一节实现 SavingAccount 类。
    • SavingAccount 类必须扩展 Account 类。
    • 该类必须包含一个类型为 double 的 interestRate 属性。
    • 该类必须包括一个带有两个参数(balance 和 interest_rate)的公有构造器。 该构造器必须通过调用 super(balance)来将 balance 参数传递给父类构造器。
  • 修改 CheckingAccount 类

    • 仿照练习 1“Account 类的两个子类”一节实现 CheckingAccount 类。
    • CheckingAccount 类必须扩展 Account 类
    • 该类必须包括一个关联属性,称作 protectedBy,表示透支保护。该属性的类型为 SavingAccount,缺省值必须是 null。除此之外该类没有其他的数据属性。
    • 该类必须包括一个带有参数(balance)的公有构造器,该构造器必须通过调用 super(balance)将 balance 参数传递到父类构造器。
    • 修改构造器为 CheckingAccount(double balance,SavingAccount protect)构造器。该构造器必须通过调用 super(balance)将 balance 参数传递给父类构造器。
    • CheckingAccount 类必须覆盖 withdraw 方法。该类必须执行下面的检查: 如果当前余额足够弥补取款 amount,则正常进行。如果不够弥补但是存在透支保护则尝试用储蓄账户来弥补这个差值(balance-amount)。如果后一个交易失败,则整个交易一定失败,但余额未受影响。
  • 修改 Customer 类,使其拥有两个账户:一个储蓄账户和一个支票账户:两个都是可选的。
    • 在练习 5_1 修改,原来的 Customer 类包含一个称作 account 的关联属性,可用该属性控制一个 Account 对象。重写这个类,使其包含两个关联属性:savingAccount 和 checkingAccount,这两个属性的缺省值是 null。
    • 包含两个访问方法:getSaving 和 getChecking,这两个方法分别返回储蓄和支票总数。
    • 包含两个相反的方法:SetSaving 和 setChecking,这两个方法分别为 savingAccount 和 checkingAccount 这两个关联属性赋值。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Banking5_2;

//储蓄账户
public class SavingAccount extends Account {
private double interestRate;

public SavingAccount(double balance, double interest_rate) {
super(balance);
this.interestRate = interest_rate;
}

public double getInterestRate() {
return interestRate;
}

public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Banking5_2;

//信用卡账户
public class CheckingAccount extends Account {
private SavingAccount protectedBy;

public CheckingAccount(double balance){
super(balance);
}

public CheckingAccount(double balance, SavingAccount protect){
super(balance);
this.protectedBy = protect;
}

public SavingAccount getProtectedBy() {
return protectedBy;
}

public void setProtectedBy(SavingAccount protectedBy) {
this.protectedBy = protectedBy;
}

//取钱
public boolean withdraw(double amt) {
if (balance >= amt){
balance -= amt;
return true;
}
else if (protectedBy != null && protectedBy.getBalance() >= (amt - balance)){
protectedBy.withdraw(amt - balance);
balance = 0;
return true;
}
else
return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package Banking5_2;

public class Customer {
private String firstName;
private String lastName;
private SavingAccount savingAccount; //储蓄账户
private CheckingAccount checkingAccount; //信用卡账户

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

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public SavingAccount getSaving() {
return savingAccount;
}

public void setSaving(SavingAccount savingAccount) {
this.savingAccount = savingAccount;
}

public CheckingAccount getChecking() {
return checkingAccount;
}

public void setChecking(CheckingAccount checkingAccount) {
this.checkingAccount = checkingAccount;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package TestBanking;

/*
* 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 Banking5_2.*;

public class TestBanking05_2 {

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

// Create two customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
// account = customer.getChecking();
customer.setSaving(new SavingAccount(500.00, 0.05));
customer.setChecking(new CheckingAccount(200.00, customer.getSaving()));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.setChecking(new CheckingAccount(200.00));

// Test the checking account of Jane Simms (with overdraft protection)
customer = bank.getCustomer(0);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ customer.getChecking().getBalance()
+ " and a savings balance of "
+ customer.getSaving().getBalance());
account = customer.getChecking();
System.out.println("Checking Acct [Jane Simms] : withdraw 150.00 succeeds? "
+ account.withdraw(150.00));
System.out.println("Checking Acct [Jane Simms] : deposit 22.50 succeeds? "
+ account.deposit(22.50));
System.out.println("Checking Acct [Jane Simms] : withdraw 147.62 succeeds? "
+ account.withdraw(147.62));
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance()
+ " and a savings balance of "
+ customer.getSaving().getBalance());
System.out.println();

// Test the checking account of Owen Bryant (without overdraft protection)
customer = bank.getCustomer(1);
account = customer.getChecking();
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? "
+ account.withdraw(100.00));
System.out.println("Checking Acct [Owen Bryant] : deposit 25.00 succeeds? "
+ account.deposit(25.00));
System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? "
+ account.withdraw(175.00));
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
System.out.println();
}
}

实验六

题目

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

目的

​ 单例模式。

提示

  • 修改 Bank 类,创建名为 getBanking 的公有静态方法,它返回一个 Bank类的实例。
  • 单个的实例应是静态属性,且为私有。同样,Bank 构造器也应该是私有的。
  • 创建 CustomerReport 类
  • 在前面的银行项目练习中,“客户报告”嵌入在 TestBanking 应用程序的 main 方法中。在这个练习中,改代码被放在,banking.reports 包的 CustomerReport 类中。您的任务是修改这个类,使其使用单一银行对象。
  • 查找标注为注释块/*** ***/的代码行.修改该行以检索单子银行对象。 编译并运行 TestBanking 应用程序看到下列输入结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package Banking6;

public class Bank {
//用于存放客户
private Customer[] customers;
//记录客户的个数
private int numberOfCustomers;

private Bank() {
customers = new Customer[5];
}

private static Bank bank = new Bank();

public static Bank getBanking(){
return bank;
}

//添加一个客户到数组中
public void addCustomer(String f,String l){
Customer c = new Customer(f, l);
customers[numberOfCustomers] = c;
numberOfCustomers ++;
}
//获取客户的个数
public int getNumOfCustomers(){
return numberOfCustomers;
}
//获取指定索引的客户
public Customer getCustomer(int index){
return customers[index];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Banking6;

import java.text.NumberFormat;

public class CustomerReport {
Bank bank = Bank.getBanking();
Customer customer;
public void generateReport(){
NumberFormat currency_format = NumberFormat.getCurrencyInstance();

// Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) {
customer = bank.getCustomer(cust_idx);

System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());

for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++ ) {
Account account = customer.getAccount(acct_idx);
String account_type = "";

// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
if (account instanceof SavingAccount){
account_type = "SavingAccount";
}
if (account instanceof CheckingAccount){
account_type = "CheckingAccount";
}

// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
System.out.println(account_type + ":current balance is " + currency_format.format(account.getBalance()));
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package TestBanking;
/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
import Banking6.*;

public class TestBanking06 {

public static void main(String[] args) {
Bank bank = Bank.getBanking();
Customer customer;
CustomerReport report = new CustomerReport();

// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingAccount(150.00, 0.05));

// Generate a report
report.generateReport();
}
}

实验七

题目

​ 将建立一个 OverdraftException 异常,它由 Account 类的 withdraw( ) 方法抛出。

目的

​ 自定义异常。

提示

  • 创建 OverdraftException 类

    • 在 banking.domain 包中建立一个共有类 OverdraftException,这个类扩展 Exception 类。
    • 添加一个 double 类型的私有属性 deficit,增加一个共有访问方法 getDeficit
    • 添加一个有两个参数的公有构造器。deficit 参数初始化 deficit 属性
  • 修改 Account 类

    • 重写 withdraw 方法使它不返回值( 即 void ),声明方法抛出 overdraftException 异常
    • 修改代码抛出新异常,指明“资金不足”以及不足数额(当前余额扣除请求的数额)
  • 修改 CheckingAccount 类

    • 重写 withdraw 方法使它不返回值(即 void),声明方法抛出 overdraftException 异常
    • 修改代码使其在需要时抛出异常。两种情况要处理:第一是存在没有透支保护的赤字,对这个异常使用“no overdraft protection”信息。第二是 overdraftProtection 数额不足以弥补赤字:对这个异常可使用 ”Insufficient funds for overdraft protection” 信息
  • 编译并运行 TestBanking 程序,结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Customer [simms,Jane]has a checking balance of 200.0 with a 500.0 overdraft 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 funds for overdraft 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]: deposit 25.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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package Banking7;

public class OverdraftException extends Exception{
static final long serialVersionUID = -3888264746582L;
private double deficit; //表示所取的钱与余额的差额

public OverdraftException(String msg, double deficit){
super(msg);
this.deficit = deficit;
}
public double getDeficit() {
return deficit;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package Banking7;

public class Account {
//账户余额
protected double balance;

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

public double getBalance() {
return balance;
}

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

//取钱
public void withdraw(double amt) throws OverdraftException {
if (balance >= amt){
balance -= amt;
}else {
throw new OverdraftException("资金不足",amt-balance);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package Banking7;

//信用卡账户
public class CheckingAccount extends Account {
private Double overdraftProtection;

public CheckingAccount(double balance){
super(balance);
}

public CheckingAccount(double balance, double protect){
super(balance);
this.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 protection",amt-balance);
}
else if (overdraftProtection <= (amt - balance)){
throw new OverdraftException("Insufficient funds for overdraft protection", amt - balance - overdraftProtection);
}
else {
overdraftProtection -= (amt - balance);
balance = 0;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package TestBanking;
/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
import Banking7.*;

public class TestBanking07 {

public static void main(String[] args) {
Bank bank = Bank.getBanking();
Customer customer;
Account account;

// Create two customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 500.00));
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));

// Test the checking account of Jane Simms (with overdraft protection)
customer = bank.getCustomer(0);
account = customer.getAccount(1);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance()
+ " with a 500.00 overdraft protection.");
try {
System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
account.withdraw(150.00);
System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
account.deposit(22.50);
System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
account.withdraw(147.62);
System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
account.withdraw(470.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
}
System.out.println();

// Test the checking account of Owen Bryant (without overdraft protection)
customer = bank.getCustomer(1);
account = customer.getAccount(0);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
try {
System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
account.withdraw(100.00);
System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
account.deposit(25.00);
System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
account.withdraw(175.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
}
}
}

实验八

题目

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

目的

​ 使用集合。

提示

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

    • 将 Customer 属性的声明修改为List 类型,不再使用 numberOfCustomers 属性。
    • 修改 Bank 构造器,将 customers 属性的声明修改为List 类型,不再使用numberOfcustomers 属性
    • 修改 addCustomer 方法,使用 add 方法
    • 修改 getCustomer 方法,使用 get 方法
    • 修改 getNumofCustomer 方法,使用 size 方法
  • 修改 Customer 类,使用 ArrayList 实现多重的账户关系。修改方法同上。

  • 编译运行 TestBanking 程序,这里,不必修改 CustomerReport 代码,因为并没有改变 Bank 和 Customer 类的接口。编译运行TestBanking,应看到下列输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
			CUSTOMERS REPORT
================

Customer: Simms, Jane
SavingAccount:current balance is ¥ 500.00
CheckingAccount:current balance is ¥ 200.00

Customer: Bryant, Owen
CheckingAccount:current balance is ¥ 200.00

Customer: Soley, Tim
SavingAccount:current balance is ¥ 1,500.00
CheckingAccount:current balance is ¥ 200.00

Customer: Soley, Maria
CheckingAccount:current balance is ¥ 200.00
SavingAccount:current balance is ¥ 150.00

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.ArrayList;
import java.util.List;

public class Bank {
//用于存放客户
private List<Customer> customers;

private Bank() {
customers = new ArrayList<>();
}

private static Bank bank = new Bank();

public static Bank getBanking(){
return bank;
}

//添加一个客户到集合中
public void addCustomer(String f,String l){
Customer c = new Customer(f, l);
customers.add(c);
}

//获取客户的个数
public int getNumOfCustomers(){
return customers.size();
}
//获取指定索引的客户
public Customer getCustomer(int index){
return customers.get(index);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.ArrayList;
import java.util.List;

public class Customer {
private String firstName;
private String lastName;
private List<Account> accounts;

public Customer(String f, String l){
firstName = f;
lastName = l;
accounts = new ArrayList<>();
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

// 添加一个账户到集合里
public void addAccount(Account acct){
accounts.add(acct);
}

// 返回账户的个数
public int getNumOfAccounts(){
return accounts.size();
}

// 返回指定索引处的账户
public Account getAccount(int index){
return accounts.get(index);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/

import Banking8.*;

public class TestBanking08 {

public static void main(String[] args) {
Bank bank = Bank.getBanking();
Customer customer;
CustomerReport report = new CustomerReport();

// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingAccount(150.00, 0.05));

// Generate a report
report.generateReport();
}
}

可选:修改 CustomerReport 类

  • 修改 CustomerReport 类,使用 Iterator 实现对客户的迭代
    • 在 Bank 类中,添加一个名为 getCustomers 的方法,该方法返回一个客户列表上的 iterator
    • 在 Customer 类中,添加一个名为个体 Accounts 的方法,该方法返回一个帐户列表上的 iterator
    • 修改 CustomerReport 类,使用一对嵌套的 while 循环(而不是使用嵌套的for 循环),在客户的 iterator 与帐户的 iterator 上进行迭代
    • 重新编译运行 TestBanking 程序,应看到与上面一样的输出结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Bank {
//用于存放客户
private List<Customer> customers;

private Bank() {
customers = new ArrayList<>();
}

private static Bank bank = new Bank();

public static Bank getBanking(){
return bank;
}

//添加一个客户到集合中
public void addCustomer(String f,String l){
Customer c = new Customer(f, l);
customers.add(c);
}

//获取客户的个数
public int getNumOfCustomers(){
return customers.size();
}

//获取指定索引的客户
public Customer getCustomer(int index){
return customers.get(index);
}

public Iterator<Customer> getCustomers(){
return customers.iterator();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Customer {
private String firstName;
private String lastName;
private List<Account> accounts;

public Customer(String f, String l){
firstName = f;
lastName = l;
accounts = new ArrayList<>();
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

// 添加一个账户到集合里
public void addAccount(Account acct){
accounts.add(acct);
}

// 返回账户的个数
public int getNumOfAccounts(){
return accounts.size();
}

// 返回指定索引处的账户
public Account getAccount(int index){
return accounts.get(index);
}

public Iterator<Account> getAccounts(){
return accounts.iterator();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.text.NumberFormat;
import java.util.Iterator;

public class CustomerReport {
Bank bank = Bank.getBanking();
Customer customer;
public void generateReport(){
NumberFormat currency_format = NumberFormat.getCurrencyInstance();

// Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

Iterator<Customer> customers = bank.getCustomers();
while (customers.hasNext()){
customer = customers.next();

System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());

Iterator<Account> accounts = customer.getAccounts();
while (accounts.hasNext()){
Account account = accounts.next();
String account_type = "";

if (account instanceof SavingAccount){
account_type = "SavingAccount";
}
if (account instanceof CheckingAccount){
account_type = "CheckingAccount";
}

System.out.println(account_type + ":current balance is " + currency_format.format(account.getBalance()));
}
}
}
}