package date;

public class Date {
    private static final int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    private final int month;   // month (between 1 and 12)
    private final int day;     // day   (between 1 and DAYS[month]
    private final int year;    // year
    
    /**
     * do bounds-checking to ensure object represents a valid date
     * @param m
     * @param d
     * @param y
     */
    public Date(int m, int d, int y) {
        if (!isValid(m, d, y)) throw new RuntimeException("Invalid date");
        month = m;
        day   = d;
        year  = y;
    }
	
	/**
	 * Read in the month, day, and year and print out what
	 * day of the week it falls on according to the Gregorian calendar.
	 * @return the day of week, 0 for Sunday, 1 for Monday, and so forth
	 */
	public int dayOfWeekUS(){
        int y0 = year - (14 - month) / 12;
        int x = y0 + y0/4 - y0/100 + y0/400;
        int m0 = month + 12 * ((14 - month) / 12) - 2;
        int d0 = (day + x + (31*m0)/12) % 7;
        return d0;
	}

	/**
	 * Spring season
	 * @return true if the given day and month fall between March 20 (inclusive)
	 * and June 20 (inclusive).
	 */
	public boolean isSpring(){
        return (month == 3 && day >= 20 && day <= 31)
            || (month == 4 && day >=  1 && day <= 30)
            || (month == 5 && day >=  1 && day <= 31)
            || (month == 6 && day >=  1 && day <= 20);
	}

    // is the given date valid?
    private static boolean isValid(int m, int d, int y) {
        if (m < 1 || m > 12)      return false;
        if (d < 1 || d > DAYS[m]) return false;
        if (m == 2 && d == 29 && !isLeapYear(y)) return false;
        return true;
    }

    // is y a leap year?
    private static boolean isLeapYear(int y) {
        if (y % 400 == 0) return true;
        if (y % 100 == 0) return false;
        return (y % 4 == 0);
    }

    // return the next Date
    public Date next() {
        if (isValid(month, day + 1, year))    return new Date(month, day + 1, year);
        else if (isValid(month + 1, 1, year)) return new Date(month + 1, 1, year);
        else                                  return new Date(1, 1, year + 1);
    }


    // is this Date after b?
    public boolean isAfter(Date b) {
        return compareTo(b) > 0;
    }

    // is this Date a before b?
    public boolean isBefore(Date b) {
        return compareTo(b) < 0;
    }

    // comparison function between two dates
    public int compareTo(Date b) {
        if  (year  != b.year)  return year  - b.year;
        if  (month != b.month) return month - b.month;
        return day - b.day;
    }

    // return a string representation of this date
    public String toString() {
        return month + "/" + day + "/" + year;
    }
    
    // sample client for testing
    public static void main(String[] args) {
        Date today = new Date(2, 25, 2004);
        System.out.println(today);
        for (int i = 0; i < 10; i++) {
            today = today.next();
            System.out.println(today);
        }

        System.out.println(today.isAfter(today.next()));
        System.out.println(today.isAfter(today));
        System.out.println(today.next().isAfter(today));
    }
}
