练习一:创建一个简单的银行程序包。
练习目标:
1. Java语言中面向对象的封装特性及构造方法的使用。
任务及操作步骤:
在这个练习里,创建一个简单版本的Account类,将这个源文件放入banking程序包中,在创建单个帐户的默认程序包中,编写一个测试程序TestBanking。这个测试程序初始化帐户余额,并可执行几种简单的事务处理。最后,该测试程序显示该帐户的最终余额。
1. 创建banking包
2. 在banking包下创建Account类,该类必须实现上述UML框图中的模型。
a) 声明一个私有对象属性:balance,该属性保存了银行帐户的当前(或即时)余额。
b) 声明一个带参数(init_balance)的构造方法:该参数为balance属性赋值。
c) 声明一个公有方法getBalance,该方法用于获取帐户余额。
d) 声明一个公有方法deposit,该方法用于向当前余额增加金额。
e) 声明一个公有方法withdraw,该方法用于从当前余额减少金额。
3. 编译并运行TestBanking类,可以看到下列输出结果:
创建一个余额为500.00的账户
取款150.00
存款22.50
取款47.62
该账户目前的余额为:324.88
练习二:改进银行项目
练习目标:使用引用类型成员变量
1. 扩展银行项目,添加一个Customer类,Customer类将包含一个Account对象。
任务及操作步骤:
1. 在banking包中创建Customer类,该类必须实现上述UML框图中的模型。
a) 声明三个私有属性:firstName,lastName和account。
b) 声明一个公有构造方法:该构造方法中带有两个属性参数(firstName f和lastName l)。
c) 声明两个公有方法:getFirstName和getLastName返回该对象的属性值。
d) 声明setAccount方法对account赋值。
e) 声明getAccount方法获取account属性。
2. 编译运行TestBanking程序,应该看到如下输出结果:
Creating the customer Jane Smith
Creating her account with a 500.00 balance
Withdraw 150.00
Deposit 22.50
Withdraw 47.62
Customer [Jane Smith] has a balance of 324.88
练习三:修改withdraw方法
练习目标:使用有返回值的方法。
1. 修改withdraw方法以返回一个布尔值来指示交易是否成功。
任务及操作步骤:
1. 修改Account类。
a) 修改deposit方法返回true(意味着所有存款都是成功的)。
b) 修改withdraw方法来检查取款金额是否大于余额,如果amt不大于balance,则从余额中扣除取款金额并返回true,否则余额不变返回false。
2. 编译并运行TestBanking
Creating the customer Jane Smith
Creating the account with a 500.00 balance
Withdraw 150.00 : true
Deposit 22.50 : true
Withdraw 47.62 : true
Withdraw 400.00 : false
Customer [Jane Smith] has a balance of 324.88
练习四:用数组表示多重性
练习目标:在类中使用数组作为模拟集合操作。
1. 用数组实现银行和客户间的多重关系。
任务及操作步骤:
对银行来说,可添加Bank类。Bank对象跟踪自身与其客户间的关系。用Customer对象的数组实现这个集合化的关系,还要包含一个整数属性来跟踪银行当前客户数量。
1. 创建Bank类。
2. 为Bank类增加两个属性:customers(Customer对象的数组)和numberofCustomers(整数,跟踪下一个customers数组索引)。
3. 添加公有构造方法,以合适的最大容量(至少大于5)初始化customers数组。
4. 添加addCustomer方法,该方法必须依照参数(姓,名)构造一个新的Customer对象然后把它放到customer数组中,还必须把numberofCustomers属性值加1。
5. 添加getNumberofCustomers访问方法,它返回numberofCustomers属性值。
6. 添加getCustomer方法,它返回与给出的index参数相关的客户。
7. 编译并运行TestBanking程序,可以看到下列输出结果:
Customer[1] is Lionel Messi
Customer[2] is Kobe Bryant
Customer[3] is David Beckham
Customer[4] is Michael Owen
练习五:用面向对象的思想解决问题
练习目标:继承、多态、方法重写。
1. 在本练习中,将在银行项目中创建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)并设置overdraftProtection属性,将balance参数传递给父类构造器。
5. CheckingAccount类必须重写withdraw方法,此方法必须执行下列检查,如果当前余额足够弥补取款amount,则正常进行,如果不够弥补但存在透支保护,则尝试用overdraftProtection的值来弥补该差值(balance-amount),如果弥补该透支所需要的金额大于当前的保护级别,则整个交易失败,但余额未受影响。
6. 运行TestBanking程序,输出以下结果:
Creating the customer Jane Smith.
Creating her Savings Account with a 500.00 balance and 3% interest.
Creating the customer Owen Bryant.
Creating her Checking Account with a 500.00 balance and no overdraft protection.
Creating the customer Tim Soley.
Creating the 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 [Smith,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 1500.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
Widthdraw 750.00:false
Customer [Soley,Maria] has a balance of 150.00
练习六:创建客户帐户
练习目标:instanceof运算符
任务及操作步骤:
1. 修改Customer类来处理具有多种类型的联合帐户。(例如用数组表示多重性一节所做的,该类必须包括以下的公有方法:addAccount(Account)、getAccount(int)和getNumOfAccount)
2. 完成TestBanking程序:该程序创建一个客户和帐户的集合,并生成这些客户及其账户余额的报告。
3. 使用instanceof操作符测试拥有的账户类型,并且将account_type设置为适当的值,例如:”Savings Account”或”Checking Account”。
4. 编译并运行该程序,将输出以下结果:
CUSTOMERS REPORT
=================
Customer: Simth,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 $1500.00
Checking Account:Current balance is $200.00
Customer: Soley,Maria
Savings Account:Current balance is $150.00
Checking Account:Current balance is $200.00
练习七:
练习目标:实现更为复杂的透支保护机制
注意:这是练习1的选择练习,它包括了更为复杂的透支保护机制模型。它使用客户储蓄帐户完成透支保护,本练习必须在完成上述两个练习后进行。
1. 仿照练习1“Account类的两个子类”一节实现SavingAccount类。
2. SavingAccount类必须扩展Account类。
3. 该类必须包含一个类型为double的interestRate属性。
4. 该类必须包含一个带有两个参数(balance和interest_rate)的公有构造器。该构造器必须通过调用super(balance)来将balance参数传递给父类构造器。
5. 仿照练习1“Account类的两个子类”一节实现CheckingAccount类。
6. CheckingAccount类必须扩展Account类。
7. 该类必须包含一个关联属性,称为protectedBy,该属性的类型为SavingAccount,默认值必须是null,除此之外该类没有其他的数据属性。
8. 该类必须包含一个带有参数(balance)的公有构造器,该构造器必须通过调用super(balance)将balance参数传递到父类构造器。
9. 该类必须包含另一个带有两个参数的(balance和protect)的公有构造器,该构造器必须通过调用super(balance)将balance参数传递给父类构造器。
10. CheckingAccount类必须重写withdraw方法。该类必须执行下面的检查:如果当前月足够弥补取款amount,则正常进行。如果不够弥补,但是存在透支保护则尝试用储蓄帐户来弥补这个差值(balance_amount)。如果后一个交易失败,则整个交易一定失败,但余额未受影响。
修改客户使其拥有两个帐户
修改Customer类使其拥有两个银行帐户:一个储蓄帐户和一个信用帐户,两个都是可选的
11. 原来的Customer类包含一个称作account的关联属性,可用该属性控制一个Account对象。重写这个类,使其包含两个关联属性:savingAccount和checkingAccount,这两个属性默认值为null。
12. 包含两个访问方法:getSaving和getChecking,这两个方法分别返回储蓄帐户和信用帐户总数。
13. 包含两个相反的方法:setSaving和setChecking,这两个方法分别为储蓄帐户和信用帐户这两个关联属性赋值。
14. 编译执行TestBanking程序,输出应为:
Customer [Simth,Jane] has a checking balance of 200 and a saving balance of 500.
Checking Account[Simth,Jane]:withdraw 150 successed?true
Checking Account[Simth,Jane]:deposit 22.50 successed?true
Checking Account[Simth,Jane]:withdraw 147.62 successed?true
Customer [Simth,Jane] has a checking balance of 0.0 and a saving balance of 424.88.
Customer [Bryant,Owen] has a checking balance of 200.00.
Checking Account[Bryant,Owen]:withdraw 100 successed?true
Checking Account[Bryant,Owen]:deposit 25 successed?true
Checking Account[Bryant,Owen]:withdraw 175 successed?false
Customer[Bryant,Owen] has a checking balance of 125.0
练习八:
练习目标:使用集合
任务及操作步骤:
修改 Bank 类,利用 ArrayList 实现多重的客户关系
1. 将 Customer 属性的声明修改为List 类型,不再使用numberOfCustomers 属性。
2. 修改 Bank 构造器,将 customers 属性的声明修改为List 类型,不再使用numberOfcustomers 属性
3. 修改 addCustomer方法,使用add方法
4. 修改 getCustomer方法,使用get方法
5. 修改 getNumofCustomer方法,使用size方法
修改 Customer 类
6. 修改 Customer 类,使用 ArrayList 实现多重的账户关系。修改方法同上。
编译运行 TestBanking 程序