2練習1:創建一個簡單的銀行程序包
練習目標-Java 語言中面向對象的封裝性及構造器的使用。
任務
在這個練習里,創建一個簡單版本的(賬戶類)Account類。將這個源文件放入banking程序包中。在創建單個帳戶的默認程序包中,已編寫了一個測試程序TestBanking。這個測試程序初始化帳戶余額,並可執行幾種簡單的事物處理。最后,該測試程序顯示該帳戶的最終余額。
1. 創建banking 包
2. 在banking 包下創建Account類。該類必須實現上述UML框圖中的模型。
- 聲明一個私有對象屬性:balance,這個屬性保留了銀行帳戶的當前(或即時)余額。
- 聲明一個帶有一個參數(init_balance)的公有構造器,這個參數為balance屬性賦值。
- 聲明一個公有方法getBalance,該方法用於獲取經常余額。
- 聲明一個公有方法deposit,該方法向當前余額增加金額。
- 聲明一個公有方法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
練習2
練習目標-使用引用類型的成員變量:在本練習中,將擴展銀行項目,添加一個(客戶類)Customer類。Customer類將包含一個Account對象。
任務
- 在banking包下的創建Customer類。該類必須實現上面的UML圖表中的模型。
a. 聲明三個私有對象屬性:firstName、lastName和account。
b. 聲明一個公有構造器,這個構造器帶有兩個代表對象屬性的參數(f和l)
c. 聲明兩個公有存取器來訪問該對象屬性,方法getFirstName和getLastName返回相應的屬性。
d. 聲明setAccount 方法來對account屬性賦值。
e. 聲明getAccount 方法以獲取account屬性。
- 在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
練習3:修改withdraw 方法
練習目標-使用有返回值的方法:在本練習里,將修改withdraw方法以返回一個布爾值來指示交易是否成功。
任務
1. 修改Account類
- 修改deposit 方法返回true(意味所有存款是成功的)。
- 修改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
練習4:用數組表示多重性
練習目標-在類中使用數組作為模擬集合操作: 在本練習中,將用數組實現銀行與客戶間的多重關系。
任務
對銀行來說,可添加Bank類。 Bank 對象跟蹤自身與其客戶間的關系。用Customer對象的數組實現這個集合化的關系。還要保持一個整數屬性來跟蹤銀行當前有多少客戶。
- 創建 Bank 類
- 為Bank類增加兩個屬性:customers(Customer對象的數組)和numberOfCustomers(整數,跟蹤下一個customers數組索引)
- 添加公有構造器,以合適的最大尺寸(至少大於5)初始化customers數組。
- 添加addCustomer方法。該方法必須依照參數(姓,名)構造一個新的Customer對象然后把它放到customer數組中。還必須把numberofCustomers屬性的值加1。
- 添加getNumOfCustomers 訪問方法,它返回numberofCustomers屬性值。
- 添加getCustomer方法。它返回與給出的index參數相關的客戶。
- 編譯並運行TestBanking程序。可以看到下列輸出結果:
Customer [1] is Simms,Jane
Customer [2] is Bryant,Owen
Customer [3] is Soley,Tim
Customer [4] is Soley,Maria
練習4:用List表示多重性
練習目標-在類中使用List作為模擬集合操作: 在本練習中,將用List實現銀行與客戶間的多重關系。
任務
對銀行來說,可添加Bank類。 Bank 對象跟蹤自身與其客戶間的關系。用Customer對象的List實現這個集合化的關系。還要保持一個整數屬性來跟蹤銀行當前有多少客戶。
- 創建 Bank 類
- 為Bank類增加兩個屬性:customers(Customer對象的List)和numberOfCustomers(整數, 當前Customer對象的數量)
- 添加公有構造器,初始化customersList。
- 添加addCustomer方法。該方法必須依照參數(姓,名)構造一個新的Customer對象然后把它放到customerList中。
- 添加getNumOfCustomers 訪問方法,它返回numberofCustomers屬性值。
- 添加getCustomer方法。它返回與給出的index參數相關的客戶。
- 編譯並運行TestBanking程序。可以看到下列輸出結果:
Customer 1 is Simms,Jane
Customer 2 is Bryant,Owen
Customer 3 is Soley,Tim
Customer 4 is Soley,Maria
當前客戶數量 = 4
第一小問:
package com.banking; public class Account { //屬性 private double balance; //構造方法 Account(int balance) { this.balance=balance; } public Account() { super(); } //成員方法 public double getBalance() { return this.balance; } public double deposit(double amt) { this.balance+=amt; return amt; } public double withdraw(double amt) { this.balance-=amt; return amt; } }
測試:
package com.banking; public class Test01 { public static void main(String[] args) { Account ac=new Account(500); System.out.println("取款金額:"+ac.withdraw(150)); System.out.println("存款金額:"+ac.deposit(22.5)); System.out.println("取款金額:"+ac.withdraw(47.62)); System.out.println("剩余金額:"+ac.getBalance()); } }
結果:
第二小問:
package com.banking; public class Customer { private String firstName; private String lastName; private Account account; Customer(String f,String l) { this.firstName=f; this.lastName=l; } @Override public String toString() { return firstName + "," + lastName ; } public Account getAccount() { return account; } public void setAccount(Account acct) { this.account = acct; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } }
測試:
package com.banking; public class Test02 { public static void main(String[] args) { Customer cu=new Customer("Jane","Smith"); System.out.println("Creating the customer"+" "+cu.getFirstName()+" "+cu.getLastName()); cu.setAccount(new Account(500)); // cu.setAccount(ac); // cu.getAccount(); System.out.println("取款金額:"+cu.getAccount().withdraw(150)); System.out.println("存款金額:"+cu.getAccount().deposit(22.5)); System.out.println("取款金額:"+cu.getAccount().withdraw(47.62)); System.out.println("剩余金額:"+cu.getAccount().getBalance()); } }
結果:
第三小問”:
package com.banking; public class Account { //屬性 private double balance; //構造方法 Account(int balance) { this.balance=balance; } public Account() { super(); } //成員方法 public double getBalance() { return this.balance; } public boolean deposit(double amt) { this.balance+=amt; System.out.print("deposit "+amt); return true; } public boolean withdraw(double amt) { System.out.print("withdraw "+amt); if(amt<balance){ this.balance-=amt; return true; } else { this.balance=amt; return false; } } }
測試:
package com.banking; public class Test02 { public static void main(String[] args) { Customer cu=new Customer("Jane","Smith"); System.out.println("Creating the customer"+" "+cu.getFirstName()+" "+cu.getLastName()); cu.setAccount(new Account(500)); // cu.setAccount(ac); // cu.getAccount(); System.out.println(" 取款金額:"+cu.getAccount().withdraw(150)); System.out.println(" 存款金額:"+cu.getAccount().deposit(22.5)); System.out.println(" 取款金額:"+cu.getAccount().withdraw(47.62)); System.out.println(" 取款金額:"+cu.getAccount().withdraw(400)); System.out.println(cu.toString()+" "+"剩余金額:"+cu.getAccount().getBalance()); } }
結果:
第四小問:
package com.banking; import java.util.ArrayList; import java.util.List; public class Bank { //定義屬性 private List<Customer> customer; private int numberOfCustomers; //構造器 public Bank(){ customer=new ArrayList<>(); } //定義方法,添加數據 public void addCustomer(String f,String l){ Customer cu=new Customer(f,l); customer.add(cu); } //獲取數組個數 public int getNumOfCustomers(){ return customer.size(); } public Customer getCustomer(int index){ return customer.get(index); } }
測試:
package com.banking; import java.util.Iterator; import java.util.List; public class TestBank { public static void main(String[] args) { Bank ba=new Bank(); ba.addCustomer("Simms","Jane"); ba.addCustomer("Bryant","Owen"); ba.addCustomer("Soley","Tim"); ba.addCustomer("Soley","Maria"); for(int i=0;i<ba.getNumOfCustomers();i++){ System.out.println("Customer "+(i+1)+" is "+ba.getCustomer(i)); } System.out.println("當前客戶數量="+ba.getNumOfCustomers()); } }
結果: