[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

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);
}
0
gudii9
Asked:
gudii9
  • 3
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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)
0
 
gudii9Author Commented:
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
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
why we need to add additional indexed methods in this case
Ok, in this case the word 'need' is a bit strong. If you look further up it uses the word 'typically'. Putting it another way, what the whole thing is trying to say, is that if you have a property that is a list, normally you provide methods to set/get the list but you also provide methods to directly access items in the list (via the index). There is definitely no NEED to do this but it is just saying that it is convention.

To put it yet another way, there is technically no NEED to provide any getters/setters (you could just make the field all public) but it is the Javabean convention to define private fields, with public getters/setters. And so it is the Javabean convention to also provide the additional indexed methods for list fields/properties. Check out this link, http://docstore.mik.ua/orelly/java-ent/jnut/ch06_02.htm, section 6.2.3 for a reference to what I have just written.
0
 
gudii9Author Commented:
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
0
 
gudii9Author Commented:
That link has no information relating to additional indexed methods. Please advise
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now