0

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?

10
  • Why would this method be in this class ? Seems more logical to have in in Customer, since it is the only class who knows all the Accounts related to a specific Customer. Commented Sep 20, 2014 at 22:34
  • @Dici ok, so if I put it in class Account, how can I access the right customer and account? How can the code look like? Commented Sep 20, 2014 at 22:39
  • Also, it is the bank class that stores all the customers. Commented Sep 20, 2014 at 22:41
  • 1
    This is how I would do it - Iterate over Bank customers to find if there is a customer whose id==2. If found, then iterate over all the accounts of that customer to find an account with no. == 1. If found, do somthing with that account like add or remove money. Commented Sep 20, 2014 at 22:41
  • 1
    It would be better to use a Map<String,Customer> in your Bank (unless the ids are consecutive integers). This way, the lookup would be more efficient. Commented Sep 20, 2014 at 22:42

1 Answer 1

2

What you could do is have a method for that in the Bank class:

public class Bank {
  // Your stuff
  // new method:
  public boolean transfer(Account accountFrom, double amount, String nameTo, int account) {
     //check if the balance can be deposit from the account
     if(amount <= accountFrom.getBalance()) {
        //Check if the person exists in the bank
        String name = nameTo.split(" "); // name[0] is the first name, name[1] last name

        boolean success = false;
        for(Customer c: customers) {
           if(c.getFirstName().equalsIgnoreCase(name[0]) &&
                     c.getLastName().equalsIgnoreCase(name[1]) {
              for(Account a : c.getAccounts()) {
                 if(a.getAccountId() == account) {
                    // Add it to the account
                    a.deposit(amount);
                    success = true;
                    break;
                 }
              }
              break;
           }
        }

        // Deposit it from the account (That class should only keep track of money, so it 
        // only takes an argument to deposit or withdraw a value, the rest is done by the bank
        // Only do this if money has been dsposited at the target account
        if(success){
          accountFrom.withdraw(amount);
          return true;
        }

     }
     return false;
  }
}

For this to happen you have to structurely change your setup.

Have the accounts only manage money, those accounts get added to a customer. The customer is the person who communicates between the bank and himself. And finally the bank communicates with customers.

I hope this will help you in the direction

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

1 Comment

thank you. I think the transfer method is too complicated to begin with. Can you show me a simplier method, like deposit or withdraw? I don't understand your explanation above, what do you mean by "those accounts get added to a customer"?.

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.