2015-02-07 147 views
2

我對OOP和Java相當陌生,有一個可能微不足道的問題,但我無法在Web上找到答案。JAVA - 兩個對象之間的關係

我正在做Java中的標準銀行賬戶程序 - 有客戶的程序,每個客戶都有銀行賬戶(一個客戶可能有多個銀行賬戶!),我可以存入或取出資金。每個銀行帳戶都有唯一的編號(即使某人擁有多個銀行帳戶,每個銀行帳戶都有其唯一編號)

我的代碼編譯和操作存取都正常工作。

問題出在以下 - 我不能將多個銀行賬戶歸於客戶,在我的程序中,客戶可以只有一個銀行並且不會超過一個銀行賬戶。

我有3個類 - 帳戶,客戶端,BankMain。你可以看到他們下面

public class Account { 
    private int balance; 
    private String NumAccount; // each bank account has a unique number 

    public Account(int money, String num) { 
     balance = money; 
     NumAccount = num; 
    } 

    public String printAccountNumber() { 
     return NumAccount; 
    } 
    // below are the methods for withdraw,deposit- they are working properly 
} 

類客戶端

public class Client { 
    private String name;// name of the client 
    private Account account; 

    // the account associated to the client. Probably I should change sth 
    // here 
    // but I don't know what 
    public Client(String nom, Compte c) { 
     name = nom; 
     account = c; 
    } 

    public void printName() { 
     System.out.println(
       "Name: " + name 
       + "\nAccount number: " + account.printAccountNumber() 
       + "\nSolde: " + account.printBalance() + "\n" 
     ); 
    } 
} 

而且BankMain

public class BankMain { 

    public static void main(String[] args) { 
     Account account1 = new Account(1000, "11223A"); 
     Account account2 = new Account(10000, "11111A"); 
     Client client1 = new Client("George", account1); 
     Client client2 = new Client("Oliver", account2); 
     // now this is working correctly 
     client1.printName(); 
     client2.ptintName(); 
     /* 
     * The problem is that I don't know what to do if I want account1 
     * and account2 to belong both to client1. I tried Client client1 = 
     * new Client("George",account1); Client client1 = new 
     * Client("Oliver", account2); but got compilation error 
     */ 
    } 
} 

你知道我怎麼能解決這個問題?我應該怎麼做才能讓多個銀行賬戶與同一客戶關聯?

在此先感謝 喬治

回答

1

結帳驗證碼:

//Account 

public class Account 
{ 
    private int balance; 
    private String accNo; 

    public Account(int money,String num) { 
    balance = money; 
    accNo = num; 
} 

public int getBalance() { 
     return balance; 
    } 

public void setBalance(int balance) { 
    this.balance = balance; 
} 

public String getAccNo() { 
    return accNo; 
} 

public void setAccNo(String accNo) { 
    this.accNo = accNo; 
} 

}

//Client 

import java.util.ArrayList; 
import java.util.Collection; 

public class Client 
{ 
    private String clientName; 
    private HashSet<Account> accounts; 
public Client(String name) 
{ 
    this.clientName = name; 
    this.accounts = new HashSet<Account>(); 
} 

public void addAccount(Account account) { 

    accounts.add(account); 
} 

public String getClientName() { 
    return clientName; 
} 

public void setClientName(String clientName) { 
    this.clientName = clientName; 
} 

public Collection<Account> getAccounts() { 
    return accounts; 
} 

public void setAccounts(HashSet<Account> accounts) { 
    this.accounts = accounts; 
} 

public void printAccountDetails() { 

    System.out.println("Account details :"); 
    int counter= 0; 
    for(Account acc : accounts) { 
     counter ++; 
     System.out.println("Account details for Account '"+counter+"' :\n"); 
     System.out.println("Account Number : "+ acc.getAccNo() +" Balance :" + acc.getBalance()); 
    } 
} 

}

// Bank class 


public class BankMain { 

    public static void main(String[] args) 
    { 

     Account account1 = new Account(1000,"11223A"); 
     Account account2 = new Account(10000,"11111A"); 
     Client client = new Client("George"); 

     client.addAccount(account1); 
     client.addAccount(account2); 

     client.printAccountDetails(); 
    } 

} 

在這裏您可以根據需要添加多個帳戶。

+0

這裏是輸出:'賬戶詳細信息:帳戶 '1' 帳戶詳細資料: 帳戶號碼:11223A餘額:1000個 帳戶細節的帳戶 '2': 帳戶號碼:11111A餘額:10000 ' – 2015-02-07 13:21:40

+0

確定,謝謝,它工作正常,但我不明白爲什麼我們需要使用HashSet?爲什麼它只能用於Set? – George 2015-02-07 20:40:44

+0

Set是更好的選擇。它不一定是HashSet。兩者之間的區別在於Set是一個接口,而HashSet是具體的類。您無法創建接口的實例。 – 2015-02-07 20:48:41

0

而不是試圖重新定義客戶端1和2又像:

Client client1 = new Client("George",account1); 
Client client1 = new Client("Oliver", account2); 

重新定義這些對象:

client1 = new Client("George",account1); 
... 
client1 = new Client("Oliver", account2); 

但這樣做,你可以操作在同一個賬戶上,即如果你現在做了client1.withdraw,你會退出奧利弗的賬戶,而不是喬治。

而不是這樣做,你可以維護地圖中的名稱和賬戶對象,並給出名稱,你可以隨時獲取該人的賬戶。像:

Map<String, Account> nameAccountMap = .. 

然後你相應的帳戶添加到它想:

