Skip to content

Instantly share code, notes, and snippets.

@arnoldmartinez
Last active April 22, 2017 05:26
Show Gist options
  • Save arnoldmartinez/2815b0f246bc44b345e80d2a1bec9340 to your computer and use it in GitHub Desktop.
Save arnoldmartinez/2815b0f246bc44b345e80d2a1bec9340 to your computer and use it in GitHub Desktop.
LEAP-YEAR CALCULATOR
package demo;
public interface Calculator {
boolean calculateLeapYear(int year);
}
package demo;
public class LeapYear implements Calculator {
private static final int MULTIPLE = 4;
private static final int SECULAR = 100;
private static final int NOT_SECULAR = 400;
private static final int REMAINDER = 0;
public boolean calculateLeapYear(int year) {
return isMultiple(year) && (!isSecular(year) || isNotSecular(year));
}
private boolean isMultiple(int year){
int result = year / MULTIPLE;
return year == (MULTIPLE * result) ? true : false;
}
private boolean isSecular(int year) {
return year % SECULAR == REMAINDER;
}
private boolean isNotSecular(int year) {
return year % NOT_SECULAR == REMAINDER;
}
}
package demo;
public class LeapYearCalculatorDemo {
private static final int LEAP_YEAR = 2400;
public static void main(String[]args){
final Calculator date = new LeapYear();
System.out.println(date.calculateLeapYear(LEAP_YEAR) ? showLeapYear() : showNotLeapYear());
}
private static String showLeapYear(){
return "Is leap-year";
}
private static String showNotLeapYear(){
return "Is not leap-year!!!";
}
#TEST
import demo.Calculator;
import demo.LeapYear;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class LeapYearTest {
private static final int LEAP_YEAR = 2000;
@Test
public void isMultipleValidateValue(){
Calculator calculator = new LeapYear();
assertTrue(calculator.calculateLeapYear(LEAP_YEAR));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment