JAVA - 两个对象之间的关系

时间:2015-02-07 12:43:50

标签: java class object

我对OOP和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
         */
    }
}

你知道我该如何解决这个问题吗?我该怎么办才能让多个银行账户关联到同一个客户?

提前致谢 乔治

5 个答案:

答案 0 :(得分:1)

您可以为一个客户端设置多个帐户,以更改Client类中的数据类型。例如:

private Map<String, Account> accounts;

地图的密钥是帐号,而值是帐户本身。这样您就可以通过其唯一编号访问每个帐户。

(如果你不知道地图是什么,请检查this

这也意味着您需要修改Client构造函数以接受多个帐户,或者添加新方法以将新帐户添加到Client

答案 1 :(得分:1)

正如@ m0skit0建议的那样,使用地图或列表来保存Client类下的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();

答案 2 :(得分: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();
    }

}

您可以在此处添加任意数量的帐户。

答案 3 :(得分:0)

而不是尝试再次重新定义client1和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,你将退出Oliver的账户而不是George。

您可以在地图中维护名称和帐户对象,而不是这样做,您可以随时获取该人的帐户名称。像:

Map<String, Account> nameAccountMap = ..

然后您将相应的帐户添加到其中:

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

现在,如果您希望在Oliver拥有的帐户上运营,您可以通过以下方式进行操作:

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

其他账户持有人的类似操作。

如果您希望将多个帐户与用户关联,您可以维护包含用户名称和帐户列表的地图,例如:

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

答案 4 :(得分: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
}