How to use generalized object for 2 different classes?

How to use generalized object for 2 different classes?



I have 2 component classes



OrderDetailsSite1.java



OrderDetailsSite2.java



I have one bean class where I'm storing the Shopping cart details



Now I have to create one common method where I have to pass the Component class Object and compare the values with, bean class data.



Code trial :


public void validateProductDetails(Object obj)

OrderDetailBean bean = (OrderDetailBean) ConfigurationManager.getBundle().getObject("order_details");
OrderDetailsSite1 comp=null;

if (obj instanceof OrderDetailsSite1)

comp = (OrderDetailsSite1) obj;


for (int i = 0; i < bean.getProductName().size(); i++)

assertResult( comp.getProductList().get(i).getProductName().getText(), bean.getProductName().get(i));
assertResult( comp.getProductList().get(i).getProductQuantity().getText(), bean.getProductQuantity().get(i));
assertResult( comp.getProductList().get(i).getProductUnitPrice().getText(), bean.getProductUnitprice().get(i));
assertResult(comp.getProductList().get(i).getProductSubtotal().getText(), bean.getProductSubtotal().get(i));





The code what i have done for component 1 but for component 2 do i need to repeat the same code ? or is there any provision to do the same in efficient way



EDITED



I'm working with selenium and I have created same variable and method name in both class but the locators for both are different. Is is possible to do the same using super class concept ?



OrderDetailsSite1.java


OrderDetailsSite1.java


@FindBy(locator = "magento.ordered.item.name")
private QAFWebElement productName;

@FindBy(locator = "magento.ordered.item.quantity")
private QAFWebElement productQuantity;

@FindBy(locator = "magento.ordered.item.price")
private QAFWebElement productUnitPrice;

@FindBy(locator = "magento.ordered.item.subtotal")
private QAFWebElement productSubtotal;



OrderDetailsSite2.java


OrderDetailsSite2.java


@FindBy(locator = "order.detail.product.name")
private QAFWebElement productName;

@FindBy(locator = "order.detail.product.uprice")
private QAFWebElement productUnitPrice;

@FindBy(locator = "order.detail.product.quantity")
private QAFWebElement productQuantity;

@FindBy(locator = "order.detail.product.subtotal")
private QAFWebElement productSubtotal;



method name in both classes are same.





Unless OrderDetailsSite1 and OrderDetailsSite2 have a common superclass offering access to those properties, there is little you can do but duplicate code.
– Andy Turner
Sep 4 '18 at 6:33



OrderDetailsSite1


OrderDetailsSite2





You could extract your code to a method in a static service/util class if the duplicated code bothers you that much. Other than that I only see the superclass solution as @AndyTurner mentioned.
– T A
Sep 4 '18 at 6:39





@AndyTurner, i have edited the question. please suggest
– NarendraR
Sep 4 '18 at 6:47




2 Answers
2



Make Components interface and implement it component classes


interface Components
...// declare all methods of your components like getProductList() etc
...


public void validateProductDetails(Components comp)

OrderDetailBean bean = (OrderDetailBean) ConfigurationManager.getBundle().getObject("order_details");

for (int i = 0; i < bean.getProductName().size(); i++)

assertResult( comp.getProductList().get(i).getProductName().getText(), bean.getProductName().get(i));
assertResult( comp.getProductList().get(i).getProductQuantity().getText(), bean.getProductQuantity().get(i));
assertResult( comp.getProductList().get(i).getProductUnitPrice().getText(), bean.getProductUnitprice().get(i));
assertResult(comp.getProductList().get(i).getProductSubtotal().getText(), bean.getProductSubtotal().get(i));







You can't check instanceof T. Wouldn't even compile.
– Andy Turner
Sep 4 '18 at 6:28


instanceof T





Now comp is always null, so you'd get a NPE whenever bean.getProductName().size() > 0.
– Andy Turner
Sep 4 '18 at 6:30



comp


bean.getProductName().size() > 0





You can't invoke getProductList() on a T, because it's an unbounded type variable.
– Andy Turner
Sep 4 '18 at 6:32



getProductList()


T





@AndyTurner Thanks for suggestions, Now i think, I fixed all errors
– Tarun
Sep 4 '18 at 6:40





There is no need to use generics if you just use T comp. Components comp works identically.
– Andy Turner
Sep 4 '18 at 6:41


T comp


Components comp



EDIT, from updated question



From your code above, I would use this approach.



Create an interface that will provide common api for your order detail sites.


public interface class OrderDetailsComponent
private QAFWebElement productQuantity;
private QAFWebElement productQuantity;
private QAFWebElement productUnitPrice;
private QAFWebElement productSubtotal;



Both your classes should implement your interface


public class OrderDetailsSite1 implements OrderDetailsComponent
@FindBy(locator = "magento.ordered.item.name")
private QAFWebElement productName;

@FindBy(locator = "magento.ordered.item.quantity")
private QAFWebElement productQuantity;

@FindBy(locator = "magento.ordered.item.price")
private QAFWebElement productUnitPrice;

@FindBy(locator = "magento.ordered.item.subtotal")
private QAFWebElement productSubtotal;


public class OrderDetailsSite2 implements OrderDetailsComponent
@FindBy(locator = "order.detail.product.name")
private QAFWebElement productName;

@FindBy(locator = "order.detail.product.uprice")
private QAFWebElement productUnitPrice;

@FindBy(locator = "order.detail.product.quantity")
private QAFWebElement productQuantity;

@FindBy(locator = "order.detail.product.subtotal")
private QAFWebElement productSubtotal;



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế