2

I'm pretty new to OOP and Java and have a question that may be trivial but I couldn't find the answer on the web.

I'm doing the standard bank account program in Java- a program where there are customers, each customer has bank accounts (one customer may have more than one bank account!) and I can deposit or withdraw money. Each bank account has a unique number (even if someone has multiple bank account, each bank account has its unique number)

My code compiles and the operations deposit and withdraw are working correctly.

The problem is the following- I cannot attribute more than one bank account to a customer, in my program a client can have exactly one bank and no more than one bank account.

I have 3 classes - Account, Client, BankMain. You can see them below

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 Client

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"
        );
    }
}

And 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
         */
    }
}

Do you know how can I fix the problem? What should I do so that to have multiple bank accounts associated to the same client?

Thanks in advance George

0

5 Answers 5

1

Checkout this code :

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

}

Here you can add accounts as many as you want.

Sign up to request clarification or add additional context in comments.

3 Comments

Here is the output : Account details : Account details for Account '1' : Account Number : 11223A Balance :1000 Account details for Account '2' : Account Number : 11111A Balance :10000
Ok, thank you, it works correctly but I can't figure out why do we need to use HashSet? Why it wouldn't work only with Set?
Set is better option. It doesn't have to be HashSet. The difference between the two is Set is an interface and whereas HashSet is concrete class. You cannot create instance of an interface.
1

You can have multiple accounts for one client changing the data type in the Client class. For example:

private Map<String, Account> accounts;

Where the key of the map is the account number, and the value is the account itself. This way you can access each account by its unique number.

(If you don't know what a map is, check this)

This also means you need to either modify the Client constructor to accept multiple accounts or add a new method to add a new account to a Client.

3 Comments

This is another interesting approach to the problem.
Is there a more simple way to associate one customer with many bank accounts? Is it possible for example to make an array of Accounts in the Client class? If yes, how should I do it? Thank you in advance, George
A map is a simple data structure. In fact you can think about it like an array: each index (key) has a value. It is possible to have an array, but in Java arrays are rarely used because there's usually a better data structure for your needs. If you need to retreive these accounts later, how would you do with an array? You would have to go through all the array until you find the account you want. Using a map, you will simply ask the map for the value with the associated key (in this case the account number, which is a suitable key because it is unique across all accounts).
1

As @m0skit0 suggested, use a map or list to hold Account object(s) under Client 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());
        }        
    }
}

And to use it in your 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();

1 Comment

I would rather use a Set, to have data integrity check and i don't think order of accounts makes any sense. Just my opinion.
0

Instead of trying to redefine client1 and 2 again like:

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

redefine those object as:

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

But doing so, you could operate on the same account i.e. if you now do client1.withdraw, you would withdraw from Oliver's account and not George.

Instead of doing so, you could maintain the name and account object in a map and given the name you can always fetch the account for that person. Like:

Map<String, Account> nameAccountMap = ..

And then you add corresponding accounts to it like:

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

Now if you wish to operate on account owned by Oliver, you could do so by:

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

And similar operations on other account holders.

If you wish to associate multiple accounts with a user, you could maintain map with name and list of accounts held by a user like:

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

4 Comments

I doubt it that's the answer OP is looking for.
Question is how to have multiple accounts for one client.
There are multiple things in question. 1. Client client1 = new Client("George",account1); Client client1 = new Client("Oliver", account2); but got compilation error as mentioned in the code by OP. Do you know how can I fix the problem? 2. >> What should I do so that to have multiple bank accounts associated to the same client? So its answer of both.
Yeah that's true but i was in mid of editing where i saw your's and sid's comment ;)
0

Instead of having a single Account in your Client class, have a Set<Account> so as to have one to many relationship. Ensure that the Account class has equals and hashcode implemented.

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
}

2 Comments

Is there a more simple way to associate one customer with many bank accounts? Is it possible for example to make an array of Accounts in the Client class? If yes, how should I do it? Thank you in advance
You can have an Array, but in my view, a Set would be better suited. But yes, array of Account objects is possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.