星座的枚举类
ConstellationUtil .java
public class ConstellationUtil {
public enum Constellation {
Capricorn(1, "摩羯座"), Aquarius(2, "水瓶座"), Pisces(3, "双鱼座"), Aries(4, "白羊座"), Taurus(5, "金牛座"), Gemini(6, "双子座"), Cancer(
7, "巨蟹座"), Leo(8, "狮子座"), Virgo(9, "处女座"), Libra(10, "天秤座"), Scorpio(11, "天蝎座"), Sagittarius(12, "射手座");
private int code;
private String chineseName;
private Constellation(int code, String chineseName) {
this.code = code;
this.chineseName = chineseName;
}
public int getCode() {
return this.code;
}
public String getChineseName() {
return this.chineseName;
}
}
public static final Constellation[] constellationArr = { Constellation.Aquarius, Constellation.Pisces,
Constellation.Aries, Constellation.Taurus, Constellation.Gemini, Constellation.Cancer, Constellation.Leo,
Constellation.Virgo, Constellation.Libra, Constellation.Scorpio, Constellation.Sagittarius,
Constellation.Capricorn };
public static final int[] constellationEdgeDay = { 21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22 };
public static int calculateConstellation(String birthday) {
if (birthday == null || birthday.trim().length() == 0)
throw new IllegalArgumentException("the birthday can not be null");
String[] birthdayElements = birthday.split("-");
if (birthdayElements.length != 3)
throw new IllegalArgumentException("the birthday form is not invalid");
int month = Integer.parseInt(birthdayElements[1]);
int day = Integer.parseInt(birthdayElements[2]);
if (month == 0 || day == 0 || month > 12)
return 0;
month = day < constellationEdgeDay[month - 1] ? month - 1 : month;
return month > 0 ? constellationArr[month - 1].getCode() : constellationArr[11].getCode();
}
}
测试ConstellationUtil 单元测试
public class TestConstellationUtil extends BaseTestCase {
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testCalculateConstellation() {
assertEquals(Constellation.Capricorn.getCode(), ConstellationUtil.calculateConstellation("1986-12-22"));
assertEquals(Constellation.Capricorn.getCode(), ConstellationUtil.calculateConstellation("1986-12-25"));
assertEquals(Constellation.Capricorn.getCode(), ConstellationUtil.calculateConstellation("1986-01-20"));
assertEquals(Constellation.Aquarius.getCode(), ConstellationUtil.calculateConstellation("1986-01-21"));
assertEquals(Constellation.Aquarius.getCode(), ConstellationUtil.calculateConstellation("1986-02-18"));
assertEquals(Constellation.Aquarius.getCode(), ConstellationUtil.calculateConstellation("1986-02-19"));
assertEquals(Constellation.Pisces.getCode(), ConstellationUtil.calculateConstellation("1986-02-20"));
assertEquals(Constellation.Pisces.getCode(), ConstellationUtil.calculateConstellation("1986-02-26"));
assertEquals(Constellation.Pisces.getCode(), ConstellationUtil.calculateConstellation("1986-03-20"));
assertEquals(Constellation.Aries.getCode(), ConstellationUtil.calculateConstellation("1986-03-21"));
assertEquals(Constellation.Aries.getCode(), ConstellationUtil.calculateConstellation("1986-03-25"));
assertEquals(Constellation.Aries.getCode(), ConstellationUtil.calculateConstellation("1986-04-20"));
assertEquals(Constellation.Taurus.getCode(), ConstellationUtil.calculateConstellation("1986-04-21"));
assertEquals(Constellation.Taurus.getCode(), ConstellationUtil.calculateConstellation("1986-05-10"));
assertEquals(Constellation.Taurus.getCode(), ConstellationUtil.calculateConstellation("1986-05-21"));
assertEquals(Constellation.Gemini.getCode(), ConstellationUtil.calculateConstellation("1986-05-22"));
assertEquals(Constellation.Gemini.getCode(), ConstellationUtil.calculateConstellation("1986-05-25"));
assertEquals(Constellation.Gemini.getCode(), ConstellationUtil.calculateConstellation("1986-06-21"));
assertEquals(Constellation.Cancer.getCode(), ConstellationUtil.calculateConstellation("1986-06-22"));
assertEquals(Constellation.Cancer.getCode(), ConstellationUtil.calculateConstellation("1986-06-28"));
assertEquals(Constellation.Cancer.getCode(), ConstellationUtil.calculateConstellation("1986-07-22"));
assertEquals(Constellation.Leo.getCode(), ConstellationUtil.calculateConstellation("1986-07-23"));
assertEquals(Constellation.Leo.getCode(), ConstellationUtil.calculateConstellation("1986-07-30"));
assertEquals(Constellation.Leo.getCode(), ConstellationUtil.calculateConstellation("1986-08-23"));
assertEquals(Constellation.Virgo.getCode(), ConstellationUtil.calculateConstellation("1986-08-24"));
assertEquals(Constellation.Virgo.getCode(), ConstellationUtil.calculateConstellation("1986-09-21"));
assertEquals(Constellation.Virgo.getCode(), ConstellationUtil.calculateConstellation("1986-09-23"));
assertEquals(Constellation.Libra.getCode(), ConstellationUtil.calculateConstellation("1986-09-24"));
assertEquals(Constellation.Libra.getCode(), ConstellationUtil.calculateConstellation("1986-10-21"));
assertEquals(Constellation.Libra.getCode(), ConstellationUtil.calculateConstellation("1986-10-23"));
assertEquals(Constellation.Scorpio.getCode(), ConstellationUtil.calculateConstellation("1986-10-24"));
assertEquals(Constellation.Scorpio.getCode(), ConstellationUtil.calculateConstellation("1986-11-21"));
assertEquals(Constellation.Scorpio.getCode(), ConstellationUtil.calculateConstellation("1986-11-22"));
assertEquals(Constellation.Sagittarius.getCode(), ConstellationUtil.calculateConstellation("1986-11-23"));
assertEquals(Constellation.Sagittarius.getCode(), ConstellationUtil.calculateConstellation("1986-11-28"));
assertEquals(Constellation.Sagittarius.getCode(), ConstellationUtil.calculateConstellation("1986-12-21"));
}
}