/* Date.java */

import java.io.*;
import java.util.*;
import java.lang.Integer;

class Date {

  /* Put your private data fields here. */
   private int month;
   private int day;
   private int year;
  /** Constructs a date with the given month, day and year.   If the date is
   *  not valid, the entire program will halt with an error message.
   *  @param month is a month, numbered in the range 1...12.
   *  @param day is between 1 and the number of days in the given month.
   *  @param year is the year in question, with no digits omitted.
   */
  public Date(int month, int day, int year)
  {
	 if(isValidDate(month,day,year)){
		 this.day=day;
		 this.month=month;
		 this.year=year;
	 }else{
		 System.out.println("False in construction");
		 //exception
	 }
	
  }

  /** Constructs a Date object corresponding to the given string.
   *  @param s should be a string of the form "month/day/year" where month must
   *  be one or two digits, day must be one or two digits, and year must be
   *  between 1 and 4 digits.  If s does not match these requirements or is not
   *  a valid date, the program halts with an error message.
   */
  public Date (String s) {
	  StringTokenizer st = new StringTokenizer(s,"/");
          String month= st.nextToken("/");
	  String day= st.nextToken("/");
	  String year= st.nextToken();
          Integer Month = new Integer(month);
	  int m=Month.intValue();
	  Integer Day= new Integer(day);
	  int d=Day.intValue();
	  Integer Year= new Integer(year);
	  int Y=Year.intValue();
	  if(isValidDate(m,d,Y)){
		 this.day=d;
		 this.month=m;
		 this.year=Y;
	 }else{
		 System.out.println("False in construction");
		 //exception
	 }
  }

  /** Checks whether the given year is a leap year.
   *  @returns true if and only if the input year is a leap year.
   */
  public static boolean isLeapYear(int year) {
	  boolean leap=false;
          int Remainder_Four=year%4;
	  int Remainder_Hundered=year%100;
	  int Remainder_Fourhundered=year%400;
	  if(Remainder_Four==0){
		  if(Remainder_Hundered==0){
			  if(Remainder_Fourhundered==0){
		           leap=true;
		          }else{ 
			     leap=false;}
		  }else{ 
		  leap=true;
		  }	  
	  }else{ 
	  leap=false;
	  }
	  
    return leap;                        // replace this line with your solution
  }

  /** Returns the number of days in a given month.
   *  @param month is a month, numbered in the range 1...12.
   *  @param year is the year in question, with no digits omitted.
   *  @return the number of days in the given month.
   */
  public static int daysInMonth(int month, int year) {
	  int Return_No_Of_Days=0;
	  if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
	  Return_No_Of_Days=31;
	  }
	  if(month==4||month==6||month==9||month==11){
		  Return_No_Of_Days=30;
	  }
	  if(month==2){
	  if(isLeapYear(year)){
			  Return_No_Of_Days=29;
	  }else{
	  Return_No_Of_Days=28;
	  }
	  }
		  
