Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing two Value Objects in an If statement

Posted on 2013-06-17
3
Medium Priority
?
202 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 36

Accepted Solution

by:
mccarl earned 2000 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 36

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

670 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