dimanche 2 septembre 2018

How can conditional code be improved ? Can inner class or lambada be used in this scenario?

I am writing an application which has lot of checks , most of the logic is based on condition something like if this and this condition is met then this else something else .,So i am trying to find an elegant way to write code , how can make such code comply with SOLID principles. I cannot give the code of my application here hence i will try to simulate problem with simpler example. Below is the code

class Person {
    public String gender;
    public int age;
    public int birthYear;
    public Benefits b;

    public Benefits getB() {
        return b;
    }

    public void setB(Benefits b) {
        this.b = b;
    }
}

class Student extends Person {
    public boolean isEngineerStudent;
    public boolean isMedicalStudent;
}

class Employed extends Person {
    public boolean privateSector;
    public boolean governmentSector;
    public int baseIncome;
}

class Benefits {
    public boolean isInsuraceAvailabe;
    public boolean isStipendAvailable;
    public boolean isPensionAvaiable;
    public boolean isHealthCheckupAvailable;
    public boolean isTransportAvaiable;
    public boolean isLabFacilityProvided;
    public boolean isComputerFacilityProvided;

}

public class EnableBenefits {

    public void enbableBenefits(Person p) {
        Benefits b = new Benefits();
        p.setB(b);
        if (p instanceof Student) {
            Student s = (Student) p;
            if (s.isEngineerStudent && s.age > 22) {
                s.b.isStipendAvailable = true;
                s.b.isComputerFacilityProvided = true;
            }
            if (s.isMedicalStudent && s.age > 20) {
                s.b.isStipendAvailable = true;
                s.b.isLabFacilityProvided = true;
            }
        }

        if (p instanceof Employed) {
            Employed e = (Employed) p;
            if (e.governmentSector == true && e.age > 40 && e.gender.equalsIgnoreCase("F") && e.birthYear <= 1960) {
                e.b.isPensionAvaiable = true;
                e.b.isInsuraceAvailabe = true;
                e.b.isTransportAvaiable = true;
            }
            if (e.governmentSector == true && e.age > 40 && e.gender.equalsIgnoreCase("M") && e.birthYear <= 1960
                    && e.baseIncome < 20000) {
                e.b.isPensionAvaiable = true;
                e.b.isInsuraceAvailabe = true;
            }
            if (e.privateSector == true & e.gender.equalsIgnoreCase("M") && e.birthYear <= 1960) {
                e.b.isInsuraceAvailabe = true;
                e.b.isTransportAvaiable = true;
            }
        }
        p.b.isHealthCheckupAvailable = true;
    }

}

I have purposefully made the access modifiers of instance variables as public so that it does not have too much code so spare me for that.

Though the code does not have too much condition and logic . Just imagine the code for student and employee logic are in hundreds of line

The question is below

1) How can i write huge if condition in better way ? can i use Lamba here ? of course the if condition is not too big but in my real application i can easily have 8 conditions.

if (e.governmentSector == true && e.age > 40 && e.gender.equalsIgnoreCase("M") && e.birthYear <= 1960
                    && e.baseIncome < 20000) {
                e.b.isPensionAvaiable = true;
                e.b.isInsuraceAvailabe = true;
            }

2) Is it better to write below code in Inner Class

if (p instanceof Student) {
            Student s = (Student) p;
            if (s.isEngineerStudent && s.age > 22) {
                s.b.isStipendAvailable = true;
                s.b.isComputerFacilityProvided = true;
            }
            if (s.isMedicalStudent && s.age > 20) {
                s.b.isStipendAvailable = true;
                s.b.isLabFacilityProvided = true;
            }
        }

3) Is there any better way i can write this code

Aucun commentaire:

Enregistrer un commentaire