vendredi 17 janvier 2020

Is there a way to create an object in which the objects class is defined by another variable?

I have two classes that have some identical methods, the method below should access the methods of one of the two classes based on which checkbox is ticked, However I am having issues passing the objects created within the IF statements into the main body of the method. Is there way to achieve this without copying the main body of the method into both areas? Here is the code:

public void populateSupplyChainTextFields() {

    if (jDeliveryCheckBox.isSelected() == true) {
        DeliveryCompany supplyChainMember = new DeliveryCompany();
        supplyChainMember = theDeliveryCompanyList.Find(jSupplyChainSearchBox.getSelectedItem().toString());
    } else {
        Supplier supplyChainMember = new Supplier();
        supplyChainMember = theSupplierList.Find(jSupplyChainSearchBox.getSelectedItem().toString());

    }

    jCategoryTextField.setText(supplyChainMember.getCategory());
    jPriceTextField.setText(String.valueOf(supplyChainMember.getPrice()));
    jUnitCostTextField.setText(String.valueOf(supplyChainMember.getUnitCost()));

My apologies if this is a novice question, I am fairly new to Java, any help with this is appreciated.

Aucun commentaire:

Enregistrer un commentaire