nameAccountMap.put("Oliver", account2); 
nameAccountMap.put("George",account1); 

現在,如果你希望在奧利弗自有賬戶進行操作,你可以通過這樣做:

nameAccountMap.get("Oliver").withdraw... 

以及其他帳戶持有人的類似操作。

如果您希望多個帳戶與用戶相關聯,你可以保持地圖的名稱和等的用戶持有的賬戶列表:

Map<String, List<Account>> nameAccountMap = .. 
+1

我懷疑這是OP尋找的答案。 – Sid 2015-02-07 12:48:15

+0

問題是如何爲一個客戶擁有多個帳戶。 – m0skit0 2015-02-07 12:48:43

+0

有多個問題。 1.客戶端client1 =新客戶端(「George」,account1); 客戶端client1 =新客戶端(「Oliver」,account2); ,但得到編譯錯誤,正如OP在代碼中提到的那樣。你知道我該如何解決這個問題? 2. >>我應該怎麼做才能讓多個銀行賬戶關聯到同一個客戶端?所以它的答案都是。 – SMA 2015-02-07 12:54:27

1

你可以有多個帳戶的一個客戶端更改數據類型在Client類中。例如:

private Map<String, Account> accounts; 

其中地圖的關鍵是帳號,而該值是帳號本身。通過這種方式,您可以通過其唯一號碼訪問每個帳戶。

(如果你不知道的地圖是什麼,檢查this

這也意味着你需要進行修改的Client構造函數接受多個帳戶或添加新的方法,以一個新的帳戶添加到Client

+0

這是另一個有趣的問題解決方法。 – Sid 2015-02-07 12:59:29

+0

有沒有更簡單的方法將一個客戶與許多銀行賬戶相關聯?是否有可能在客戶端類中創建一組帳戶?如果是,我該怎麼做? 預先感謝您,喬治 – George 2015-02-07 18:47:25

+0

地圖是一個簡單的數據結構。事實上,你可以像數組一樣考慮它:每個索引(鍵)都有一個值。可以有一個數組,但在Java中很少使用數組,因爲通常會有更好的數據結構來滿足您的需求。如果您以後需要檢索這些帳戶,您將如何處理數組?你將不得不通過所有的數組,直到你找到你想要的賬戶。使用地圖,您只需向地圖詢問相關密鑰的值(在這種情況下,帳戶號碼是合適的密鑰,因爲它在所有帳戶中都是唯一的)。 – m0skit0 2015-02-07 19:59:26

0

而不是在您的客戶類中有一個Account,有一個Set<Account>,以便有一對多的關係。確保帳戶類別已實施equalshashcode

public class Client 
{ 
    private String name;//name of the client 
    private Set<Account> accounts; 
//the account associated to the client. Probably I should change sth here 
// but I don't know what 
    public Client(String nom, Set<Account> c) 
    { 
     name = nom; 
     account = c; 
    } 
    public String getName() 
    { 
     return this.name; 
    } 

    public Set<Account> getAccounts() 
    { 
     return this.accounts; 
    } 
    public String toString() 
    { 
     ... 
     // Return description of the Object state 
    } 
} 





public class Account 
{ 
    private int balance; 
    private String NumAccount; //each bank account has a unique number 
    public Account(int money,String num) 
    { 
     balance = money; 
     NumAccount = num; 
    } 
    public String getAccountNumber() 
    { 
     return NumAccount; 
    } 
    public boolean equals(..) 
    { 
     ... 
    } 
    public int hashcode() 
    { 
     ... 
    } 
    public String toString() 
    { 
     ... 
     // Return description of the Object state 
    } 
    // below are the methods for withdraw,deposit- they are working properly 
} 
+0

是否有更簡單的方法將一個客戶與許多銀行賬戶相關聯?是否有可能在客戶端類中創建一組帳戶?如果是,我該怎麼做?預先感謝您 – George 2015-02-07 19:02:44

+0

您可以有一個陣列,但在我看來,一套更適合。但是,可以使用'Account'對象數組。 – Sid 2015-02-08 05:33:49

1

正如@ m0skit0建議的那樣,使用地圖或列表來保存客戶端類下的Account對象。

public class Client 
{ 
    private String name;//name of the client 
    private List<Account> accounts = new ArrayList<Account>(); 

    public Client(String nom, Account c) 
    { 
     name = nom; 
     accounts.add (c); 
    } 

    public boolean addAccount (Account c) 
    { 
     return accounts.add (c); 
    } 
    public removeAccount (Account c) 
    { 
     final int accountIndex = accounts.indexOf (c); 
     if (accountIndex > 0) 
     { 
      accounts.remove (accountIndex); 
      return true; 
     } 
     return false; 
    } 

    public void printName() 
    { 
     System.out.println("Name: " + name); 
     System.out.println ("Total Accounts: " + accounts.size()); 
     for (int i=0; i<accounts.size(); i++) 
     { 
      final Account a = accounts.get(i); 
      System.out.println ("\tAccount Number: " + a.printAccountNumber()); 
      System.out.println ("\tAccount Balance: " + a.printBalance()); 
     }   
    } 
} 

,並使用它在你的BankMain.java

Account account1 = new Account(1000,"11223A"); 
Account account2 = new Account(10000,"11111A"); 
Client client1 = new Client("George", account1); 
client1.addAccount (account2); 

client1.printName(); 
+0

我寧願使用Set來進行數據完整性檢查,我不認爲帳號順序是有意義的。只是我的觀點。 – Sid 2015-02-07 13:17:57

相關問題