Solved

Accessing two Value Objects in an If statement

Posted on 2013-06-17
3
192 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
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now