JAVA - 两个对象之间的关系

JAVA - relation between two objects

我对 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
}

Class 客户

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 和 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 = ..

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

private Map<String, Account> accounts;

其中map的key是账号,value是账号本身。这样您就可以通过其唯一编号访问每个帐户。

(如果您不知道什么是地图,请查看 this

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

不要在您的客户端 class 中使用单个 Account,而是使用 Set<Account> 以便建立一对多关系。确保帐户 class 已实施 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
}

正如@m0skit0 所建议的那样,使用映射或列表在客户端 class 下保存帐户对象。

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();

检查此代码:

//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();
    }

}

在这里您可以添加任意数量的帐户。