实验题目 6:(在5_续1的基础上修改)
修改 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
尚硅谷 Java 基础实战—Bank 项目
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
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 boolean withdraw(double amt){
if (balance >= amt){
balance -= amt ;
return true ;
} else {
System.out.println("余额不足!");
return false ;
}
}
}
Bank.java
package banking;
public class Bank {
private Customer[] customers ;
private int numberOfCustomer ;
private Bank(){
customers = new Customer[5] ;
numberOfCustomer = 0 ;
}
private static Bank bank = new Bank() ;
public static