Solved

Accessing two Value Objects in an If statement

Posted on 2013-06-17
3
197 Views
Last Modified: 2013-06-24
In one of the Value Objects,  I have
ItemsVO

ItemID
Name

ItemDetailsVO

ItemID
Code
Quantity

I would like to extract information from ItemsVO only when the Name = Test.
I would like to extract information from ItemDetailsVO where ItemID = ItemsVO ItemID
How do I instantiate the ItemDetailsVO object to use in the If statement?

Given the following example, how would I write the statement

So is this how it should look?  

public ItemVO getItems (List<ItemsVO > XItems, String
                                            test, List<ItemDetails> details)
      {
          List<ItemsVO> resultList = new ArrayList<ItemVO>();
           
          for (ItemsVO vo : XItems)
          {

            HOW WOULD I CHANGE if (vo.getName == test  TO
                if ( vo.getname == test and vo.getItemID =  details.getItemID

             if (vo.getName == test)
                 {
                  resultList.add(vo);
}             }  
 
            return resultList;
      }
0
Comment
Question by:cookiejar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39255087
Whilst what you are after could be done with just a plain "List" of ItemDetailsVO objects, what you really should use is a "Map" instead. The Map collection allows you to associate a "key" and a "value" and allow you to do a "lookup" of the key and return the value, which essentially what you need to do here. You want to do a lookup of a Map based on the "ItemID" key and return the ItemDetailsVO object value that corresponds to that key.

So ideally you would change whatever code is calling the above method to create a Map of ItemDetailsVO objects and pass that it (that way, if there is other places in the code that need to do similar lookups, the map is already created). If you can't change the other code, or if this is the ONLY place where such a lookup happens, you can create the Map based on the List at the beginning of the method like so...
// This code is assuming that .getItemID() returns a String, if not change the type below...
Map<String, ItemDetailsVO> detailsMap = new HashMap<String, ItemDetailsVO>();

for (ItemDetailsVO detail : details) {
    detailsMap.put(detail.getItemId(), detail);
}

Open in new window

Once you have that at the beginning of your method (ie. before and _outside_ the for loop that you have already), you could do this...
if (vo.getName.equals(test)) {
    ItemDetailsVO detail = detailsMap.get(vo.getItemID());

    // Do something with the "detail" object, such as detail.getCode(), etc
    
    resultList.add(vo);
}

Open in new window

Note, that I am leaving it up to you to work out whatever you are going to do with the info in that "detail" object, because it is not clear, ie. are you returning either a list of ItemVO's or just one (again that is not clear in your code as that wouldn't compile). Also note from the above code how you should do String comparison. You can't use ==, you must use the equals() method.
0
 

Author Comment

by:cookiejar
ID: 39264743
ItemID is a integer

Just for clarity, where is the ItemID in ItemVO mapped to the ItemID in the ItemDetailsVO to get the corresponding name from ItemVO.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39264808
ItemID is a integer
Ok, so just change the reference to String (in line 2 of the first snippet that I posted) to Integer. Everything else should stay the same.


Just for clarity, where is the ItemID in ItemVO mapped to the ItemID in the ItemDetailsVO to get the corresponding name from ItemVO.
Maybe I don't fully understand what you are saying here, but let me see if I get it.

In the code in your original question above, you have a "for" loop that iterates through every ItemVO object in the XItems list. So, inside this loop, for each ItemVO object you can get it's ItemID, with vo.getItemID(), and you can get it's name, with vo.getName(). And by building that "detailsMap" object, and executing line 2 (of the second code snippet) you now have a "detail" object that corresponds to the current ItemVO being iterated.

To be 100% clear, if I expand that second code snippet above, hopefully you can see what is happening...
if (vo.getName.equals(test)) {
    ItemDetailsVO detail = detailsMap.get(vo.getItemID());

    // Do something with the "detail" object, such as detail.getCode(), etc
    String itemName = vo.getName();
    int itemID = vo.getItemID();      // detail.getItemID() would do exactly the same as this line
    String itemCode = detail.getCode();
    int itemQuantity = detail.getQuantity();

    resultList.add(vo);
}

Open in new window

Those extra lines that I have added (lines 5 - 8) show that you can access the "name" of the item (from the vo object), the "ID" of the item (from either vo or detail) and the associated "Code" and "Quantity" for the item (from the detail object).

And this code is inside that "for" loop that you have in your original code, so you get these details for each item in the XItems list (or at least each item where the name = "test" since that is what your "if" statement is selecting)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
import as existing maven project 3 78
Where to place postgres JDBC driver jar on tomcat 8 71
Selenium findElement(By.classname  identifier 15 38
Java regex 5 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question