### Code Kata?

Removing smell from dayOfYear method found in here.

It is another day, and I just decide to refactor the following method:
```public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month == 2) {
dayOfMonth += 31;
} else if (month == 3) {
dayOfMonth += 59;
} else if (month == 4) {
dayOfMonth += 90;
} else if (month == 5) {
dayOfMonth += 31 + 28 + 31 + 30;
} else if (month == 6) {
dayOfMonth += 31 + 28 + 31 + 30 + 31;
} else if (month == 7) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30;
} else if (month == 8) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31;
} else if (month == 9) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
} else if (month == 10) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
} else if (month == 11) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
} else if (month == 12) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31;
}
return dayOfMonth;
}```

So this is what I end up with..

Month.java
```public enum Month {

JAN(1, 31, 31),
FEB(2, 28, 29),
MAR(3, 31, 31),
APR(4, 30, 30),
MAY(5, 31, 31),
JUN(6, 30, 30),
JUL(7, 31, 31),
AUG(8, 31, 31),
SEP(9, 30, 30),
OCT(10, 31, 31),
NOV(11, 30, 30),
DEC(12, 31, 31);

private final int monthIndex;
private final int numberOfDaysInNonLeapYear;
private final int numberOfDaysInLeapYear;

Month(final int monthIndex,
final int numberOfDaysInNonLeapYear,
final int numberOfDaysInLeapYear) {
this.monthIndex = monthIndex;
this.numberOfDaysInNonLeapYear = numberOfDaysInNonLeapYear;
this.numberOfDaysInLeapYear = numberOfDaysInLeapYear;
}

public int getNumberOfDaysInNonLeapYear() {
return numberOfDaysInNonLeapYear;
}

public int getNumberOfDaysInLeapYear() {
return numberOfDaysInLeapYear;
}

public int getMonthIndex() {
return monthIndex;
}

static Month getMonthForIndex(final int index) throws MonthIndexNotInRangeException {
final Month[] months = Month.values();
for (final Month month : months) {
if (month.monthIndex == index) {
return month;
}
}

// If we are at this point, the client did not provide an index between 1 - 12!
throw new MonthIndexNotInRangeException();
}
}```

Year.java
```public final class Year {

private final int yearValue;

public Year(final int yearValue) {
this.yearValue = yearValue;
}

public boolean isLeap() {
return (yearValue % 400 == 0) || ((yearValue % 4 == 0) && (yearValue % 100 != 0));
}
}```

YearMonth.java
```public final class YearMonth {

private final Month month;
private final Year year;

public YearMonth(final Year year, final Month month) {
this.year = year;
this.month = month;
}

public int numberOfDays() {
int numberOfDays;

if (year.isLeap()) {
numberOfDays = month.getNumberOfDaysInLeapYear();
} else {
numberOfDays = month.getNumberOfDaysInNonLeapYear();
}

return numberOfDays;
}

public int dayOfYear(final int dayOfMonth) throws MonthIndexNotInRangeException {
int dayOfYear = 0;
int monthIndex = month.getMonthIndex() - 1;

while (monthIndex != 0) {
final Month previousMonth = Month.getMonthForIndex(monthIndex);
final YearMonth yearPreviousMonth = new YearMonth(year, previousMonth);
dayOfYear = dayOfYear + yearPreviousMonth.numberOfDays();
monthIndex--;
}

return dayOfYear + dayOfMonth;
}
}```

TestClass.java
```public class TestClass {
public static void main(String[] args) throws MonthIndexNotInRangeException {
final Month january = Month.getMonthForIndex(1);
final Year year2016 = new Year(2016);
final YearMonth january2016 = new YearMonth(year2016, january);
final int dayOfYearJanuary2016Day1 = january2016.dayOfYear(1);
System.out.println("Day of Year 2016 for January Day 1: " + dayOfYearJanuary2016Day1);

final Month march = Month.MAR;
final Year year2014 = new Year(2014);
final YearMonth march2014 = new YearMonth(year2014, march);
final int dayOfYearMarch2014Day2 = march2014.dayOfYear(2);
System.out.println("Day of Year 2014 for March Day 2: " + dayOfYearMarch2014Day2);
}
}```

The output will be:
```Day of Year 2016 for January Day 1: 1
Day of Year 2014 for March Day 2: 61```

What do you think? And ideas? Please contribute here.