基于上一个实验Banking_5 ,代码先全部复制过来.
笔记心得:
1.SavingAccount() 需要两种构造方法,接受单个参数和两个的 2.Account 有两个类型 SavingAccount 和 CheckingAccount ,每个人都有两个或一个 ,
setAccount(Account account,int id) ,id=1 表示设置前者, id=2表示设置后者. 3.在 public class CustomerReport{}中 ,首先调用static Bank : Bank bank=Bank.getBank();
然后遍历其中元素, 可能存在只有一个账户,所以需要特判一下:
if(customer.getSavingAccount()!=null) 和if(customer.getCheckingAccount()!=null) ,解决这个空内存 的异常!
实验要求:
实验目的: 单例模式。 提示: 1. 修改 Bank 类,创建名为 getBanking 的公有静态方法,它返回一个 Bank 类的实例。 2. 单个的实例应是静态属性,且为私有。同样,Bank 构造器也应该是私有的 创建 CustomerReport 类 1.在前面的银行项目练习中,“客户报告”嵌入在 TestBanking 应用程序的 main 方法中。在这个练习中,改代码被放在,banking.reports 包的 CustomerReport 类中。您的任务是修改这个类,使其使用单一银行对象。 2. 查找标注为注释块/*** ***/的代码行.修改该行以检索单子银行对象。
文件工程组织: (基于上一个实验Banking_5 ,代码 先 全部复制过来,然后再具体改改.)
具体代码:
Bank.java (构造器方法私有化,private static Bank bank=new Bank();)
package Banking_6; //要求使bank实现单例模式 public class Bank { private Customer[] customers ; //用于存放客户 private int numberofCustomers; //用于记录Customer的个数 private Bank(){ ///构造器方法私有化 numberofCustomers=0; customers = new Customer[100]; ///这里记得要初始化!不然要发生java.lang.NullPointerException } private static Bank bank=new Bank(); public static Bank getBank(){ return bank; } public void addCustomer(String f,String l){ int i= this.numberofCustomers; customers[i]=new Customer(f,l);//新建一个构造对象 this.numberofCustomers++; } public int getNumOfCustomers() { return numberofCustomers; } public Customer getCustomer(int index) { return customers[index]; } }
CustomerReport.java (打印全部Bank的信息 )
package Banking_6; //遍历银行里的所有账户,进行汇报 public class CustomerReport { Bank bank=Bank.getBank(); public void generateReport(){ System.out.println("======================================="); System.out.println(" "+"CUSTOMERS REPORT"); System.out.println("======================================="); int num=bank.getNumOfCustomers(); Customer customer; Account Savingaccount,Checkingaccount; for(int i=0;i<num;i++){ customer=bank.getCustomer(i); Savingaccount = customer.getSavingAccount(); Checkingaccount=customer.getCheckingAccount(); //输出所有存款信息 System.out.println("Customer: "+customer.getFirstName()+" "+customer.getLastName()); if(customer.getSavingAccount()!=null) System.out.println("SavingAccount: current balance is $"+customer.getSavingAccount().getBalance()); if(customer.getCheckingAccount()!=null) System.out.println("CheckingAccount: current balance is $"+customer.getCheckingAccount().getBalance()); System.out.println(); } } }
TestBank.java
package TestBanks; import Banking_6.*; public class TestBanking_6 { public static void main(String[] args) { Bank bank = Bank.getBank(); Customer customer; CustomerReport report = new CustomerReport(); // Create several customers and their accounts bank.addCustomer("Jane", "Simms"); customer = bank.getCustomer(0); customer.setAccount(new SavingAccount(500.00, 0.05),1); customer.setAccount(new CheckingAccount(200.00, 400.00),2); bank.addCustomer("Owen", "Bryant"); customer = bank.getCustomer(1); customer.setAccount(new CheckingAccount(200.00),2); bank.addCustomer("Tim", "Soley"); customer = bank.getCustomer(2); customer.setAccount(new SavingAccount(1500.00, 0.05),1); customer.setAccount(new CheckingAccount(200.00),2); bank.addCustomer("Maria", "Soley"); customer = bank.getCustomer(3); // Maria and Tim have a shared checking account customer.setAccount(bank.getCustomer(2).getCheckingAccount(),1); customer.setAccount(new SavingAccount(150.00, 0.05),2); // Generate a report report.generateReport(); } }
Account.java
package Banking_6; public class Account { protected double balance=0;//余额 ,uml前该变量是 '-' public Account(double init_balance){ balance=init_balance; } public double getBalance() { return balance; } public void setBalance(double b){this.balance=b;} //存钱 public boolean deposit(double amt){ this.balance+=amt;return true; } //取钱 public Boolean withdraw(double amt) { if(amt>this.balance) return false; else{ this.balance-=amt; return true; } } }
TestBanking_6.java (测试类)
1 package TestBanks; 2 import Banking_6.*; 3 public class TestBanking_6 { 4 5 public static void main(String[] args) { 6 Bank bank = Bank.getBank(); 7 Customer customer; 8 CustomerReport report = new CustomerReport(); 9 10 // Create several customers and their accounts 11 bank.addCustomer("Jane", "Simms"); 12 customer = bank.getCustomer(0); 13 customer.setAccount(new SavingAccount(500.00, 0.05),1); 14 customer.setAccount(new CheckingAccount(200.00, 400.00),2); 15 16 bank.addCustomer("Owen", "Bryant"); 17 customer = bank.getCustomer(1); 18 customer.setAccount(new CheckingAccount(200.00),2); 19 20 bank.addCustomer("Tim", "Soley"); 21 customer = bank.getCustomer(2); 22 customer.setAccount(new SavingAccount(1500.00, 0.05),1); 23 customer.setAccount(new CheckingAccount(200.00),2); 24 25 bank.addCustomer("Maria", "Soley"); 26 customer = bank.getCustomer(3); 27 // Maria and Tim have a shared checking account 28 customer.setAccount(bank.getCustomer(2).getCheckingAccount(),1); 29 customer.setAccount(new SavingAccount(150.00, 0.05),2); 30 31 // Generate a report 32 report.generateReport(); 33 } 34 }
输出结果:
======================================= CUSTOMERS REPORT ======================================= Customer: Jane Simms SavingAccount: current balance is $500.0 CheckingAccount: current balance is $200.0 Customer: Owen Bryant CheckingAccount: current balance is $200.0 Customer: Tim Soley SavingAccount: current balance is $1500.0 CheckingAccount: current balance is $200.0 Customer: Maria Soley SavingAccount: current balance is $200.0 CheckingAccount: current balance is $150.0