In main I do this: First I create a new customer with its first name, lastname and number. Then I create two savingsAccounts, with its amount, id and interest rate. I then add the two savingsAccounts to the new customer. Finally I add the new customer to the bank.
Customer newCustomer = new Customer(firstName, lastName, pnumber);
SavingsAccount savingsAccount1 = new SavingsAccount(400, "1", 4); //400$ into account no.1, with interest 4%
SavingsAccount savingsAccount2 = new SavingsAccount(300, "2", 3);
newCustomer.addAccount(savingsAccount1);
newCustomer.addAccount(savingsAccount2);
bank.addCustomer(newCustomer);
Here is class Bank:
public class Bank {
String bankName;
private ArrayList<Customer> customers = new ArrayList<Customer>();
Bank(String bankName) {
this.bankName = bankName;
}
public void addCustomer(Customer newCustomer) {
customers.add(newCustomer);
}
}
Here is class Customer:
public class Customer {
private String firstName;
private String lastName;
private String number;
private ArrayList<Account> accounts;
Customer(String firstName, String lastName, String number) {
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
this.accounts = new ArrayList<Account>();
}
public void addAccount(SavingsAccount account) {
accounts.add(account);
}
public void addAccount(CreditAccount account) {
accounts.add(account);
}
public ArrayList<Account> getAccounts() {
return accounts;
}
}
Here is class SavingsAccount (that inherits class Account):
public class SavingsAccount extends Account {
public SavingsAccount() {
super();
}
public SavingsAccount(double bal, String id, double inte) {
super(bal, id, inte);
}
@Override
public void deposit(String number, String id, double amount) {
}
@Override
public void withdraw(String number, String id, double amount) {
}
@Override
public void transfer(String number, String id, double amount) {
}
@Override
public double getBalance() {
}
@Override
public String getAccountId() {
return accountId;
}
@Override
public double getInterest(){
return interest;
}
}
My problem is: How can I write code in class SavingsAccount to deposit, withdraw, transfer money for a certain customer, for a certain account? Let's say I want to deposit 500 to customer no.2 on his account no.1.
That should be something like savingsAccount.deposit("2", "1", 500);
I just can't figure out how to access customer number 2, and his account number 1. Can anyone help me please?
Customer, since it is the only class who knows all theAccounts related to a specificCustomer.Map<String,Customer>in yourBank(unless the ids are consecutive integers). This way, the lookup would be more efficient.