    return (Return_No_Of_Days);                           // replace this line with your solution
  }

  /** Checks whether the given date is valid.
   *  @return true if and only if month/day/year constitute a valid date.
   */
  public static boolean isValidDate(int month, int day, int year) {
	  boolean isValid= false;
	  if((0< month)&&(month < 13)){ 
		  if((0< day) && (day <= daysInMonth(month,year)))
		  {
			  isValid=true;
		  }else{
			  isValid=false;
		  }
	  } else {
		  isValid=false;
	  }
	  
	  return isValid;                        // replace this line with your solution
    // there is no check for year this can be a potential bug -Krishna
  }

  /** Returns a string representation of a date in the form month/day/year.
   *  The month, day, and year are printed in full as integers; for example,
   *  12/7/1998 or 3/21/407.
   *  @return a String representation of the date.
   */
  public String toString() {
	  String constructedDate=(this.month+ "/" + this.day + "/" + this.year );
    return constructedDate;                     // replace this line with your solution
  }

 

  /** Returns the number of this Date in the year.
   *  @return a number n in the range 1...366, inclusive, such that this Date
   *  is the nth day of its year.  (366 is only used for December 31 in a leap
   *  year.)
   */
  public int dayInYear() {
	  int nth_Day=0;
	  if(this.month==1){
		  nth_Day = this.day;
	  }
	  if(this.month==2){
		   nth_Day=31+this.day;
	  }
	  if(this.month==3){
		  if(isLeapYear(this.year)){
		  nth_Day=60+this.day;
		  } else{
		  nth_Day=59+this.day;
		  }               
	  }
	  if(this.month==4){
		  if(isLeapYear(this.year)){
		  nth_Day=91+this.day;
		  } else{
		  nth_Day=90+this.day;
		  }
	  }if(this.month==5){
		 if(isLeapYear(this.year)){
		  nth_Day=121+this.day;
		  } else{
		  nth_Day=120+this.day;
		  }
	  }
	  if(this.month==6){
		 if(isLeapYear(this.year)){
		  nth_Day=152+this.day;
		  } else{
		  nth_Day=151+this.day;
		  }
	  }
	  if(this.month==7){
		  if(isLeapYear(this.year)){
		  nth_Day=182+this.day;
		  } else{
		  nth_Day=181+this.day;
		  }
	  }
	  if(this.month==8){
		  if(isLeapYear(this.year)){
		  nth_Day=213+this.day;
		  } else{
		  nth_Day=212+this.day;
		  }
	  }
	  if(this.month==9){
		 if(isLeapYear(this.year)){
		  nth_Day=244+this.day;
		  } else{
		  nth_Day=243+this.day;
		  }
	  }
          if(this.month==10){
		 if(isLeapYear(this.year)){
		  nth_Day=274+this.day;
		  } else{
		  nth_Day=273+this.day;
		  }
	  }
	  if(this.month==11){
		 if(isLeapYear(this.year)){
		  nth_Day=305+this.day;
		  } else{
		  nth_Day=304+this.day;
		  }
	  }
	  if(this.month==12){
		 if(isLeapYear(this.year)){
		  nth_Day=335+this.day;
		  } else{
		  nth_Day=334+this.day;
		  }
	  }
    return nth_Day;                           // replace this line with your solution
  }

   public int dayRemainInYear() {
	   int Remain_Day=0;
	   if(isLeapYear(this.year)){
		  Remain_Day=366- this.dayInYear();
		  } else{
		  Remain_Day=365- this.dayInYear();
		  }
	   return (Remain_Day);
   }
    public int inBetweenDays(Date d) {
                  int sum=0;
		  for(int x = (d.year +1);x<(this.year);x++)
		  {if(isLeapYear(x)){sum=sum+366;}
			  else{sum=sum+365;}
		  }
		  return sum;
    }
    
    
    
    
     /** Determines whether this Date is before the Date d.
   *  @return true if and only if this Date is before d. 
   */
  public boolean isBefore(Date d) {
	  boolean isBeforevalue=false;
	  if(this.difference(d)<0){
		  isBeforevalue=true;
	  } else {
		  isBeforevalue=false;
	  }
    return isBeforevalue;                        // replace this line with your solution
  }

  /** Determines whether this Date is after the Date d.
   *  @return true if and only if this Date is after d. 
   */
  public boolean isAfter(Date d) {
	  boolean isAftervalue=false;
	  if(this.difference(d)>0){
		  isAftervalue=true;
	  } else {
		  isAftervalue=false;
	  }
    return isAftervalue;
                           // replace this line with your solution
  }
  
  
  
  
  /** Determines the difference in days between d and this Date.  For example,
   *  if this Date is 12/15/1997 and d is 12/14/1997, the difference is 1.
   * if this Date is after d Date then the result is positive
   *  If this Date occurs before d, the result is negative.
   *  @return the difference in days between d and this date.
   */
  public int difference(Date d) {
	  int difference_In_Day=0;
	  if(this.year== d.year){
	  difference_In_Day=this.dayInYear()- d.dayInYear();}
	  if(d.year<this.year){//(d year is 1975 and this year 1976 case
		  int sum=this.inBetweenDays(d);
		  difference_In_Day=this.dayInYear()+d.dayRemainInYear()+sum;
	  }
	  if(d.year> this.year){//(d year is 1976 and this year 1974 case
		  int sum=d.inBetweenDays(this);
		  difference_In_Day=-d.dayInYear()-this.dayRemainInYear()-sum;
	  }
    return difference_In_Day;                           // replace this line with your solution
    // the bug in this is year stuff this is for same year so change latter - Krishna
  }

  public static void main (String[] argv) {
    System.out.println("\nTesting constructors.");
   Date d1 = new Date(1, 1, 1);
    System.out.println("Date should be 1/1/1: " + d1);
    d1 = new Date("2/4/2");
    System.out.println("Date should be 2/4/2: " + d1);
    d1 = new Date("2/29/2000");
    System.out.println("Date should be 2/29/2000: " + d1);
    d1 = new Date("2/29/1904");
    System.out.println("Date should be 2/29/1904: " + d1);

     d1 = new Date(12, 31, 1975);
    System.out.println("Date should be 12/31/1975: " + d1);
    Date d2 = new Date("1/1/1976");
    System.out.println("Date should be 1/1/1976: " + d2);
    Date d3 = new Date("1/2/1976");
    System.out.println("Date should be 1/2/1976: " + d3);

    Date d4 = new Date("2/27/1977");
    Date d5 = new Date("8/31/2110");

    /* I recommend you write code to test the isLeapYear function! */

    System.out.println("\nTesting before and after.");
    System.out.println(d2 + " after " + d1 + " should be true: " + 
                       d2.isAfter(d1));
    System.out.println(d3 + " after " + d2 + " should be true: " + 
                       d3.isAfter(d2));
    System.out.println(d1 + " after " + d1 + " should be false: " + 
                       d1.isAfter(d1));
    System.out.println(d1 + " after " + d2 + " should be false: " + 
                       d1.isAfter(d2));
    System.out.println(d2 + " after " + d3 + " should be false: " + 
                       d2.isAfter(d3));

    System.out.println(d1 + " before " + d2 + " should be true: " + 
                       d1.isBefore(d2));
    System.out.println(d2 + " before " + d3 + " should be true: " + 
                       d2.isBefore(d3));
    System.out.println(d1 + " before " + d1 + " should be false: " + 
                       d1.isBefore(d1));
    System.out.println(d2 + " before " + d1 + " should be false: " + 
                       d2.isBefore(d1));
    System.out.println(d3 + " before " + d2 + " should be false: " + 
                       d3.isBefore(d2));

    System.out.println("\nTesting difference.");
    System.out.println(d1 + " - " + d1  + " should be 0: " + 
                       d1.difference(d1));
    System.out.println(d3 + " - " + d2  + " should be what ?: " + 
                       d3.difference(d2));
    System.out.println(d2 + " - " + d1  + " should be 1: " + 
                       d2.difference(d1));
    System.out.println(d3 + " - " + d1  + " should be 2: " + 
                       d3.difference(d1));
    System.out.println(d3 + " - " + d4  + " should be -422: " + 
                       d3.difference(d4));
    System.out.println(d5 + " - " + d4  + " should be 48762: " + 
                       d5.difference(d4));
  }
}


/*  if(0<month<13){
		  if(((month==1||month==3||month==5||month==7||month==8||month==10||month==12) && (0<day<32)) ||
			  ((month==4||month==6||month==9||month==11) && (0<day<31)) || ((month==2) && (o<day<29))){
			  if(
	  this.month=month;
	  } else {
		  // exception 
	  }
	  
*/
