Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Bean additional indexed methods

The above four bean classes are pretty straight forward. Look at the Account bean. This
bean defines four properties as shown below:
AccountDetail accountDetail;
Address address;
List creditCards;
String bankName;


package beans;
import java.util.List;
public class Account {
      AccountDetail accountDetail;
      Address address;
      List creditCards;
      String bankName;
      
      public String getBankName() {
            return bankName;
      }
      public void setBankName(String bankName) {
            this.bankName = bankName;
      }
      public AccountDetail getAccountDetail() {
            return accountDetail;
      }
      public void setAccountDetail(AccountDetail accountDetail) {
            this.accountDetail = accountDetail;
      }
      public Address getAddress() {
            return address;
      }
      public void setAddress(Address address) {
            this.address = address;
      }
      public List getCreditCards() {
            return creditCards;
      }
      public void setCreditCards(List creditCards) {
            this.creditCards = creditCards;
      }
      public CreditCard getCreditCards(int pos) {
            return (CreditCard) creditCards.get(pos);
      }
      public void setCreditCards(int pos, CreditCard card) {
            this.creditCards.add(pos, card);
      }
}
As we store multiple creditcards, we defined a List of creditCards as shown
above. Typically, if the parameter is of type List (repeating element), then besides the
following two regular getter and setter methods,
public List getCreditCards() {
return creditCards;
}
public void setCreditCards(List creditCards) {
this.creditCards = creditCards;
}
we also need to define two additional indexed methods with the actual object contained
in the list as shown below:
public CreditCard getCreditCards(int pos) {
return (CreditCard)creditCards.get(pos);
}
public void setCreditCards(int pos, CreditCard card) {
this.creditCards.add(pos,card);
}
Avatar of mccarl
mccarl
Flag of Australia image

That's nice! But do you have a question to ask? (By the way, should indicate in the question when it is related to academic assignments, etc)
Avatar of gudii9

ASKER

I have a question on following statement

>>>
we also need to define two additional indexed methods with the actual object contained
in the list as shown below:
public CreditCard getCreditCards(int pos) {
return (CreditCard)creditCards.get(pos);
}
public void setCreditCards(int pos, CreditCard card) {
this.creditCards.add(pos,card);
}




why we need to add additional indexed methods in this case. Please avise
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

I got confused with part


>>>      public List getCreditCards() {
            return creditCards;
      }
      public void setCreditCards(List creditCards) {
            this.creditCards = creditCards;
      }
      public CreditCard getCreditCards(int pos) {
            return (CreditCard) creditCards.get(pos);
      }
      public void setCreditCards(int pos, CreditCard card) {
            this.creditCards.add(pos, card);
      }



In addition to regular getter and setter methods of CreditCards there are 2 additional indexed methods as well like

   public CreditCard getCreditCards(int pos) {
            return (CreditCard) creditCards.get(pos);
      }
      public void setCreditCards(int pos, CreditCard card) {
            this.creditCards.add(pos, card);
      }



I still not usnerstood importance of indexed additional methods. Is it is only specific to lists or any other colection object. Please advise
Avatar of gudii9

ASKER

That link has no information relating to additional indexed methods. Please advise