jeudi 19 janvier 2017

Need design suggestions for nested conditions

I need to write the logic with many conditions(up to 30 conditions) in one set of rule with many if else conditions and it could end in between or after all the conditions.

Here is the sample code I have tried with some possible scenario. This gives me result but doesn't look good and any minor miss in one condition would take forever to track.

What I have tried so far is, Take out common conditions and refactored to some methods. Tried creating interface with conditions and various set would implement it.

If you have any suggestion to design this, would help me. Not looking for detailed solution but even a hint would be great.

private Boolean RunCondition(Input input) {
    Boolean ret=false;
    //First if
    if(input.a.equals("v1")){
        //Somelogic1();
        //Second if
        if(input.b.equals("v2"))
            //Third if
            if(input.c >1)
                //Fourth if
                //Somelogic2();
                //Go fetch key Z1 from database and see if d matches.
                if(input.d.equals("Z1"))                        
                        System.out.println("Passed 1");
                    // Fourth Else
                    else{
                        System.out.println("Failed at fourth");
                    }

            //Third Else
            else{
                if(input.aa.equals("v2"))
                    System.out.println("Failed at third");
                }
        //Second Else
        else{
            if(input.bb.equals("v2"))
                System.out.println("Failed at second");
            }
    }
    //First Else
    else{
        if(input.cc.equals("v2"))
            System.out.println("Failed aat first");
        }

    return ret;
}

public class Input {
    String a;
    String b;
    int c;
    String d;
    String e;       
    String aa;
    String bb;
    String cc;
    String dd;
    String ee;  

}

Aucun commentaire:

Enregistrer un commentaire