简体   繁体   中英

Call for methods of an ArrayList in another class in Java

If I have three classes. One class A , one class Customer where I've put an ArrayList of A in the constructor and one class SavingsAccount where I want to put them together. In class A I have a method which I want to call from SavingsAccount . How do I do that? And to call for both Customer and SavingsAccount ? In Customer there is a variable; SocSecNr , that has to match a Nr in SavingsAccount , to be correct in A , that's why I put an ArrayList of SavingsAccount in Customer . (This is just an example class. I only want to know how to do this call without inheritance)

import java.util.ArrayList;

public class A {
private ArrayList<Customer> customerlist;
private SavingsAccount account;

public A() {
    customerlist = new ArrayList<Customer>();
    account = new SavingsAccount();
}

public boolean deposit(long pNr, int accountId, double amount)
{   
    for(int i = 0; i < customerlist.size(); i++)
    {
        if(customerlist.get(i).getPCode() == pNr)
        {
            account.transaction(amount);
        }        
    }
    return false;        
       
}
public double transaction(){

    if(amount < 0 && balance + amount < 0)
        return -0;
    else
        return balance += amount;
}




   public class Customer {
private long pNr;
private ArrayList<SavingsAccount> accounts;

public Customer(long pCode)
{
    pNr = pCode;
    accounts = new ArrayList<SavingsAccount>();
}

public ArrayList<SavingsAccount> getAccount(){
    return accounts;
}
}



public class SavingsAccount {
private double balance;

public SavingsAccount(){
    accountId = accountCount;
}

public double transaction(double amount){
    if(amount < 0 && balance + amount < 0)
        return -0;
    else
        return balance += amount;

}
}

you can achieve it by two way

1- inheritance

class C extentds A{
 //all the public or protected attribues and methods are availble here 
} 

2- By has a relationship.

class C{

   private A aType;

   aType.methode();
}

In class AI have a method which I want to call from C

You should make an association between A and C via putting a C field into the A . You can pass the reference to it in the constructor or use a setter method to initialize it. Actually, you don't need them in the code for deposit method, but you can initialize them in this method for further references or usage in other methods.

EDIT:

import java.util.ArrayList;

public class A {
  private ArrayList<Customer> customerlist;
  private Customer customer;
  private SavingsAccount account;

  public A() {
      customerlist = new ArrayList<Customer>();
  }

  public boolean deposit(long pNr, int accountId, double amount)
  {   
      for(Customer customer : customerlist) {
          if(customer.getPCode() == pNr) {
            this.customer = customer; //initialize
            List<SavingsAccount> accounts = customer.getAccounts();
            for (SavingsAccount account : accounts) {
              if (account.getAccountId() == accountId){
                this.account = account; //initialize
                account.transaction(amount);
                return true;
              } 
            }
          }        
      }
      return false;            
  }

}

public class Customer {
  private long pNr;
  private ArrayList<SavingsAccount> accounts;

  public long getPCode(){
    return pNr;
  }

  public Customer(long pCode)
  {
    pNr = pCode;
    accounts = new ArrayList<SavingsAccount>();
  }

  public ArrayList<SavingsAccount> getAccounts(){
    return accounts;
  }
}



public class SavingsAccount {
  private double balance;
  private int accountId;

  public int getAccountId(){
    return accountId;
  }

  public SavingsAccount(int accountId){
    this.accountId = accountId;
  }

  public double transaction(double amount){
    if(amount < 0 && balance + amount < 0)
        return -0;
    else
        return balance += amount;

  }
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM