package rebelsky.date; /** * Whee! A simple implementation of months. */ public class Month implements Comparable { /** The month: 1 = jan, 2 = feb, ... */ int month; public Month(int _month) { this.month = _month; } /** * Compare this month to another month. * Precondition: other must be a month. */ public int compareTo(Object other) { Month otherMonth = (Month) other; // If other isn't a month, this fails. return this.month - other.month; } // compareTo(Object) /** * Determine if this month is the same as another object. */ public boolean equals(Object other) { // Make sure that other is a month if (other instanceof Month) { Month otherMonth = (Month) other; return this.month == otherMonth.month; } // Whoops! other is not a Month. else { return this.toString().equals(other.toString()); } } // equals(Object) /** * Get the number of days in the whole year before the first day of * this month. */ public int daysPreceding() { int days = 0; for (int i = 1; i < this.month; i++) { days += daysInMonth(i); } // for each month preceding the current month return days; } // daysPreceding int daysInMonth(int mon) { switch (mon) { case 9: case 4: case 6: case 11: return 30; case 2: return 28; default: return 31; } // switch } // daysInMonth(int) /** * Represent this month as a string. */ public String toString() { return Integer.toString(month); } // toString() /** * Get the month as an integer. */ int get() { return this.month; } // get() } // class Month