Thursday, June 16, 2011

Effective Enums - 1

I'm planning to start a small series of one of my favorite inclusions in Java 5 - Enums. Joshua Bloch in his book Effective Java has dedicated an entire chapter on the same. This series will also be loosely based on the same.

In short, I want to have a Loan constant that can help me get the interest rate, maximum repay period and to check if one is eligible provided you enter the age and credit score. Here is my implementation in Java 5 for the same using enum.


package org.sxvarg2.enums;
public enum LoanEnum {
HOME(7, 20) {
@Override
public boolean isEligible(int age, int creditScore) {
return age < 30 && creditScore > 700;
}
},
CAR(5, 5) {
@Override
public boolean isEligible(int age, int creditScore) {
return age < 50 && creditScore > 700;
}
},
EDUCATION(2, 20) {
@Override
public boolean isEligible(int age, int creditScore) {
return age < 25 && creditScore > 630;
}
},
STARTUP(9, 5) {
@Override
public boolean isEligible(int age, int creditScore) {
return age < 40 && creditScore > 770;
}
};
private final int interestPercentage;
private final int maxRepayPeriod;
private LoanEnum(final int interestPercentage, final intmaxRepayPeriod) {
this.interestPercentage = interestPercentage;
this.maxRepayPeriod = maxRepayPeriod;
}
public int getInterestPercentage() {
return interestPercentage;
}
public int getMaxRepayPeriod() {
return maxRepayPeriod;
}
public abstract boolean isEligible(int age, int creditScore);
}
view raw LoanEnum.java hosted with ❤ by GitHub
Now, there are some constraints for a developer on the environment he works. Lets say if you are forced to work on Java 1.4, how do we implement this feature.

Lets create a new interface for Enums


package org.sxvarg2.enums;
public interface Enumerable{
Object[] values();
}
view raw Enumerable.java hosted with ❤ by GitHub

Lets now implement our Loan enum here:


package org.sxvarg2.enums;
public abstract class LoanType implements Enumerable {
public static final LoanType HOME = new LoanType(7, 20) {
public boolean isEligible(int age, int creditScore) {
return age < 30 && creditScore > 700;
}
};
public static final LoanType CAR = new LoanType(5, 5) {
public boolean isEligible(int age, int creditScore) {
return age < 50 && creditScore > 700;
}
};
public static final LoanType EDUCATION = new LoanType(2, 20) {
public boolean isEligible(int age, int creditScore) {
return age < 25 && creditScore > 630;
}
};
public static final LoanType STARTUP = new LoanType(9, 5) {
public boolean isEligible(int age, int creditScore) {
return age < 40 && creditScore > 770;
}
};
private static final Object[] registeredTypes = new Object[] {HOME, CAR, EDUCATION, STARTUP };
private final int interestPercentage;
private final int maxRepayPeriod;
public int getInterestPercentage() {
return interestPercentage;
}
public int getMaxRepayPeriod() {
return maxRepayPeriod;
}
private LoanType(final int interestPercentage, final intmaxRepayPeriod) {
this.interestPercentage = interestPercentage;
this.maxRepayPeriod = maxRepayPeriod;
}
public final Object[] values() {
return registeredTypes;
}
public abstract boolean isEligible(int age, int creditScore);
}
view raw LoanType.java hosted with ❤ by GitHub
Phew! Its done.

No comments: