【Java基礎 項目實例 -- Bank項目2】Account 和 customer 對象


1 總結: 2 customer.setAccount(account);   //引用,日后的account 和 customer.getAccount()的結果始終一致

 

實驗目的

擴展銀行項目,添加一個 Customer 類。Customer 類將包含一個 Account對 象。 
 
 
實驗目的: 
 使用引用類型的成員變量。 
 
 提 示: 
 1. 在banking包下的創建Customer類。該類必須實現上面的UML圖表中的模 型。 
 
a. 聲明三個私有對象屬性:firstName、lastName 和 account。  
 b. 聲明一個公有構造器,這個構造器帶有兩個代表對象屬性的參數(f 和 l)  
 c. 聲明兩個公有存取器來訪問該對象屬性,方法 getFirstName 和 getLastName 返   回相應的屬性。   d. 聲明 setAccount 方法來對 account 屬性賦值。  
 e. 聲明 getAccount 方法以獲取 account 屬性。  
 
2. 在 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 

UML 結構圖

工程組織結構:

 

代碼編程:

1/Account.java

package Banking_2;

public class Account {
    private double balance;//余額  ,uml前該變量是 '-'
    public Account(double init_balance){
        balance=init_balance;
    }
    public double getBalance() {
        return balance;
    }
    //存錢
    public void deposit(double amt){
        this.balance+=amt;
    }
    //取錢
    public void withdraw(double amt){
        this.balance-=amt;
    }

}

2/Customer.java

package Banking_2;

public class Customer {
    private String firstName;
    private String lastName;
    private  Account account;
    public Customer(String f,String l){
        this.firstName=f;
        this.lastName=l;
    }
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
}

3/新開一個package, 添加測試文件:TestBanking2.java

package TestBanks;
import Banking_2.*;

public class TestBanking2{
    public static void main(String[] args) {
        Customer customer=new Customer("Jane","Smith");
        Account  account=new Account(500);

        // Create an account that can has a 500.00 balance.

        System.out.println ("Creating the customer Jane Smith.");
        System.out.println("Creating her account with a 500.00 balance.");
        customer.setAccount(account);
        //code
        System.out.println("Withdraw 150.00");
        customer.getAccount().withdraw(150);
        //code
        System.out.println("Deposit 22.50");
        customer.getAccount().deposit(22.5);
        //code
        System.out.println("Withdraw 47.62");
        customer.getAccount().withdraw(47.62);
        //code
        // Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName()
                + "] has a balance of " + account.getBalance());
        //等價於下面的寫法
        /*
         System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName()
                + "] has a balance of " + customer.getAccount().getBalance());

         */
    }
}

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM