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